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. spring配置多数据源——mybatis

    这篇文章是配置mybatis多数据源文章,如果是hibernate的话也是没什么影响,配置都是差不多的. 在这家公司上班差不多一星期了,不小心点开配置文件一看这项目配置了两个数据源,蒙了. 之后上网查 ...

  2. JS中关于换行的2个坑

    //第一个为ul/ol使用中的li使用需要注意的地方 1.<ul id="banner_indicator"><li></li><li&g ...

  3. Java 8中你可能没听过的10个新特性

    lambda表达式,lambda表达式,还是lambda表达式.一提到Java 8就只能听到这个,但这不过是其中的一个新功能而已,Java 8还有许多新的特性——有一些功能强大的新类或者新的用法,还有 ...

  4. 在eclipse中安装TestNG

    https://www.cnblogs.com/baixiaozheng/p/4989856.html 1.可借助Eclipse的Marketplace来安装TestNG Eclipse插件 a.打开 ...

  5. 我的sublime text3 配置文件设置

    { "ignored_packages": [ "Vintage" ], //vim模式 "line_padding_bottom": 2, ...

  6. MySQL笔记(一)之新建数据库和数据表

    创建数据库 CREATE DATABASE database_name 创建数据表 CREATE TABLE table_name ( 列1 数据类型, 列2 数据类型, 列3 数据类型, .... ...

  7. FastReport.Net使用:[28]数据合并

    基础数据 1.拖动数据源中的数据列到报表设计器中,获得一张简单的报表. 2.下面使用两种方法将期中考试和期末考试的成绩合并到一行显示 合并数据(分组方法) 1.按学生名字和科目来进行分组,成绩文本框咱 ...

  8. PHP 笔记——String 字符串

    1. 定义 单引号括起来的字符串被原样输出. 双引号字符串中的变量被PHP解析为变量值. 2. 获取字符串长度 strlen(string $string): int 在utf-8下,汉字占3个字符, ...

  9. SpringBoot返回结果如果为null或空值不显示处理方法

    第一种方法:自定义消息转换器 @Configuration public class WebMvcConfig extends WebMvcConfigurerAdapter{ // /** // * ...

  10. Unity随手机

    该文章持续更新! 协程的返回值必需是 IEnumerator 协程的参数不能加关键字 ref 或 out 在函数 Update 和 FixedUpdate 中不能使用 yield 语句,但可以启动协程 ...