Kefir.js - How do I pass events from a callback function?

The Mousetrap.js library allows you to bind a callback function to these keys:

Mousetrap.bind('space', function, 'keydown');

What is the best way to connect a stream to it without using Bus of Doom ? Should I use emitter or pool ?

I am trying to connect the arrow keys in this script: jsfiddle.net/vzafq25w

+8
javascript frp
source share
2 answers

You can use the general stream shell

 var leftKeys = Kefir.stream(function(emitter){ Mousetrap.bind('left', function(e) { emitter.emit(e); console.log(e); }); return function(){ // unbind }; }); 

http://jsfiddle.net/be9200kh/1/

+8
source share

You can usually use Kefir.fromEvents , but in your case when Mousetrap.js is not connected using the on|off methods, instead you can just use Kefir.pool ( Kefir.emitter deprecated) and run kefir in Mousetrap callbacks. I modified your code to demonstrate the use of Kefir.pool in Mousetrap callbacks: http://jsfiddle.net/be9200kh/

Basically you do

 var pool = Kefir.pool(); pool.plug(Kefir.constant(1)); pool.map(...).filter(etc) 

Good luck

+2
source share

All Articles