[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 ...
随机推荐
- Python网络管理模块Paramiko-代码实例
import paramiko import time import sys ip = "10.1.1.1" username = "Admin" passwo ...
- Markdown 实现首行缩进
把输入法由半角改为全角. 两次空格之后就能够有两个汉字的缩进. 在开头的时候,先输入这个,然后紧跟着输入文本即可.分号也不要掉.
- AM335x内核模块驱动之LED
在Ubuntu的任意可操作的文件才建立text目录 在text中建立zyr-hello.c: #include<linux/kernel.h> #include<linux/modu ...
- 在Azure中创建asp.net core 应用
0.前言 在玩转Azure之前首先大家要有Azure账号,或者可以先申请一下微软的账号,然后进行与Azure的关联(azure账号是免费的).但是关联的步骤还是很有意思的,他需要VISA国际信用卡(我 ...
- shell 空间利用
root #df -h / Filesystem Size Used Avail Use% Mounted on /dev/sda3 3.3G 1.7G 1.5G 54% /
- Redis学习与总结
Redis是业界普遍应用的缓存组件,研究一个组件框架,最直观的办法就是从应用方的角度出发,将每个步骤的考虑一番,从这些步骤入手去研究往往能够最快的体会到一个组件框架的设计哲学.以Redis为例,每当发 ...
- 【BZOJ 3676】 3676: [Apio2014]回文串 (SAM+Manacher+倍增)
3676: [Apio2014]回文串 Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 2343 Solved: 1031 Description 考 ...
- 【BZOJ 3566】 3566: [SHOI2014]概率充电器 (概率树形DP)
3566: [SHOI2014]概率充电器 Description 著名的电子产品品牌 SHOI 刚刚发布了引领世界潮流的下一代电子产品——概率充电器:“采用全新纳米级加工技术,实现元件与导线能否通电 ...
- HDU 5514 Frogs(容斥原理)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5514 [题目大意] m个石子围成一圈,标号为0~m-1,现在有n只青蛙,每只每次跳a[i]个石子, ...
- hdu 4790 Just Random
思路:对于a<=x<=b,c<=y<=d,满足条件的结果为ans=f(b,d)-f(b,c-1)-f(a-1,d)+f(a-1,c-1). 而函数f(a,b)是计算0<= ...