考csp所需算法_CSP vs RxJS:您所不知道的。

本文探讨了CSP(通信顺序进程)与RxJS之间的差异。CSP是一种使用共享通道在代码中不同功能间通信的方法,但在JavaScript中并未流行,而Redux-Saga和Redux-Observable作为其替代品得到应用。尽管CSP的概念如Redux-Saga中的“通道”依然存在,但RxJS的广泛社区和功能使得CSP在JavaScript中逐渐消亡。
摘要由CSDN通过智能技术生成

考csp所需算法

by Kevin Ghadyani

通过凯文·加迪亚尼(Kevin Ghadyani)

CSP vs RxJS:您所不知道的。 (CSP vs RxJS: what you don’t know.)

CSP发生了什么? (What happened to CSP?)

You probably clicked this article thinking “what is CSP?” It’s communicating sequential processes. Still baffled?

您可能以“什么是CSP?”的方式单击了这篇文章。 它在传达顺序过程 。 还在困惑吗?

CSP is a method of communicating between different functions (generators) in your code using a shared channel.

CSP是一种使用共享通道在代码中不同功能(生成器)之间进行通信的方法。

What in the world does that mean? Lemme tell it to ya straight. There’s this concept of a channel. Think of it like a queue. You can put stuff on it and take stuff off it.

这到底是什么意思? 莱姆直接告诉你。 有一个渠道概念。 认为它像一个队列。 您可以在上面放东西,也可以从上面取下东西。

So with two functions, you can have one adding stuff on the channel (producer) and another pulling things off and doing some work (consumer).

因此,通过两个功能,您可以在通道上(生产者)添加一个东西,而在通道上(消费者)添加另一个东西。

A typical advanced use case would be multiple producers and one consumer. That way you can control the data you’re getting, but you can have multiple things giving it to you.

一个典型的高级用例是多个生产者和一个消费者。 这样,您可以控制要获取的数据,但是可以有多种选择。

Unlike RxJS, these channels are automatic. You don’t get values willy-nilly, you have to ask for them.

与RxJS不同,这些通道是自动的。 您不会随意获取价值,您必须要求它们。

使用CSP (Using CSP)

Here’s a small CSP example using the super simple (and dead) library Channel4:

这是一个使用超级简单( 无效 )库Channel4的小型CSP示例:

CSP channels run asynchronously. So as soon as this runs, the synchronous “DONE” message gets logged first. Then our channel takers are executed in order.

CSP通道异步运行。 因此,一旦运行,同步“ DONE”消息将首先被记录。 然后我们的频道接收者将按顺序执行。

The most-interesting thing to me is the blocking (but async) nature of CSP. Notice how we created the third take before putting “C” on the channel. Unlike the first two take functions, the third one doesn’t have anything to take. Once something comes into the channel, it immediately takes it.

对我来说,最有趣的是CSP的阻塞性(但异步性)。 注意我们是如何创建的第三take放“C”通道之前。 与前两个take函数不同,第三个没有任何内容。 一旦有东西进入通道,它将立即接收它。

Also note, consumers need to be constantly taking things off the channel until that channel closes. That’s why “D” is never logged. You need to define another take to grab the next value off the channel.

还应注意,消费者需要不断从渠道中取出东西,直到该渠道关闭。 这就是为什么从未记录“ D”的原因。 您需要定义另一个take抢关通道的下一个值。

With observables, you’re given values so you don’t have to worry about manually pulling them off. If you want to buffer those values, RxJS provides quite a few pipeline methods for that very purpose. No need to use CSP.

使用可观察变量,您将获得值,因此您不必担心手动将其拖出。 如果要缓冲这些值,RxJS为此提供了很多管道方法。 无需使用CSP。

The entire concept behind observables is that every listeners gets the same data as soon as the observer calls next. With CSP, it’s like the IxJS approach where you’re dealing with data in chunks.

可观察对象背后的整个概念是,每个观察者在观察者next调用时都会获得相同的数据。 使用CSP,就像IxJS方法一样,您要分块处理数据。

CSP已死!! (CSP IS DEAD!?)

You can find CSP implementations in Go and Closure. In JavaScript, all but a couple CSP libraries are dead and even then, their audience is small ?.

您可以在Go and Closure中找到CSP实现。 在JavaScript中,除了几个CSP库之外,其他所有库都已消失,即使到那时,它们的受众还是很小的?

I found out about CSP from Vincenzo Chianese’s awesome talk. He recommended this high-end library called js-csp. Sadly, it’s no longer maintained.

我从Vincenzo Chianese的精彩演讲中发现了有关CSP 的信息 。 他推荐了这个高端库js-csp 。 可悲的是,它不再被维护。

Based on what he said in his 2017 talk, it seemed like a big deal. He talked about how transducers were going to to explode in a few months and how js-csp already had support for them.

根据他在2017年演讲中所说的话,这似乎很重要。 他谈到了换能器将在几个月后爆炸,以及js-csp已经对其提供了支持。

It looked like CSP could fundamentally change how you developed async applications in JavaScript. But none of that ever happened. Transducers died away; replaced by libraries like RxJS, and the hype around CSP dissolved.

看起来CSP可以从根本上改变您使用JavaScript开发异步应用程序的方式。 但是这些都没有发生。 换能器消失了。 替换为RxJS之类的库,并且消除了CSP的炒作。

