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. 初拾Java(问题三:乱码问题)

    年后的工作不是那么的忙,最近也开始思考自己以后的路该怎么走,在迷茫的时候,还是坚持学习点儿东西吧. 接着之前的JSP,这次是要尝试着用request.getParameter(ParameterNam ...

  2. Hibernate 多对一注解

    在前面学习了基于配置文件的多对一关系,而在实际的开发过程中我们更多的是使用注解去开发.在这里来简单学习一下基于注解的多对一关系. 1. 创建所需要的实体 注:这里需要特别注意的是,如果使用的是mysq ...

  3. hibernate4日志配置

    在平时运行程序时控制台有很对的日志打印,本文说明如何控制这些日志打印. 1.确定要使用日志的实现,我在这使用的是log4j. 2.加入log4j的api包slf4j-api.jar,log4j的转换包 ...

  4. 【51Nod 1815】【算法马拉松 23】调查任务

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1815 tarjan缩点后在DAG上递推即可. 每个点维护所有根到它的路径 ...

  5. [POJ2749]Building roads(2-SAT)

    Building roads Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8153   Accepted: 2772 De ...

  6. [BZOJ4890][TJOI2017]城市(DP)

    题目描述 从加里敦大学城市规划专业毕业的小明来到了一个地区城市规划局工作.这个地区一共有ri座城市,<-1条高速公路,保证了任意两运城市之间都可以通过高速公路相互可达,但是通过一条高速公路需要收 ...

  7. 【20181030T1】排列树【树形结构+组合数】

    题面 [正解] 一眼找规律 --**这东西有啥规律啊 哎好像根节点是最小的 好像可以组合数分配子树大小,子树内部-- 可以递归啊! 乱搞一顿就好啦 过了大样例 复杂度\(O(N)\) 切完T2后T3水 ...

  8. OpenSSL Heartbleed “心脏滴血”漏洞简单攻击示例

    OpenSSL Heartbleed漏洞的公开和流行让许多人兴奋了一把,也让另一些人惊慌了一把. 单纯从攻击的角度讲,我已知道的,网上公开的扫描工具有: 1.  Nmap脚本ssl-heartblee ...

  9. Java反射机制涉及的类常见方法使用总结

    import java.lang.reflect.Constructor; import java.lang.reflect.*; /*Class:代表一个字节码文件的对象,每当有类被加载进内存,JV ...

  10. 《python学习手册》第32章 异常基础

    发生异常与默认的异常处理   当发生异常的时候,我们代码没有刻意捕获这个异常,所以它会一直向上返回到程序顶层,并启用默认的异常处理器:打印标准出错信息.而且会终止程序.   执行下面程序 def fu ...