Flux reference
https://facebook.github.io/flux/docs/dispatcher.html#content
首先安装
npm install --save flux
Dispatcher
dispatcher 和 订阅发布模式(pub-sub systems)有两个不同点:
- 在有事件触发的时候,每个注册到dispatcher上的回调函数都会接收到,它们不是针对指定事件才回调
- 回调函数可以延迟执行,可以等到其他所有或部分回调函数执行后才执行
例子
演示第一个不同点:
const flux = require("flux")
var dispatcher = new flux.Dispatcher();
dispatcher.register(function (payload) {
console.log("1")
console.log(payload)
});
dispatcher.register(function (payload) {
console.log("2")
console.log(payload)
});
dispatcher.dispatch({
actionType: 'city-update',
selectedCity: 'paris'
});
运行结果:
1
{ actionType: 'city-update', selectedCity: 'paris' }
2
{ actionType: 'city-update', selectedCity: 'paris' }
可见注册的所有回调函数都按顺序执行了。所以一般要在回调函数中判断类型,再执行后续的操作
演示第二个不同点:先执行第二个回调函数再执行第一个。
const flux = require("flux")
var dispatcher = new flux.Dispatcher();
dispatcher.register(function (payload) {
dispatcher.waitFor([token]);
console.log("1")
console.log(payload)
});
var token = dispatcher.register(function (payload) {
console.log("2")
console.log(payload)
});
dispatcher.dispatch({
actionType: 'city-update',
selectedCity: 'paris'
});
执行结果:
2
{ actionType: 'city-update', selectedCity: 'paris' }
1
{ actionType: 'city-update', selectedCity: 'paris' }
dispatcher原理简单实现如下:
class Dispatcher {
constructor() {
this.cbs = {}
this.order = []; // for marking the register order
this.curPayload = null;
}
register(callback) {
var token = Math.random()
this.cbs[token] = {
callback,
exected: false,
}
this.order.push(token);
return token
}
waitFor(tokens) {
var self = this;
tokens.forEach((token) => {
var c = self.cbs[token]
c.exected || c.callback(self.curPayload)
c.exected = true;
})
}
dispatch(payload) {
this.curPayload = payload
var self = this;
this.order.forEach((token) => {
var c = self.cbs[token]
c.exected || c.callback(payload)
})
// reset status
this.order.forEach((token) => {
var c = self.cbs[token]
c.exected = false;
})
}
}
Flux Utils
flux utils 提供了一些工具类来帮助我们实现一个简单的flux架构,但它们不具备flux架构的所有特征,不能帮我们实现所有的用户场景。
这个工具集主要暴露了 3 个类出来,分别是:Store、ReduceStore和Container
主要使用的是ReduceStore和Container
https://github.com/947133297/reactDemo/tree/master/simple-flux
Flux reference的更多相关文章
- 构建具有用户身份认证的 React + Flux 应用程序
原文:Build a React + Flux App with User Authentication 译者:nzbin 译者的话:这是一篇内容详实的 React + Flux 教程,文章主要介绍了 ...
- Spring Boot Reference Guide
Spring Boot Reference Guide Authors Phillip Webb, Dave Syer, Josh Long, Stéphane Nicoll, Rob Winch, ...
- React: 研究Flux设计模式
一.简介 一般来说,State管理在React中是一种最常用的实现机制,使用这种state管理系统基本可以开发各种需求的应用程序.然而,随着应用程序规模的不断扩张,原有的这种State管理系统就会暴露 ...
- ASP.NET Core: You must add a reference to assembly mscorlib, version=4.0.0.0
ASP.NET Core 引用外部程序包的时候,有时会出现下面的错误: The type 'Object' is defined in an assembly that is not referenc ...
- Flux 普及读本
话说当时做 APP 时,三月不知肉味,再次将眼光投放前端,有种天上一天,地下一年的感觉. Flux 是一种思想 了解的最好方式当然是看Flux官方文档了.React 中文站点也能找到对应的翻译版本,但 ...
- 【转】Django Model field reference学习总结
Django Model field reference学习总结(一) 本文档包含所有字段选项(field options)的内部细节和Django已经提供的field types. Field 选项 ...
- (转) Qt 出现“undefined reference to `vtable for”原因总结
由于Qt本身实现的机制所限,我们在使用Qt制作某些软件程序的时候,会遇到各种各样这样那样的问题,而且很多是很难,或者根本找不到原因的,即使解决了问题,如果有人问你为什么,你只能回答--不知道. 今天我 ...
- undefined reference to `__android_log_print'
使用android studio 编写NDK代码时出现错误:undefined reference to `__android_log_print' 解决办法: eclipse andro ...
- CentOS 6.5 编译 PHP-7 报错:undefined reference to `libiconv_open 无法编译 PHP libiconv
./configure --with-mysql=/backup/mysql --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zli ...
随机推荐
- unicode码表和标准下载 unicode官网
- Tinghua Data Mining 7
SVM B分割得更加无偏 比较公平 卡着分界面的点叫支持向量,就好比托着分界面 支持向量决定了可移动的范围,这个范围就叫margin 分界面可移动的距离 前提是先要被分对 对偶问题一般是不等价的,但是 ...
- [已读]编写高质量代码--Web前端开发修炼之道
我觉得还蛮实用的一本,推荐看看,主要涉及到这些: 标签语义化.css模块化. css的一些东西,比如haslayout 文档流,还有如何实现水平.垂直居中. js代码组织与js分层.js压缩 编码规范 ...
- How to detect the presence of the Visual C++ 2010 redistributable package
Question: I have seen your previous blog posts that describe how to detect the presence of the Visua ...
- [Luogu1343]地震逃生 最大流
题目链接:https://www.luogu.org/problem/show?pid=1343 dinic跑最大流. #include<cstdio> #include<cstri ...
- TP框架设置验证码
thinkphp框架有专门的的验证码生成的模块 public function shengcheng(){ $n = new \Think\Verify(); $n->entry(); } 下面 ...
- link标签的media属性的用法
<link rel=stylesheet" type="text/css" href="print.css" media="scree ...
- “Debug Assertion” Runtime Error on VS2008 VS2010 winhand.cpp
I'm writing a C++ MFC program on VS2008 and I'm getting this "Debug Assertion Error" when ...
- Android属性系统简介
1.简介 在android 系统中,为统一管理系统的属性,设计了一个统一的属性系统.每个属性都有一个名称和值,他们都是字符串格式.属性被大量使用在Android系统中,用来记录系统设置或进程之间的信息 ...
- 【读书笔记】构建之法(CH1~CH3)
人类文明的发展离不开哲学家的思考.科学家的发现和工程师的构建.三个简单的方程式解释了什么是现代软件工程: 1.程序=算法+数据结构 2.软件=程序+软件工程 3.软件企业=软件+商业模式 软件开发的不 ...