Kefir.js - 如何从回调函数流式传输事件?
Kefir.js - How to stream events from a callback function?
Mousetrap.js 库允许您将回调函数绑定到键,如下所示:
Mousetrap.bind('space', function, 'keydown');
在不使用 Bus of Doom 的情况下将流附加到此的最佳方法是什么?我应该使用 emitter
还是 pool
?
我正试图在这个 fiddle 中连接箭头键:jsfiddle.net/vzafq25w
通常,您可以使用 Kefir.fromEvents
,但在您的情况下,Mousetrap.js 不使用 on|off
方法进行绑定,您可以只使用 Kefir.pool
(Kefir.emitter
已弃用)并在 Mousetrap 回调中触发 Kefir。我修改了您的代码以演示在捕鼠器回调中使用 Kefir.pool
:http://jsfiddle.net/be9200kh/
基本上,你会
var pool = Kefir.pool();
pool.plug(Kefir.constant(1));
pool.map(...).filter(etc)
玩得开心!
您可以使用通用包装器stream
var leftKeys = Kefir.stream(function(emitter){
Mousetrap.bind('left', function(e) {
emitter.emit(e);
console.log(e);
});
return function(){
// unbind
};
});
Mousetrap.js 库允许您将回调函数绑定到键,如下所示:
Mousetrap.bind('space', function, 'keydown');
在不使用 Bus of Doom 的情况下将流附加到此的最佳方法是什么?我应该使用 emitter
还是 pool
?
我正试图在这个 fiddle 中连接箭头键:jsfiddle.net/vzafq25w
通常,您可以使用 Kefir.fromEvents
,但在您的情况下,Mousetrap.js 不使用 on|off
方法进行绑定,您可以只使用 Kefir.pool
(Kefir.emitter
已弃用)并在 Mousetrap 回调中触发 Kefir。我修改了您的代码以演示在捕鼠器回调中使用 Kefir.pool
:http://jsfiddle.net/be9200kh/
基本上,你会
var pool = Kefir.pool();
pool.plug(Kefir.constant(1));
pool.map(...).filter(etc)
玩得开心!
您可以使用通用包装器stream
var leftKeys = Kefir.stream(function(emitter){
Mousetrap.bind('left', function(e) {
emitter.emit(e);
console.log(e);
});
return function(){
// unbind
};
});