Vincenzo did note how CSP is a whole ‘nother level above promises. He’s right. The power you get having multiple functions interacting asynchronously is incredible.

Vincenzo确实指出了CSP的整体水平是否超出了承诺。 他是对的。 具有异步交互的多个功能的强大功能令人难以置信。

Promises, by their eager nature, aren’t even in the same ballpark. Little did he know the last few CSP libraries would end up supporting promises under-the-hood ?.

从本质上说,诺言甚至不在同一个球场上。 他几乎不知道最后几个CSP库最终将支持幕后承诺吗?

CSP替代:Redux-Saga (CSP Alternative: Redux-Saga)

If you’ve ever used Redux-Saga, the ideas and concepts around CSP probably sound familiar. That’s because they are. In fact, Redux-Saga is an implementation of CSP in JavaScript; the most popular by far.

如果您曾经使用过Redux-Saga,关于CSP的想法和概念可能听起来很熟悉。 那是因为它们是。 实际上,Redux-Saga是JavaScript中CSP的实现; 迄今为止最受欢迎的。

There’s even a concept of “channels” in Redux-Sagas:https://github.com/redux-saga/redux-saga/blob/master/docs/advanced/Channels.md

Redux-Sagas中甚至有一个“通道”的概念: https : //github.com/redux-saga/redux-saga/blob/master/docs/advanced/Channels.md

Channels receive information from external events, buffer actions to the Redux store, and communicate between two sagas. It’s the same way they’re used in CSP with the same take and put functions.

通道从外部事件接收信息,缓冲到Redux存储的动作,并在两个sagas之间进行通信。 这与在CSP中使用相同的takeput函数的方式相同。

Pretty cool to see an actual implementation of CSP in JavaScript, but strange very few have noticed it. That shows you how little CSP took off before dying.

看到JavaScript中CSP的实际实现非常酷,但很少有人注意到它。 这说明您死前CSP起飞很少。

CSP替代:Redux-Observable (CSP Alternative: Redux-Observable)

You might’ve heard of something called Redux-Observable. This is a similar concept to CSP and Redux-Saga, but instead of the imperative style of generators, it takes a functional approach and utilizes RxJS pipelines referred to as “epics”.

您可能听说过Redux-Observable。 这是与CSP和Redux-Saga相似的概念,但它不是强制性的生成器样式,而是采用了功能性方法并利用了称为“史诗”的RxJS管道。

In Redux-Observable, everything happens through two subjects: action$ and state$. Those are your channels.

在Redux-Observable中,一切都通过两个主题发生: action$state$ 。 这些是您的频道。

Instead of manually taking and putting, you’re listening for specific actions as a consumer of an action or state channel. Each epic has the ability of also being a producer by sending actions through the pipeline.

您不是在手动操作,而是在作为操作或状态通道的使用者来监听特定操作。 每部史诗都有通过管道发送动作来成为制作人的能力。

If you want to build a queue in Redux-Observable just like CSP, it’s a little more complicated as there’s no operator available for this purpose, but it’s entirely possible.

如果您想要像CSP一样在Redux-Observable中构建队列,则要复杂一点,因为没有可用于此目的的运算符,但这是完全可能的。

I created a repl that does just that:

我创建了一个repl来做到这一点:

Compared to our earlier CSP example, this is what you can expect to see:

与我们之前的CSP示例相比,这是您可以期望看到的:

The example only requires RxJS and everything is in a single file for simplicity. As you can see, it’s a lot harder to queue up items in RxJS the same way you might with CSP. It’s entirely possible, but requires a lot more code.

该示例仅需要RxJS,为简单起见,所有内容都在一个文件中。 如您所见,与使用CSP一样,在RxJS中排队项目要困难得多。 这是完全可能的,但是需要更多代码。

Personally, I’d love to see RxJS add an operator like bufferWhen that allows you to divvy out individual items instead of the entire buffer. Then you could accomplish the CSP-style in Redux-Observable a lot easier.

就个人而言,我很乐意看到RxJS添加一个像bufferWhen的运算符,这样您就可以分出单个项目而不是整个缓冲区。 然后,您可以轻松完成Redux-Observable中的CSP样式。

结论 (Conclusion)

CSP was a cool concept, but it’s dead in JavaScript. Redux-Saga and Redux-Observable are worthy alternatives.

CSP是一个很酷的概念,但是死在JavaScript中。 Redux-Saga和Redux-Observable是值得的替代方案。

Even with the ability to integrate with transducer libraries, RxJS still has a clear leg-up. It’s massive community of educators and production applications makes it hard to compete.

即使能够与换能器库集成,RxJS仍然具有明显优势。 庞大的教育者社区和生产应用程序使其难以竞争。

That’s why I think CSP died in JavaScript.

这就是为什么我认为CSP死于JavaScript。

更多阅读 (More Reads)

If you liked what you read, please checkout my other articles on similar eye-opening topics:

如果您喜欢阅读的内容,请查看我的其他文章,这些文章涉及类似的令人大开眼界的主题:

翻译自: https://www.freecodecamp.org/news/csp-vs-rxjs-what-you-dont-know-1542cd5dd100/

考csp所需算法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值