Skip to content

Commit d62dc89

Browse files
committed
feat: initial commit
1 parent e2bb057 commit d62dc89

File tree

7 files changed

+5532
-0
lines changed

7 files changed

+5532
-0
lines changed

.editorconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
indent_size = 2
6+
indent_style = space
7+
end_of_line = lf
8+
insert_final_newline = false
9+
trim_trailing_whitespace = true

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
dist

.travis.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
language: node_js
2+
3+
node_js:
4+
- '8'
5+
6+
script:
7+
- yarn lint
8+
- yarn test
9+
- yarn dist
10+
11+
deploy:
12+
provider: script
13+
script: npm run semantic-release
14+
skip_cleanup: true
15+
on:
16+
branch: master
17+
18+
branches:
19+
except:
20+
- /^v\d+\.\d+\.\d+$/

package.json

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{
2+
"name": "redux-serial-queue-middleware",
3+
"version": "0.0.0-development",
4+
"description": "Redux serial queue middleware",
5+
"main": "dist/index.js",
6+
"source": "src/index.js",
7+
"scripts": {
8+
"dist": "babel src --out-dir dist --ignore **.test.js",
9+
"lint": "standard",
10+
"test": "jest",
11+
"test:watch": "jest --watch",
12+
"semantic-release": "semantic-release pre && npm publish && semantic-release post"
13+
},
14+
"author": "kirkov",
15+
"license": "MIT",
16+
"devDependencies": {
17+
"@babel/cli": "^7.0.0-beta.41",
18+
"@babel/core": "^7.0.0-beta.41",
19+
"@babel/preset-env": "^7.0.0-beta.41",
20+
"@babel/preset-stage-3": "^7.0.0-beta.41",
21+
"babel-core": "^7.0.0-0",
22+
"babel-jest": "^22.4.1",
23+
"jest": "^22.4.2",
24+
"redux": "^3.7.2",
25+
"semantic-release": "^15.1.5",
26+
"standard": "^11.0.0"
27+
},
28+
"peerDependencies": {
29+
"redux": "^3.0.0"
30+
},
31+
"babel": {
32+
"presets": [
33+
"@babel/preset-env",
34+
"@babel/preset-stage-3"
35+
]
36+
},
37+
"jest": {
38+
"roots": [
39+
"src"
40+
]
41+
},
42+
"repository": {
43+
"type": "git",
44+
"url": "https://github.com/kirkov/redux-serial-queue-middleware.git"
45+
}
46+
}

src/index.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
export default function serialQueueMiddleware () {
2+
const queues = {}
3+
4+
return () => next => action => {
5+
if (
6+
typeof action.payload !== 'function' ||
7+
typeof action.meta !== 'object' ||
8+
typeof action.meta.queue !== 'string') {
9+
return next(action)
10+
}
11+
12+
const queueName = action.meta.queue
13+
const lastPromise = queues[queueName] || Promise.resolve()
14+
const cb = () => {
15+
const promise = action.payload()
16+
17+
next({
18+
...action,
19+
payload: promise
20+
})
21+
return promise
22+
}
23+
24+
queues[queueName] = lastPromise.then(cb, cb)
25+
}
26+
}

src/index.test.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/* eslint-env jest */
2+
import serialQueueMiddleware from './'
3+
4+
const task = (result, delay = 1) => new Promise(
5+
resolve => setTimeout(() => resolve(result), delay)
6+
)
7+
8+
describe('promiseQueue', () => {
9+
it('skips unrelated actions.', () => {
10+
const promiseQueue = serialQueueMiddleware()
11+
const next = jest.fn(a => a)
12+
const action = {
13+
type: 'TEST',
14+
payload: jest.fn()
15+
}
16+
17+
promiseQueue()(next)(action)
18+
19+
expect(next).toHaveBeenCalledWith(action)
20+
})
21+
22+
it('calls next with the promise from the payload function.', async () => {
23+
const promiseQueue = serialQueueMiddleware()
24+
const next = jest.fn(a => a)
25+
26+
const promise = task('Done!')
27+
const payloadFn = jest.fn(_ => promise)
28+
29+
promiseQueue()(next)({
30+
type: 'TEST',
31+
payload: payloadFn,
32+
meta: {
33+
queue: 'TEST'
34+
}
35+
})
36+
37+
await promise
38+
39+
expect(next).toBeCalledWith({
40+
type: 'TEST',
41+
payload: promise,
42+
meta: {
43+
queue: 'TEST'
44+
}
45+
})
46+
})
47+
})

0 commit comments

Comments
 (0)