Skip to content

Commit

Permalink
feat: initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
kirkov committed Apr 4, 2018
1 parent e2bb057 commit d62dc89
Show file tree
Hide file tree
Showing 7 changed files with 5,532 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*]
charset = utf-8
indent_size = 2
indent_style = space
end_of_line = lf
insert_final_newline = false
trim_trailing_whitespace = true
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
dist
20 changes: 20 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
language: node_js

node_js:
- '8'

script:
- yarn lint
- yarn test
- yarn dist

deploy:
provider: script
script: npm run semantic-release
skip_cleanup: true
on:
branch: master

branches:
except:
- /^v\d+\.\d+\.\d+$/
46 changes: 46 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"name": "redux-serial-queue-middleware",
"version": "0.0.0-development",
"description": "Redux serial queue middleware",
"main": "dist/index.js",
"source": "src/index.js",
"scripts": {
"dist": "babel src --out-dir dist --ignore **.test.js",
"lint": "standard",
"test": "jest",
"test:watch": "jest --watch",
"semantic-release": "semantic-release pre && npm publish && semantic-release post"
},
"author": "kirkov",
"license": "MIT",
"devDependencies": {
"@babel/cli": "^7.0.0-beta.41",
"@babel/core": "^7.0.0-beta.41",
"@babel/preset-env": "^7.0.0-beta.41",
"@babel/preset-stage-3": "^7.0.0-beta.41",
"babel-core": "^7.0.0-0",
"babel-jest": "^22.4.1",
"jest": "^22.4.2",
"redux": "^3.7.2",
"semantic-release": "^15.1.5",
"standard": "^11.0.0"
},
"peerDependencies": {
"redux": "^3.0.0"
},
"babel": {
"presets": [
"@babel/preset-env",
"@babel/preset-stage-3"
]
},
"jest": {
"roots": [
"src"
]
},
"repository": {
"type": "git",
"url": "https://github.com/kirkov/redux-serial-queue-middleware.git"
}
}
26 changes: 26 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export default function serialQueueMiddleware () {
const queues = {}

return () => next => action => {
if (
typeof action.payload !== 'function' ||
typeof action.meta !== 'object' ||
typeof action.meta.queue !== 'string') {
return next(action)
}

const queueName = action.meta.queue
const lastPromise = queues[queueName] || Promise.resolve()
const cb = () => {
const promise = action.payload()

next({
...action,
payload: promise
})
return promise
}

queues[queueName] = lastPromise.then(cb, cb)
}
}
47 changes: 47 additions & 0 deletions src/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/* eslint-env jest */
import serialQueueMiddleware from './'

const task = (result, delay = 1) => new Promise(
resolve => setTimeout(() => resolve(result), delay)
)

describe('promiseQueue', () => {
it('skips unrelated actions.', () => {
const promiseQueue = serialQueueMiddleware()
const next = jest.fn(a => a)
const action = {
type: 'TEST',
payload: jest.fn()
}

promiseQueue()(next)(action)

expect(next).toHaveBeenCalledWith(action)
})

it('calls next with the promise from the payload function.', async () => {
const promiseQueue = serialQueueMiddleware()
const next = jest.fn(a => a)

const promise = task('Done!')
const payloadFn = jest.fn(_ => promise)

promiseQueue()(next)({
type: 'TEST',
payload: payloadFn,
meta: {
queue: 'TEST'
}
})

await promise

expect(next).toBeCalledWith({
type: 'TEST',
payload: promise,
meta: {
queue: 'TEST'
}
})
})
})
Loading

0 comments on commit d62dc89

Please sign in to comment.