当前位置: 首页 > 编程日记 > 正文

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

考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所需算法

相关文章:

iOS蓝牙4.0开发例子

1建立中心角色 123#import <CoreBluetooth/CoreBluetooth.h> CBCentralManager *manager; manager [[CBCentralManager alloc] initWithDelegate:self queue:nil]; 2扫描外设&#xff08;discover&#xff09; [manager scanForPeripheralsWithServices:nil options:…

Spark Shuffle原理解析

Spark Shuffle原理解析 一&#xff1a;到底什么是Shuffle&#xff1f; Shuffle中文翻译为“洗牌”&#xff0c;需要Shuffle的关键性原因是某种具有共同特征的数据需要最终汇聚到一个计算节点上进行计算。 二&#xff1a;Shuffle可能面临的问题&#xff1f;运行Task的时候才会产…

云开发使用 got 的 get/post 传参请求示例代码

使用 got 进行网络请求的步骤&#xff1a; 1.创建云函数&#xff0c;并在终端执行云函数 2.执行 npm 安装 got &#xff0c;命令&#xff1a;cnpm install --save got 3.在云函数中使用 示例代码&#xff1a; // 云函数入口文件 const cloud require(wx-server-sdk) cons…

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…

Flex 布局详解 - 转自阮一峰老师

Flex 是 Flexible Box 的缩写&#xff0c;意为"弹性布局"&#xff0c;用来为盒状模型提供最大的灵活性。任何的盒子都可以使用它。 下面我们来看一下使用 Flex 布局的容器的属性 flex-direction flex-wrap flex-flow justify-content align-items align-content 1.…

bzoj 1086: [SCOI2005]王室联邦

Description “余”人国的国王想重新编制他的国家。他想把他的国家划分成若干个省&#xff0c;每个省都由他们王室联邦的一个成员来管理。他的国家有n个城市&#xff0c;编号为1..n。一些城市之间有道路相连&#xff0c;任意两个不同的城市之间有且仅有一条直接或间接的道路。为…

在7分钟内深刻理解咖喱

Eric Elliott’s exceptional Composing Software series is initially what got me excited about functional programming. Its a must-read. 埃里克埃利奥特(Eric Elliott)杰出的合成软件系列最初使我对函数式编程感到兴奋。 这是必读的。 At one point in the series, he …

github后端开发面试题大集合(一)

作者&#xff1a;小海胆链接&#xff1a;https://www.nowcoder.com/discuss/3614?type0&order0&pos5&page0?fromwb来源&#xff1a;牛客网 转载自github&#xff0c;中文--->链接在这&#xff0c;英文---->链接在这文章较长&#xff0c;我把它拆分了三个部…

微信小程序左滑删除效果的实现完整源码附效果图

效果图&#xff1a; 功能描述&#xff0c;小程序列表左滑删除功能的实现完整源代码实现&#xff1a; <view wx:for{{friends}} wx:key"" wx:if{{groupType4}} catchtap"nav_oneChat" id"{{item._id}}" class"item p_r" style"…

Eclipse下修改工程名

一。 右键工程&#xff1a;Refactor->Rename&#xff0c;或选中工程按F2&#xff0c;修改名称 二。右键工程&#xff1a;Properties->Web Project Settings,修改Context Root三。1.找到项目所在位置&#xff08;如图&#xff09;&#xff1a; 2.修改项目目录/.setting目录…

git操作手册_基本的Git手册

git操作手册介绍 (Introduction) Hi! I am Sanjula, and in this guide I hope to teach you a little bit about Git including:嗨&#xff01; 我是Sanjula &#xff0c;在本指南中&#xff0c;我希望教您一些有关Git的知识&#xff0c;包括&#xff1a; What Git is 什么是…

PHP 接入(第三方登录)QQ 登录 OAuth2.0 过程中遇到的坑

前言 绝大多数网站都集成了第三方登录&#xff0c;降低了注册门槛&#xff0c;增强了用户体验。最近看了看 QQ 互联上 QQ 登录的接口文档。接入 QQ 登录的一般流程是这样的&#xff1a;先申请开发者 -> 然后创建应用&#xff08;拿到一组 AppId 和 AppKey&#xff09;-> …

npm全局环境变量配置,全局配置cnpm

今天新电脑想安装node.js &#xff0c; 发现最新版本的node.js已经不支持win7了&#xff0c;但是又不想换系统&#xff0c;所以找了个旧版本&#xff0c;这里不多说了。如果找不到旧版本的node下载&#xff0c;可以去我的QQ交流群文件里面下载&#xff0c;QQ群号&#xff1a;17…

NTFS for Mac OS X:使用Brew安裝NTFS-3G

在最新的OSX系統中&#xff0c;其實已經完整兼容NTFS文件系統&#xff0c;只是出於安全考慮默認是以只讀模式掛載NTFS分區的&#xff0c;可以透過diskutil查詢硬碟UUID&#xff0c;重新以讀寫(rw)權限掛載&#xff0c;具體的可以參照這裡 當然&#xff0c;也有現成的軟體幫助你…

javascript开关_JavaScript开关案例简介

javascript开关In this short article, I will introduce you to JavaScript switch cases and how to use them with practical examples.在这篇简短的文章中&#xff0c;我将向您介绍JavaScript切换案例以及如何通过实际示例使用它们。 This article will explain better wi…

C++中一个class类对象占用多少内字节(7个例子,很清楚)

