[React + Functional Programming ADT] Create Redux Middleware to Dispatch Actions with the Async ADT
We would like the ability to group a series of actions to be dispatched with single dispatching functions used as handlers in various parts of our game. The only issue with that, is that animations and other design elements in our game require us to provide some temporal space between each of those actions in the series being fired.
This is something we can achieve by reaching for the Async ADT provided by the crocks library. In order to allow for Asyncs in our Redux flow we are going to need to create some middleware that can identify when an Async is provided as an action, and then handle it appropriately .
Here we have dispatched three action on game start:
const mapDispatch = dispatch => ({
answer: unit,
restart: unit,
start: () => dispatch([
startGame(),
hideAllCards(),
startTurn()
]),
})
Now that we want is after we call 'startGame', we should give players 5 second to remember each cards, then we dispatch 'hideAllCards' & 'startTurn' actions.
We can use Async ADT for this:
import Async from 'crocks/Async'
const mapDispatch = dispatch => ({
answer: unit,
restart: unit,
start: () => dispatch([
startGame(),
Async.resolveAfter(, hideAllCards()),
Async.resolveAfter(, startTurn()),
]),
})
It is not enough for the program to working yet, because Redux by default expect sync opreation, not async opreation, to take async operation, we need async middleware:
import Async from 'crocks/Async'
import compose from 'crocks/helpers/compose'
import isSameType from 'crocks/predicates/isSameType' export const errAction = payload => ({
type: 'ASYNC_ERROR', payload, error: true
}) export default function asyncMiddleware({dispatch}) {
return next => action =>
isSameType(Async, action) ?
action.fork(compose(next, errAction), dispatch) : //fork(reject, resolve)
next(action)
}
Last apply the middleware:
import { createStore, applyMiddleware, compose } from 'redux'
import asyncMiddleware from './middleware/async'
import multiMiddleware from './middleware/multi'
import reducer from './reducers'
import { initialState } from './model/initialize'
const middleware = applyMiddleware(
asyncMiddleware,
multiMiddleware
)
const composeEnhancers =
window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose
export default createStore(
reducer,
initialState(),
composeEnhancers(middleware)
)
[React + Functional Programming ADT] Create Redux Middleware to Dispatch Actions with the Async ADT的更多相关文章
- [React + Functional Programming ADT] Create Redux Middleware to Dispatch Multiple Actions
We only have a few dispatching functions that need to be known by our React Application. Each one ac ...
- [React + Functional Programming ADT] Connect State ADT Based Redux Actions to a React Application
With our Redux implementation lousy with State ADT based reducers, it is time to hook it all up to a ...
- [Functional Programming] Randomly Pull an Item from an Array with the State ADT (Pair)
Functor composition is a powerful concept that arises when we have one Functor nested in another Fun ...
- Functional Programming without Lambda - Part 1 Functional Composition
Functions in Java Prior to the introduction of Lambda Expressions feature in version 8, Java had lon ...
- a primary example for Functional programming in javascript
background In pursuit of a real-world application, let’s say we need an e-commerce web applicationfo ...
- redux middleware 源码分析
原文链接 middleware 的由来 在业务中需要打印每一个 action 信息来调试,又或者希望 dispatch 或 reducer 拥有异步请求的功能.面对这些场景时,一个个修改 dispat ...
- BETTER SUPPORT FOR FUNCTIONAL PROGRAMMING IN ANGULAR 2
In this blog post I will talk about the changes coming in Angular 2 that will improve its support fo ...
- Monad (functional programming)
In functional programming, a monad is a design pattern that defines how functions, actions, inputs, ...
- Beginning Scala study note(4) Functional Programming in Scala
1. Functional programming treats computation as the evaluation of mathematical and avoids state and ...
随机推荐
- 最小生成树---->prim算法的应用 hdu1863
畅通工程 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submi ...
- nyoj 737 石子合并 http://blog.csdn.net/wangdan11111/article/details/45032519
http://blog.csdn.net/wangdan11111/article/details/45032519 http://acm.nyist.net/JudgeOnline/problem. ...
- 欧拉回路 uoj117
写了一道欧拉回路的模板题.先判断是否是欧拉回路,有向图和无向图有一点点不同,然后就是特判独立点的存在. 之后是输出路径,和dls学的dfs,利用last数组的更新可以做到线性的复杂度,否则一不小心就会 ...
- 「BZOJ4763」雪辉
「BZOJ4763」天野雪辉 题目大意:有一棵 \(n\) 个点的树,树上每一个点有权值 \(a_i \leq 30000\) ,每次询问给出若干路径,求出这些路径的并上面的不同颜色数与 \(mex\ ...
- 【并查集】BZOJ1370- [Baltic2003]Gang团伙
[题目大意] 在某城市里住着n个人,任何两个认识的人不是朋友就是敌人,而且满足: 1. 我朋友的朋友是我的朋友: 2. 我敌人的敌人是我的朋友: 所有是朋友的人组成一个团伙.告诉你关于这n个人的m条信 ...
- 【动态规划/多重背包问题】POJ1014-Dividing
多重背包问题的优化版来做,详见之前的动态规划读书笔记. dp[i][j]表示前i中数加得到j时第i种数最多剩余几个(不能加和得到i的情况下为-1)递推式为: dp[i][j]=mi(dp[i-1][j ...
- bzoj 1101
其实这个用的是Mobius反演的第二种形式 F(d) = (n div d) * (m div d) f(d) = [ gcd(i,j)=d ] (i in [1,a], j in [1,b]) /* ...
- POJ 2774 Long Long Message 后缀数组模板题
题意 给定字符串A.B,求其最长公共子串 后缀数组模板题,求出height数组,判断sa[i]与sa[i-1]是否分属字符串A.B,统计答案即可. #include <cstdio> #i ...
- HDU 4611 Balls Rearrangement 数学
Balls Rearrangement 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=4611 Description Bob has N balls ...
- UESTC 2015dp专题 N 导弹拦截 dp
导弹拦截 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/65 Descrip ...