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的更多相关文章

  1. [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 ...

  2. [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 ...

  3. [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 ...

  4. 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 ...

  5. 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 ...

  6. redux middleware 源码分析

    原文链接 middleware 的由来 在业务中需要打印每一个 action 信息来调试,又或者希望 dispatch 或 reducer 拥有异步请求的功能.面对这些场景时,一个个修改 dispat ...

  7. 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 ...

  8. Monad (functional programming)

    In functional programming, a monad is a design pattern that defines how functions, actions, inputs, ...

  9. Beginning Scala study note(4) Functional Programming in Scala

    1. Functional programming treats computation as the evaluation of mathematical and avoids state and ...

随机推荐

  1. hdu 1180 诡异的楼梯(优先队列)

    Hogwarts正式开学以后,Harry发现在Hogwarts里,某些楼梯并不是静止不动的,相反,他们每隔一分钟就变动一次方向.  比如下面的例子里,一开始楼梯在竖直方向,一分钟以后它移动到了水平方向 ...

  2. HDU 6205[计算几何,JAVA]

    题目链接[http://acm.hdu.edu.cn/showproblem.php?pid=6206] 题意: 给出不共线的三个点,和一个点(x,y),然后判断(x,y)在不在这三个点组成的圆外. ...

  3. [SDOI2014]数数 --- AC自动机 + 数位DP

    [SDOI2014]数数 题目描述: 我们称一个正整数N是幸运数,当且仅当它的十进制表示中不包含数字串集合S中任意一个元素作为其子串. 例如当S=(22,333,0233)时,233是幸运数,2333 ...

  4. 【并查集】BZOJ4668-冷战

    [题目大意] 给出N个军工厂和M 个操作,操作分为两类: • 0 u v,这次操作苏联会修建一条连接 u 号军工厂及 v 号军工厂的铁路,注意铁路都是双向的; • 1 u v, Reddington ...

  5. hdu 3642 体积并

    题意:求三个矩形体积的并 链接:点我 枚举z #include<stdio.h> #include<iostream> #include<stdlib.h> #in ...

  6. asp.net调用存储过程2

    创建一个只有输入参数的存储过程 create procedure proc_user@name varchar(20),@Password varchar(100)as select * from l ...

  7. hadoop招聘需求每天都在添加,短短半个月时间,需求量差点儿翻了一番,这是大数据要爆发的节奏么?

    近期常常关注企业hadoop招聘需求的动态变化,多说无益,直接上几张百度的截图: 4月20日: 4月22日: 4月27日: 5月8日:

  8. Circuit provides reference for multiple ADCs

    The achievable accuracy for systems with multiple ADCs depends directly on the reference voltages ap ...

  9. WebService如何抛出干净的异常

    转载:http://www.cnblogs.com/ahdung/p/3953431.html 说明:[干净]指的是客户端在捕获WebService(下称WS)抛出的异常时,得到的ex.Message ...

  10. Rob Pike 的 5 个编程原则

    原则 1. 你没有办法预测每个程序的运行时间,瓶颈会出现在出乎意料的地方,所以在分析瓶颈原因之前,先不要盲目猜测. 原则 2. 测试(measure).在测试之前不要优化程序,即使在测试之后也要慎重, ...