一个空的class在内存中多少字节&#xff1f;如果加入一个成员函数后是多大&#xff1f;这个成员函数存储在内存中什么部分&#xff1f; 一个Class对象需要占用多大的内存空间。最权威的结论是&#xff1a; *非静态成员变量总合。 *加上编译器为了CPU计算&#xff0c;作出的数据…

Java学习----到底调用哪一个方法(多态)

public class Father {public void print() {System.out.println("Father:print()");} } public class Son extends Father{// 方法的覆盖&#xff1a;子类重写父类的同名方法 Overridepublic void print() {System.out.println("Son:print()");}// Father…

mpvue 转uni-app 操作记录

1.创建一个uni-app 的项目&#xff0c;把原有的mpvue项目手动迁移过来 2.用到mpvue特性的代码&#xff0c;需要修改成uni-app 兼容的&#xff0c;例如 mpvue-wxparse 可以修改成 3.src/app.json 改成 pages.json ,路径格式需要改&#xff0c;如下格式 {"pages": [&…

桌面应用程序 azure_Azure Logic应用程序用例–黑色星期五

桌面应用程序 azureThis blog gives an overview of how Azure Serverless technologies came to rescue when the custom-built integration system went down. Also, it shows the high-level architecture solution built using Azure Serverless services like Logic Apps,…

PHP的静态变量介绍

静态变量只存在于函数作用域内&#xff0c;也就是说&#xff0c;静态变量只存活在栈中。一般的函数内变量在函数结束后会释放&#xff0c;比如局部变量&#xff0c;但是静态变量却不会。就是说&#xff0c;下次再调用这个函数的时候&#xff0c;该变量的值会保留下来。 只要在变…

MySQL 解压版创建用户密码

root 权限进入MySQL&#xff1a; mysql –uroot 查看当前MySQL用户&#xff1a; select user,host from mysql.user; 此处以User为root&#xff0c;Host为localhost的账户为例&#xff0c;将密码改为password的命令&#xff1a; SET PASSWORD FOR rootlocalhost PASSWORD(newp…

git 忽略指定文件夹的上传

我们在使用 git 开发的时候&#xff0c;有些插件的模块文件通过npm install 就可以下载&#xff0c;一般是不上传到 git 中的&#xff08;因为文件太多会导致很耗时)&#xff0c;例如 我的 node_modules 文件夹&#xff0c;不想上传&#xff0c;我们应该这么做。 我们需要创建一…

数据库初学者_面向初学者的免费6小时数据科学课程

数据库初学者Data science is considered the "sexiest job of the 21st century." Learn data science in this full 6-hour course for absolute beginners from Barton Poulson of datalab.cc.数据科学被认为是“ 21世纪最艰巨的工作”。 通过datalab.cc的 Barton…

网页抓取及下载

downAndroidApk.php <?php /* 命令行 d: cd ApacheServer\php php.exe D:\ApacheServer\web\crawl\downAndroidApk.php --appidFileD:\ApacheServer\web\crawl\youxi.txt --newDirD:\ApacheServer\web\crawl\requestNewDir*/ // 判断必须在php-cli模式下运行&#xff0c;即…

javascript中关于this指向问题详解

前 言 LiuDaP 在前端的学习中&#xff0c;我们必然要用到js&#xff0c;js可以说是前端必不可少的的东西。在学习js的过程中&#xff0c;我们会经常用到this这个东西&#xff0c;而this的指向问题就变得尤为重要。今天正好有空闲时间&#xff0c;就给大家详细介绍一下js中关于…

mpvue 转uniapp 导航栏样式错乱问题修复 tabbar 样式修复

效果图&#xff1a;修改前&#xff0c;修改后 找了半天没找到原因&#xff0c;只能自己改样式了&#xff0c;下面是样式代码&#xff08;在app.vue 里面加上就行&#xff09; <style>/*每个页面公共css */uni-tabbar {box-sizing: border-box;position: fixed;left: 0;bo…

css规则_CSS规则,将使您的生活更轻松

css规则by Nick Gard尼克加德(Nick Gard) CSS规则&#xff0c;将使您的生活更轻松 (CSS rules that will make your life easier) After years of writing and maintaining a couple of very large web projects and numerous smaller ones, I have developed some heuristics…

在mybatis中模糊查询有三种写法

<select id"selectStudentsByName" resultType"Student"> <!--第一种--> <!-- select id,name,age,score from student where name like % #{0} % --> <!--第二种--> <!-- select id,name,age,score from student wher…

BZOJ 3566: [SHOI2014]概率充电器

题目&#xff1a;http://www.lydsy.com/JudgeOnline/problem.php?id3566 首先这题正着想不好想&#xff0c;考虑补集转化。 先dfs一遍&#xff0c;令f[u](1-p[u])*∏(1-(1-f[v])*w) f[u]表示u这个点通过其子树并不能联通的概率。 然后考虑v从其父亲连过来的情况&#xff0c;设…

小程序云开发,订阅消息定时批量发送实现代码

需求&#xff1a;做一个类似抽奖结果通知的订阅消息提醒 实现流程&#xff1a; 每个用户需要先授权订阅消息接收&#xff0c;授权成功后把数据存到云开发的数据集合里面&#xff0c;再写个定时器&#xff0c;遍历数据集合的所有数据&#xff0c;拿到后遍历发送订阅消息&#…