Skip to content

Instantly share code, notes, and snippets.

@Gozala
Last active December 13, 2015 21:19
Show Gist options
  • Save Gozala/4976759 to your computer and use it in GitHub Desktop.
Save Gozala/4976759 to your computer and use it in GitHub Desktop.
"use strict";
let recorder=require("micropilot").Micropilot("addressbar").start();
let possibles = [
"where to do today?",
"search or enter an address",
"your wish is my command"
];
require('simple-prefs').prefs.logtoconsole=true
const { event } = require("dom-reduce/event")
const { filter, expand, fold } = require("reducers/index")
const { windows, isBrowser } = require("sdk/window/utils")
const { open, close } = require("sdk/window/events");
// All windows presently open & ones that will open in the future.
// Note: Both arrays & streams of events are logical collections
// and reducers there for can concatinate them into another logical
// collection.
let browsers = filter(concat(windows(), open), isBrowser)
// Expand browsers to it's click event & which pairs. Expansion
// is like result of `map`-ing and then `flatten`-ing.
let urlBarClicks = expand(browsers, function(window) {
let which = Math.floor(Math.random() * possibles.length)
// I would encapsulate all the mutations in the folds, but
// as I don't know much about this and weather it's ok to
// do it it in the fold below, I'm leaving it here.
window.gURLBar.placeholder = possibles[which]
// `event` function just does the boileplate of registering
// listeners and returns stream of dispatched events.
let clicks = event(window.gURLBar, "click")
// Note: We probably need:
// takeUntil(clicks, filter(close, function(x) x === window))
// So that click handler is automatically removed on window
// close.
// Pair events with `which`.
return map(clicks, function(event) [event, which])
})
// Finally consume all the [event, which] paris for all
// browser windows that will ever open.
fold(urlBarClicks, function([event, which]) {
console.log('clicked!', possibles[which]);
recorder.record({ ts:Date.now(), msg:'searchbar', txt:possibles[which] })
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment