admin管理员组文章数量:1335611
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 badges2 Answers
Reset to default 8You 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!
本文标签: javascriptKefirjsHow to stream events from a callback functionStack Overflow
版权声明:本文标题:javascript - Kefir.js - How to stream events from a callback function? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742392904a2466344.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论