Redux 实现过程的推演
- /*
- * createStore 状态容器
- * @param reducers 容器总得需要知道是做什么样的改变
- * @param initialState 初始化(预置)的 state
- */
- const createStore = (reducers, initialState) => {
- // 通过 reducer 取到改变后的值,重新存储
- let currentReducer = reducers;
- // 存
- let currentState = initialState;
- // 取
- const getState = () => {
- return currentState;
- };
- // 改
- const dispatch = action => {
- currentState = currentReducer(currentState, action);
- return action;
- };
- // 这里可能还需要可被观察的,留坑、不实现,有兴趣的看文章后的源码阅读链接
- return {
- getState,
- dispatch
- };
- };
- /*
- * action
- * @property type 描述需要做什么操作
- * @property preload 预加载数据,包含 state 的新值
- */
- const RESET = "RESET";
- const RESET_ACTION = {
- type: RESET,
- preload: {
- count: 0
- }
- };
- /*
- * reducer
- * @currentState 当前的 state,旧值
- * @action 该做什么修改的类型描述
- */
- const reducer = (state = { count: 0 }, action) => {
- switch (action.type) {
- case RESET: {
- return {
- ...state,
- ...action.preload
- };
- }
- default: {
- return state;
- }
- }
- };
- const store = createStore(reducer);
- store.dispatch({ type: RESET, preload: { count: 10 } });
- store.getState(); // output { count: 10}
- store.dispatch(RESET_ACTION);
- store.getState(); // output { count: 0}
- dispatch({ type: "@redux/INIT" });
Middleware is the suggested way to extend Redux with custom functionality. Middleware lets you wrap the store's dispatch method for fun and profit. The key feature of middleware is that it is composable. Multiple middleware can be combined together, where each middleware requires no knowledge of what comes before or after it in the chain.
Middleware 是通过自定义功能来扩展 redux 的推荐方法,它能够让你有效的包裹 store 的 dispatch 方法已达到所需的目的,其关键特征在于组合,多个 middleware 能够进行组合,每个 middleware 都是独立的,它们不需要知道在流程的之前或之后会发生什么。
- var a = function(next) {
- console.log("a-before");
- next();
- console.log("a-after");
- };
- var dispatch = function(action) {
- console.log("do ", action);
- return action;
- };
- a(dispatch);
- // output:
- // a-before
- // do undefined
- // a-after
- var a = function(next) {
- return function(action) {
- console.log("a-before");
- next(action);
- console.log("a-after");
- };
- };
- var dispatch = function(action) {
- console.log("do ", action);
- return action;
- };
- a(dispatch)("test action");
- // output:
- // a-before
- // do test action
- // a-after
- var a = function(next) {
- return function(action) {
- console.log("a-before");
- next(action);
- console.log("a-after");
- };
- };
- var b = function(next) {
- return function(action) {
- console.log("b-before");
- next(action);
- console.log("b-after");
- };
- };
- var dispatch = function(action) {
- console.log("do ", action);
- return action;
- };
- a(b(dispatch))("test action");
- // output:
- // a-before
- // b-before
- // do test action
- // b-after
- // a-after
- var a = function(next) {
- return function(action) {
- console.log("a-before");
- next(action);
- console.log("a-after");
- };
- };
- var b = function(next) {
- return function(action) {
- console.log("b-before");
- next(action);
- console.log("c-after");
- };
- };
- var c = function(next) {
- return function(action) {
- console.log("c-before");
- next(action);
- console.log("c-after");
- };
- };
- var dispatch = function(action) {
- console.log("do ", action);
- return action;
- };
- var d = [a, b, c].reduce((pre, now) => (...args) => pre(now(...args)));
- d(dispatch)("test action");
- // output:
- // a-before
- // b-before
- // c-before
- // do test action
- // c-after
- // b-after
- // a-after
- const compose = (...funcs) => {
- if (funcs.length === 0) {
- return arg => arg;
- }
- if (funcs.length === 1) {
- return funcs[0];
- }
- return funcs.reduce((a, b) => (...args) => a(b(...args)));
- };
- /*
- * createStore 状态容器
- * @param reducers 容器总得需要知道是做什么样的改变
- * @param initialState 初始化(预置)的 state
- * @param enhancer 扩展的 middlewares
- */
- const createStore = (reducers, initialState, enhancer) => {
- // 参数互换 如果 initialState 是个函数,enhancer = undefined 则 enhancer 和 initialState 互换
- if (typeof initialState === "function" && typeof enhancer === "undefined") {
- enhancer = initialState;
- initialState = undefined;
- }
- // 如果有 middleware 的时候,则 createStore 稍后处理,处理详情参照 applyMiddleware 函数
- if (typeof enhancer !== "undefined" && typeof enhancer === "function") {
- // 为什么是这样写? 继续往下看
- return enhancer(createStore)(reducer, initialState);
- }
- // ...
- // 之前的代码
- };
- /*
- * applyMiddleware 实现中间件的应用
- * @param ...middlewares 插入的 state 处理流程的中间件
- */
- const applyMiddleware = (...middlewares) => {
- // 传入 middlewares
- return createStore => (...args) => {
- const store = createStore(...args);
- // middleware 内部能做的 state 操作
- const middlewareAPI = {
- getState: store.getState,
- dispatch: (...args) => dispatch(...args)
- };
- // 将 middleware 处理,以 middlewareAPI 作为参数执行并且取到 middleware 的内部函数
- const chain = middlewares.map(middleware => middleware(middlewareAPI));
- // 进行 compose 组合
- // 如存在 3 个 middleware A(ABefore,AAfter) B(BBefore,BAfter) C(CBefore,CAfter)
- // 则执行顺序是 ABefore - BBefore - CBefore - (真实的操作) - CAfter - BAfter - AAfter
- dispatch = compose(...chain)(store.dispatch);
- return {
- ...store,
- dispatch
- };
- };
- };
- const logger = ({ getState }) => {
- return next => action => {
- console.log("will dispatch", action);
- const returnValue = next(action);
- console.log("state after dispatch", getState());
- return returnValue;
- };
- };
- const store = createStore(reducer, applyMiddleware(logger));
- store.dispatch(RESET_ACTION);
- // output
- // will dispatch {type: "RESET", preload: {…}}
- // do dispatch
- // state after dispatch {count: 0}
Redux 实现过程的推演的更多相关文章
- 从匿名方法到 Lambda 表达式的推演过程
Lambda 表达式是一种可用于创建委托或表达式目录树类型的匿名函数. 通过使用 lambda 表达式,可以写入可作为参数传递或作为函数调用值返回的本地函数. 以上是msdn官网对Lambda 表达式 ...
- 从下往上看--新皮层资料的读后感 第三部分 70年前的逆向推演- 从NN到ANN
第三部分 NN-ANN 70年前的逆向推演 从这部分开始,调整一下视角主要学习神经网络算法,将其与生物神经网络进行横向的比较,以窥探一二. 现在基于NN的AI应用几乎是满地都是,效果也不错,这种貌似神 ...
- Lambda 表达式推演全过程
Java 的 Lambda 表达式推演过程: 第一步:正常的类实现(外部实现),new一个对象,然后重写方法实现 public class TestLambda3 { public static vo ...
- .NET 云原生架构师训练营(ASP .NET Core 整体概念推演)--学习笔记
演化与完善整体概念 ASP .NET Core 整体概念推演 整体概念推演到具体的形式 ASP .NET Core 整体概念推演 ASP .NET Core 其实就是通过 web framework ...
- C语言 数组做函数参数退化为指针的技术推演
//数组做函数参数退化为指针的技术推演 #include<stdio.h> #include<stdlib.h> #include<string.h> //一维数组 ...
- javascript基础修炼(4)——UMD规范的代码推演
javascript基础修炼(4)--UMD规范的代码推演 1. UMD规范 地址:https://github.com/umdjs/umd UMD规范,就是所有规范里长得最丑的那个,没有之一!!!它 ...
- 【转】- 从FM推演各深度CTR预估模型(附代码)
从FM推演各深度CTR预估模型(附代码) 2018年07月13日 15:04:34 阅读数:584 作者: 龙心尘 && 寒小阳 时间:2018年7月 出处: 龙心尘 寒小阳
- Android 进程常驻----native保活5.0以上方案推演过程以及代码
正文: 上一篇我们通过父子进程间建立双管道,来监听进程死掉,经过测试,无耗电问题,无内存消耗问题,可以在设置中force close下成功拉起,也可以在获取到root权限的360/cleanmaste ...
- Android 进程常驻----native保活5.0以下方案推演过程以及代码
正文: 今天继续昨天,一鼓作气,争取这个礼拜全部写完. 上一篇文章留了一个别人的github链接,他里面的native保活实现方案也是大多数公司采用的方案. 我们先来讲一下他的方案. 他是首先开启一个 ...
随机推荐
- CCS中cmd文件的编写
http://blog.sina.com.cn/s/blog_abe5740601015b3q.html CMD的专业名称叫链接器配置文件,是存放链接器的配置信息的,我们简称为命令文件,其中比较关键的 ...
- AHOI——Day1个人感悟
今天,是个bilibili的日子.(嗯?什么意思?) 洛谷已经尽力了: 于是我带着洛谷的祝福,来到了AHOI的考场--合肥一中. 其实我是考完才签到的,我一大早五点多就起来了,到考场后,在肯德基吃了早 ...
- 02.02.02 第2章 制作power bi图表(Power BI商业智能分析)
---恢复内容开始--- 02.02.02第2章 制作power bi图表 02.02.02.01 power pivot数据导入 00:08:43 02.02.02.02建立数据透视表 00:11: ...
- Buffer.h
#ifndef __NOXIMBUFFER_H__ #define __NOXIMBUFFER_H__ #include <cassert> #include <queue> ...
- 工程无法正常调试运行unknown failure at android.os.Binder.execTransact
同事正常使用的工程,放到另电脑上,开后可以正常编译,但是无法安装调试到手机上,始终提示错误 新建一个工程正常. 最后通过把开发工具升级到最新版本解决.
- 线程中的setDaemon方法
setDaemon方法必须在start方法前定义.t1.setDaemon(True),该语句的意思是:将主线程A设置为子线程t1的守护线程.也就是在执行程序时,t1会随着主线程A的退出而退出,不论t ...
- [译]迁移到新的 React Context Api
随着 React 16.3.0 的发布,context api 也有了很大的更新.我已经从旧版的 api 更新到了新版.这里就分享一下我(作者)的心得体会. 回顾 下面是一个展示如何使用旧版 api ...
- 命令更新emacs
sudo apt-add-repository -y ppa:adrozdoff/emacs sudo apt update sudo apt install emacs25
- spring整合mybatis框架
1.导入jar包 2.配置文件 a. applicationContext.xml文件 <beans xmlns="http://www.springframework.org/ ...
- 把纯C的动态库代码改造成C++版的
近期想把一份纯C的跨Win/Linux的动态库工程代码改成支持C++编译器,这样用C++写起代码来比较顺手.要点是保证动态库的ABI一致性,既导出接口不能改变. 主要的改动有: 1.把.c后缀名换成. ...