Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
flintinatux committed Jul 13, 2018
0 parents commit 23ff50d
Show file tree
Hide file tree
Showing 10 changed files with 2,719 additions and 0 deletions.
18 changes: 18 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module.exports = {
'env': {
'es6': true,
'mocha': true,
'node': true
},
'extends': 'eslint:recommended',
'parserOptions': {
'sourceType': 'module'
},
'rules': {
'indent': ['error', 2, { 'SwitchCase': 1 }],
'linebreak-style': ['error', 'unix'],
'no-console': 'off',
'quotes': ['error', 'single', { 'allowTemplateLiterals': true }],
'semi': ['error', 'never']
}
}
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
*.log
.DS_Store
.yarn*
/.nyc_output
/node_modules
11 changes: 11 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
*.log
*.md
.DS_Store
.eslintrc.js
.gitignore
.travis.yml
.yarn*
/.nyc_output
/node_modules
test.js
yarn.lock
12 changes: 12 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
language: node_js
node_js:
- '10'
- '8'
- '7'
- '6'
before_install:
- npm install -g yarn
install:
- yarn install --pure-lockfile
script:
- yarn run test:ci
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
The MIT License (MIT)

Copyright 2018 Articulate Global, Inc

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.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# redux-future2
[![npm version](https://img.shields.io/npm/v/redux-future2.svg)](https://www.npmjs.com/package/redux-future2)
[![npm downloads](https://img.shields.io/npm/dm/redux-future2.svg)](https://www.npmjs.com/package/redux-future2)
[![Build Status](https://travis-ci.org/articulate/redux-future2.svg?branch=master)](https://travis-ci.org/articulate/redux-future2)
[![Coverage Status](https://coveralls.io/repos/github/articulate/redux-future2/badge.svg?branch=master)](https://coveralls.io/github/articulate/redux-future2?branch=master)
[![NSP Status](https://nodesecurity.io/orgs/articulate/projects/94556299-8383-47c3-83a6-1add64143199/badge)](https://nodesecurity.io/orgs/articulate/projects/94556299-8383-47c3-83a6-1add64143199)

Future middleware for redux.

### Usage

```haskell
future : Store -> Function -> Action -> a
```

Redux middleware to dispatch actions that are Futures, also known as Asyncs or Tasks. Popular libraries providing Future implementations include [`crocks`](https://github.com/evilsoft/crocks) and [`Fluture`](https://github.com/fluture-js/Fluture).

If any action has a property called `fork` that is a function, the action is assumed to be a Future.

```js
const { applyMiddleware, combineReducers, createStore } = require('redux')
const future = require('redux-future2')

const reducers = require('../ducks')

const store = createStore(combineReducers(reducers), applyMiddleware(future))
```

14 changes: 14 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// future : Store -> Function -> Action -> a
function future(store) {
return function(next) {
return function(action) {
if (action && typeof action.fork === 'function') {
action.fork(store.dispatch, store.dispatch)
} else {
next(action)
}
}
}
}

module.exports = future
44 changes: 44 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"name": "redux-future2",
"version": "0.0.0",
"description": "Future middleware for redux",
"main": "index.js",
"repository": "git@github.com:articulate/redux-future2.git",
"author": "articulate",
"license": "MIT",
"keywords": [
"async",
"future",
"middleware",
"monad",
"promise",
"redux",
"redux-middleware",
"task"
],
"nyc": {
"check-coverage": true,
"branches": 100,
"functions": 100,
"lines": 100,
"statements": 100
},
"scripts": {
"coverage": "nyc report --reporter=text-lcov | coveralls",
"lint": "eslint .",
"postversion": "git push --tags origin master",
"preversion": "git checkout master",
"test": "mocha test.js --reporter=dot",
"test:ci": "yarn run lint && yarn run test:coverage && yarn run coverage",
"test:coverage": "nyc yarn run test"
},
"devDependencies": {
"@articulate/spy": "^0.0.1",
"chai": "^4.1.2",
"coveralls": "^3.0.2",
"crocks": "^0.9.4",
"eslint": "^5.1.0",
"mocha": "^5.2.0",
"nyc": "^12.0.2"
}
}
62 changes: 62 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
const { Async } = require('crocks')
const { expect } = require('chai')
const spy = require('@articulate/spy')

const future = require('.')

describe('future (redux middleware)', () => {
const axn = { type: 'TYPE', payload: null }
const left = Async.Rejected(axn)
const right = Async.Resolved(axn)
const next = spy()
const store = { dispatch: spy() }

afterEach(() => {
next.reset()
store.dispatch.reset()
})

describe('when action is a rejected future', () => {
beforeEach(() =>
future(store)(next)(left)
)

it('forks the future and dispatches the rejected value', () => {
expect(store.dispatch.calls.length).to.equal(1)
expect(store.dispatch.calls[0]).to.eql([axn])
})

it('does not call the next middleware', () =>
expect(next.calls.length).to.equal(0)
)
})

describe('when action is a resolved future', () => {
beforeEach(() =>
future(store)(next)(right)
)

it('forks the future and dispatches the resolved value', () => {
expect(store.dispatch.calls.length).to.equal(1)
expect(store.dispatch.calls[0]).to.eql([axn])
})

it('does not call the next middleware', () =>
expect(next.calls.length).to.equal(0)
)
})

describe('when action is not a future', () => {
beforeEach(() =>
future(store)(next)(axn)
)

it('does not fork anything to dispatch values', () =>
expect(store.dispatch.calls.length).to.equal(0)
)

it('calls the next middleware with the action', () =>
expect(next.calls[0]).to.eql([axn])
)
})
})
Loading

0 comments on commit 23ff50d

Please sign in to comment.