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

news/2024/7/1 4:44:09

考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:

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

  • Redux-Observable Can Solve Your State Problems

    Redux-Observable可以解决您的状态问题

  • Redux-Observable without Redux

    Redux-无需Redux就可观察

  • Callbacks: The Definitive Guide

    回调:权威指南

  • Promises: The Definitive Guide

    承诺:权威指南

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

考csp所需算法


http://lihuaxi.xjx100.cn/news/238078.html

相关文章

哈希函数是什么,在区块链中有什么用?

想知道更多关于区块链技术知识,请百度【链客区块链技术问答社区】 链客,有问必答!哈希函数是什么? 哈希函数,又叫散列函数、散列算法,是一种从任何一种数据中创建小的数字“指纹”(也叫做摘要&a…

深入学习Lock锁(2)——LockSupport工具类

2019独角兽企业重金招聘Python工程师标准>>> 在同步组件中,当需要阻塞或唤醒一个线程的时候,都会使用LockSupport工具类来完成相应 工作。LockSupport定义了一组的公共静态方法,这些方法提供了最基本的线程阻塞和唤醒功能&#xf…

自动化运维工具Saltstack(一)

1、saltstack简介: 什么是saltstack? saltstack是基于python开发的一套C/S架构配置管理工具 使用SSL证书签方的方式进行认证管理 号称世界上最快的消息队列ZeroMQ使得SaltStack能快速在成千上万台机器上进行各种操作 采用RSA Key方式确认身份 传输采用AE…

JavaScript词法作用域的简单介绍

by Michael McMillan迈克尔麦克米兰(Michael McMillan) JavaScript词法作用域的简单介绍 (An easy intro to Lexical Scoping in JavaScript) Lexical scoping is a topic that frightens many programmers. One of the best explanations of lexical scoping can be found in…

非对称加密及RSA算法

想知道更多关于区块链技术知识,请百度【链客区块链技术问答社区】 链客,有问必答!最近在学习区块链相关的知识,发现其保证去中心化的一个重要的手段就是基于密码学中的非对称加密。 何为非对称加密? 在回答这个问题之前…

处理器拦截器(HandlerInterceptor)详解

处理器拦截器(HandlerInterceptor)详解 编程界的小学生 关注 2017.04.06 15:19* 字数 881 阅读 657评论 0喜欢 4简介SpringWebMVC的处理器拦截器,类似于Servlet开发中的过滤器Filter,用于处理器进行预处理和后处理。 应用场景1、日…

saltstack实现haproxy+keepalived负载均衡+高可用(二)

一键部署haproxykeepalived实现负载均衡高可用 实验环境: !!!! 特别注意: www.westos.org为test1的minion名字 test1: 172.25.1.11 nginx master minion test2: 172.25.…

python学习day3

1丶 用户先进行登陆如果用户名在文件中且用户密码也正确就登陆成功调用购物车函数,如果用户用户名输入正确密码错误,提示用户密码错误且重新输入,如果用户 输入用户名不存在,提示用户是否创建该用户,调用注册函数。 1.…