• Stars
    star
    182
  • Rank 211,154 (Top 5 %)
  • Language
    JavaScript
  • Created over 6 years ago
  • Updated over 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

🕺🕺Redux middleware for action side effects with callbag 👉< 1KB

npm version Build Status

Redux middleware for action side effects with callbag

You may not need redux-saga/redux-observable. When you use redux-callbag, you will find that this's what you want.

  • 🙀 Minisize
  • 🙀 Scalable
  • 🙀 Easy to understand

Install

npm install --save  redux-callbag

Try it online

Usage

import { createStore, applyMiddleware } from "redux"
import { pipe, filter, forEach, map } from "callbag-basics"
import createCallbagMiddleware from "./index"
import delay from 'callbag-delay'

const  todos = (state = [], action)=> {
    switch (action.type) {
        case "ADD_TODO":
            return state.concat([action.payload])
        case "REMOVE_TODO":
            return []
        case "ADD_SOMETHING":
            return state.concat([action.payload])
        default:
            return state
    }
}

const addTodo = (payload)=> {
    return {
        type: "ADD_TODO",
        payload
    }
}

const addSomething = (payload)=> {
    return {
        type: "ADD_SOMETHING",
        payload
    }
}

const removeTodo = ()=> {
    return {
        type: "REMOVE_TODO"
    }
}



const store = createStore(
    todos,
    ["Hello world"],
    applyMiddleware(
        createCallbagMiddleware((actions, store) => {
            const {
                select,
                mapPromise,
                mapSuccessTo,
                mapFailTo
            } = actions
            
            actions
                |> select("ADD_SOMETHING")
                |> delay(1000)
                |> forEach(({ payload }) => {
                    console.log("log:" + payload)
                })

            actions
                |> select("ADD_TODO")
                |> delay(1000)
                |> mapPromise((d)=>fetch('/xxxx',{data:d}).then(res=>res.json()))
                |> mapSuccessTo("ADD_SOMETHING",(payload)=>payload + "  23333333")
        })
    )
)

store.dispatch(addTodo("Hello redux"))
store.dispatch(addSomething("This will not add numbers"))

console.log(store.getState())

store.subscribe(()=>{
    console.log(store.getState())
})

API

createCallbagMiddleware([...epics : Array<(actions : Function,store : Object) {} :any>]) : Function

This API is used to create middleware

actions.select([...actionType : String]) : Function

This API is used to select action

If action-type is undefined, it is equivalent to select to initialize the action, as you can pass multiple action-types

actions.mapPromise(mapFn : Function<(payload : any) {} : any>) : Function

This API is used to insert promise flow

actions.mapSuccessTo(actionType : String , [mapFn : Function<(payload : any) {} : any])

This API is used to dispatch action when callbags chain is successed

actions.mapFailTo(actionType : String , [mapFn : Function<(payload : any) {} : any])

This API is used to dispatch action when callbags chain is failed

Q/A

How to use pipline operator syntax?

You can install the latest version of babel7(@babel/cli), and use @babel/plugin-proposal-pipeline-operator.

LICENSE

The MIT License (MIT)

Copyright (c) 2018 JanryWang

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

More Repositories

1

react-study

React源码分析,需要进一步了解请移步https://github.com/purplebamboo/little-reactjs
JavaScript
173
star
2

react-eva

Effects+View+Actions(React distributed state management solution with rxjs.)
JavaScript
156
star
3

doc-scripts

React Document Build Tool Scripts , Like react-scripts of create react app
JavaScript
155
star
4

depath

Path Matcher/Getter/Setter for Object/Array
TypeScript
132
star
5

mfetch

mfetch will provide you with a strong ability to request resource management
JavaScript
93
star
6

awesome-callbags

Callbag Libraries & Learning Material https://github.com/callbag/callbag
86
star
7

callbag-map-promise

Callbag map promise
JavaScript
65
star
8

callbag-delay

callbag delay
JavaScript
60
star
9

immutability-skinable-component

immutability-skinable-component
JavaScript
48
star
10

exp-parser

An expression parser like the angular's parser
JavaScript
41
star
11

kvm

KVM.js 轻量级模块管理器,依赖注入,AMD,Commonjs规范,Promise化
JavaScript
41
star
12

react-propers

Select react doms , and update props.
JavaScript
40
star
13

gub

CLI tool for create an npm package from any repos. 🐳
JavaScript
33
star
14

dot-match

Extremely easy dot path matching tool
JavaScript
29
star
15

qverse

Traverse any data with DPML commands.
JavaScript
25
star
16

immutability

Improve the react components immutability by es6 decorate
JavaScript
14
star
17

react-chrome-boilerplate

Improve your development efficiency to create chrome extension.
JavaScript
9
star
18

github-tag-release

github release by git tags
TypeScript
9
star
19

react-banners

React通用横幅组件
JavaScript
8
star
20

react-snap-svg

react snap.svg
JavaScript
5
star
21

react-stikky

It's very easy to sticky fixed target element.
JavaScript
5
star
22

react-slot-props

slot slot slot , react slot!
JavaScript
5
star
23

react-code-snippet

React Code Snippet
JavaScript
4
star
24

react-site-renderer

JavaScript
3
star
25

awesome-react-suspense

Awesome list of React components with suspense and resources.
3
star
26

react-demo-loader

webpack loader for react demo
JavaScript
2
star
27

dom-imagify

Generates an image from a DOM node using HTML5 canvas
JavaScript
2
star
28

use-element-rect-observer

TypeScript
2
star
29

get-target-cookie

TypeScript
1
star
30

react-doc-renderer

JavaScript
1
star
31

super-plugin

JavaScript
1
star
32

janry.blog.io

1
star
33

clean-ns

Clean Node Modules
JavaScript
1
star
34

janryWang

Config files for my GitHub profile.
1
star
35

deproxy

Downgradable ES Proxy solution
1
star