Created
February 10, 2015 08:46
-
-
Save cultofmetatron/7e0b2045fa6a8c253b29 to your computer and use it in GitHub Desktop.
a reactive remote interface
This file contains hidden or 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
/* | |
* this object is a set of streams that can be subscribed to | |
* forworking with the minimal interface. | |
* | |
* up, down, left, right, enter | |
* | |
*/ | |
var Rx = require('rx'); | |
var _ = require('lodash'); | |
var keyupStream = Rx.Observable.fromEvent(document, 'keyup'); | |
var keydownStream = Rx.Observable.fromEvent(document, 'keydown'); | |
//add additional key commands here | |
var inputs = { | |
'left': 37, | |
'right': 39, | |
'down': 40, | |
'up': 38, | |
'enter': 13 | |
}; | |
//helper method for creating the individual streams | |
var createPressStream = function(inputType) { | |
if (_.isUndefined(inputType)) { throw new Error('input type undefined');} | |
return keydownStream.sample(keyupStream) | |
.map(function(event) { | |
//console.log(event.keyCode) //why does this code run multiple times? | |
return event.keyCode; | |
}) | |
.filter(function(code) { | |
return code === inputs[inputType]; | |
}); | |
}; | |
var inputStreams = {} | |
_.each(_.keys(inputs), function(inputType) { | |
//we set up a subject proxy so that we can have multiple subscribers | |
var msgProxy = new Rx.Subject(); | |
createPressStream(inputType).subscribe(msgProxy); | |
inputStreams[inputType + 'Key'] = msgProxy; | |
}); | |
module.exports = inputStreams; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment