最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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/vzafq25w

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/vzafq25w

Share Improve this question asked May 14, 2015 at 23:15 dprendpren 1,30513 silver badges18 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 8

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/be9200kh/1/

Normally, you can use Kefir.fromEvents, but in your case, where Mousetrap.js does not bind using on|off methods, you can instead just use Kefir.pool (Kefir.emitter was deprecated) and trigger Kefir in the Mousetrap callbacks. I modified your code to demonstrate using Kefir.pool in the Mousetrap callbacks: http://jsfiddle/be9200kh/

Basically, you do

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

Have fun!

发布评论

评论列表(0)

  1. 暂无评论