反应式编程 - RxJS VS EventEmitter Node.js的中 [英] Reactive Programming - RxJS vs EventEmitter in Node.js

查看:588
本文介绍了反应式编程 - RxJS VS EventEmitter Node.js的中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我开始寻找它的反应式编程的概念工作 RxJS 并RxJava(来自Netflix)库。

Recently I've started looking at RxJS and RxJava(from Netflix) libraries which work on the concept of Reactive Programming.

Node.js的工作原理事件循环的基础上,为您提供所有异步编程和后续节点库,例如集群帮助您获得最佳的出你的多核机器的阿森纳。 Node.js的和还为您提供的EventEmitter功能,您可以订阅的事件和异步就此采取行动。

Node.js works on the basis of event loops, which provides you all the arsenal for asynchronous programming and the subsequent node libraries like "cluster" help you to get best out of your multi-core machine. And Node.js also provides you the EventEmitter functionality where you can subscribe to events and act upon it asynchronously.

在另一方面,如果我理解正确RxJS(和一般的反应式编程)工作在事件流的原则,订阅事件流,异步转换事件流数据。

On the other hand if I understand correctly RxJS (and Reactive Programming in general) works on the principle of event streams, subscribing to event streams, transforming the event stream data asynchronously.

所以,问题是使用Node.js的接收包的意思是什么呢。如何不同的是节点的事件循环,事件发射器和放大器;订阅到Rx的溪流和订阅。

So, the question is what does using Rx packages in Node.js mean. How different is the Node's event loop, event emitter & subscriptions to the Rx's streams and subscriptions.

推荐答案

您是正确的,接收流和EventEmitter非常相似,两者都是观察者模式的实现。

You are correct that Rx streams and EventEmitter are very similar, both are implementations of the Observer pattern.

的区别在于,接收包含功能用于转换和组合事件流。试想一下,比如,我们要减少2秒延迟每个回应事件。随着EventEmitter,你就必须订阅该,并进行手动超时:

The difference is that Rx contains functions for transforming and combining event streams. Imagine for instance that we want to delay each "response event" by 2 seconds. With EventEmitter, you would have to subscribe to that, and manually make a timeout:

eventEmitter.on('response', function(res) {
    setTimeout(function() { /* handle res */ }, 2000);
});

通过RX,您可以创建一个事件流是简单地应用于延时功能原始流:

With Rx, you can create a new event stream which is simply the original stream applied to the delay function:

responseStream.delay(2000).subscribe(function(res) {
    /* handle res */
});

延迟()是一个简单的功能,而的many他人提供。有修改流这么多不同的方式,这有可能编程很多应用程序逻辑仅仅通过变换流与所有可能的功能,而不是依赖于低层次的逻辑,如的setTimeout

delay() is a simple function, and one of the many others available. There are so many different ways of modifying streams, that it becomes possible to program a lot of application logic just by transforming streams with all the possible functions, instead of relying on low-level logic such as setTimeout.

另外,请查阅这些职能这种视觉和互动探索

这篇关于反应式编程 - RxJS VS EventEmitter Node.js的中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