Search code examples
javascriptfrp

Kefir.js - How to stream events from a callback function?


The Mousetrap.js library lets you bind a callback function to keys like so:

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

What's the best way to attach a stream to this without using the Bus of Doom? Should I use emitter or pool?

I'm trying to get arrow keys hooked up in this fiddle: jsfiddle.net/vzafq25w


Solution

  • You can use general wrapper stream

    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/