Skip to content

Instantly share code, notes, and snippets.

@cwharris
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cwharris/e6cf8f0ebf3784565db9 to your computer and use it in GitHub Desktop.
Save cwharris/e6cf8f0ebf3784565db9 to your computer and use it in GitHub Desktop.
var Rx = require('rx'),
log = console.log.bind(console);
Rx.window = Rx; // #hack to make rx-dom work
require('rx-dom');
var prop = 'which',
map = {
37: 'west',
38: 'north',
39: 'east',
40: 'south'
};
Rx.DOM.fromEvent(document.body, 'keydown')
.map(function (e) { return e.which; })
.filter(function (which) {
return map.hasOwnProperty(which);
})
.map(function (key) { return map[key]; })
.subscribe(log);
var Rx = require('rx'),
log = console.log.bind(console);
Rx.window = Rx; // #hack to make rx-dom work
require('rx-dom');
Rx.DOM.fromEvent(document.body, 'keydown')
.pluckMap('which', {
37: 'west',
38: 'north',
39: 'east',
40: 'south'
})
.subscribe(log);
Rx.Observable.prototype.pluckMap = function (prop, obj) {
var self = this;
return Rx.Observable.create(function (o) {
return self.subscribe(onNext, o.onError, o.onCompleted);
function onNext (x) {
var key = x[prop];
if (obj.hasOwnProperty(key)) {
o.onNext(obj[key]);
}
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment