-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
5,532 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules | ||
dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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+$/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
} | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.