Last active
August 29, 2015 14:00
-
-
Save cwharris/e6cf8f0ebf3784565db9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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