Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from keyanzhang/1.0
Major cleanup for 1.0.0
- Loading branch information
Showing
13 changed files
with
3,657 additions
and
253 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 |
---|---|---|
@@ -1,28 +1,9 @@ | ||
{ | ||
"extends": "airbnb/base", | ||
"extends": "airbnb-base", | ||
"env": { | ||
"browser": true, | ||
"node": true, | ||
"es6": true, | ||
"mocha": true, | ||
"jest": true, | ||
}, | ||
"ecmaFeatures": { | ||
"generators": true, | ||
}, | ||
"parser": "babel-eslint", | ||
"rules": { | ||
"babel/generator-star-spacing": 1, | ||
"babel/new-cap": 1, | ||
"babel/object-shorthand": 1, | ||
"babel/arrow-parens": 1, | ||
"babel/no-await-in-loop": 1, | ||
"array-bracket-spacing": [1, "always", { | ||
"singleValue": true, | ||
"objectsInArrays": false, | ||
"arraysInArrays": true, | ||
}], | ||
}, | ||
"plugins": [ | ||
"babel" | ||
], | ||
} |
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
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,6 @@ | ||
## 1.0.0 (February 19, 2017) | ||
|
||
* Bugfix: pass along all actions to next middleware in chain. ([@jebeck](https://github.com/jebeck) in [#5](https://github.com/keyanzhang/redux-worker-middleware/pull/5)) | ||
* It's now important to let the worker return messages that have the `meta.WebWorker` field removed. Since the returned data will be re-dispatched as a new action and be passed through all the middlewares, keeping the `meta.WebWorker` field may cause an infinite loop. | ||
* Changed from `console.error` to (throw) real errors for incorrect configs ([#6](https://github.com/keyanzhang/redux-worker-middleware/pull/6)) | ||
* Let the middleware grab `dispatch` earlier ([#6](https://github.com/keyanzhang/redux-worker-middleware/pull/6)) |
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
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
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,150 @@ | ||
import createWorkerMiddleware from '..'; | ||
|
||
class Worker { | ||
constructor(fn = x => x) { | ||
this.fn = fn; | ||
|
||
this.postMessage = this.postMessage.bind(this); | ||
this.onmessage = undefined; | ||
} | ||
|
||
postMessage(msg) { | ||
if (this.onmessage) { | ||
setTimeout(() => { | ||
const data = this.fn(msg); | ||
this.onmessage({ data }); | ||
}, 0); | ||
} | ||
} | ||
} | ||
|
||
describe('createWorkerMiddleware', () => { | ||
const actionWithWorker = { | ||
type: 'I_USE_WORKER', | ||
meta: { | ||
WebWorker: true, | ||
}, | ||
payload: { | ||
data: 42, | ||
category: 'life', | ||
}, | ||
}; | ||
|
||
const actionWithoutWorker = { | ||
type: 'I_DONT_USE_WORKER', | ||
payload: { | ||
data: 23, | ||
category: 'number', | ||
}, | ||
}; | ||
|
||
const dispatch = jest.fn(x => x); | ||
|
||
beforeEach(() => { | ||
dispatch.mockClear(); | ||
}); | ||
|
||
it('should throw if `worker` is falsy', () => { | ||
expect(() => { | ||
createWorkerMiddleware(); | ||
}).toThrow( | ||
'`createWorkerMiddleware` expects a worker instance as the argument. Instead received: undefined', | ||
); | ||
}); | ||
|
||
it('should throw if `worker.postMessage` is falsy', () => { | ||
expect(() => { | ||
createWorkerMiddleware({}); | ||
}).toThrow( | ||
'The worker instance is expected to have a `postMessage` method.', | ||
); | ||
}); | ||
|
||
it('the worker should be invoked if the action needs it', (done) => { | ||
const mockWorkerBehavior = jest.fn(); | ||
const middleware = createWorkerMiddleware(new Worker(mockWorkerBehavior)); | ||
|
||
const next = (action) => { | ||
expect(action).toBe(actionWithWorker); | ||
setTimeout(() => { | ||
expect(mockWorkerBehavior).toHaveBeenCalledTimes(1); | ||
expect(mockWorkerBehavior).toHaveBeenCalledWith(actionWithWorker); | ||
done(); | ||
}, 10); | ||
}; | ||
|
||
middleware({ dispatch })(next)(actionWithWorker); | ||
}); | ||
|
||
it('the worker shouldn\'t be invoked if the action doesn\'t need it', (done) => { | ||
const mockWorkerBehavior = jest.fn(); | ||
const middleware = createWorkerMiddleware(new Worker(mockWorkerBehavior)); | ||
|
||
const next = (action) => { | ||
expect(action).toBe(actionWithoutWorker); | ||
setTimeout(() => { | ||
expect(mockWorkerBehavior).toHaveBeenCalledTimes(0); | ||
done(); | ||
}, 10); | ||
}; | ||
|
||
middleware({ dispatch })(next)(actionWithoutWorker); | ||
}); | ||
|
||
it('when the action needs a worker, it should pass along the action with ' + | ||
'`next` so that the next middleware in the chain sees it', (done) => { | ||
const middleware = createWorkerMiddleware(new Worker()); | ||
|
||
const next = (action) => { | ||
expect(action).toBe(actionWithWorker); | ||
setTimeout(() => { | ||
expect(dispatch).toHaveBeenCalledWith(actionWithWorker); | ||
done(); | ||
}, 10); | ||
}; | ||
|
||
middleware({ dispatch })(next)(actionWithWorker); | ||
}); | ||
|
||
it('when the action doesn\'t need a worker, it should also pass along the action with ' + | ||
'`next` so that the next middleware in the chain sees it', (done) => { | ||
const middleware = createWorkerMiddleware(new Worker()); | ||
|
||
const next = (action) => { | ||
expect(action).toBe(actionWithoutWorker); | ||
setTimeout(() => { | ||
expect(dispatch).toHaveBeenCalledTimes(0); | ||
done(); | ||
}, 10); | ||
}; | ||
|
||
middleware({ dispatch })(next)(actionWithoutWorker); | ||
}); | ||
|
||
it('when the action needs a worker, it should still pass along the action with ' + | ||
'`next`, and it should `dispatch` the action returned from the worker', (done) => { | ||
const actionFromWorker = { | ||
type: 'WORKER_RETURN', | ||
payload: { | ||
data: 100, | ||
}, | ||
}; | ||
|
||
const mockWorkerBehavior = jest.fn(() => actionFromWorker); | ||
const middleware = createWorkerMiddleware(new Worker(mockWorkerBehavior)); | ||
|
||
const next = (action) => { | ||
expect(action).toBe(actionWithWorker); | ||
setTimeout(() => { | ||
expect(mockWorkerBehavior).toHaveBeenCalledTimes(1); | ||
expect(mockWorkerBehavior).toHaveBeenCalledWith(actionWithWorker); | ||
|
||
expect(dispatch).toHaveBeenCalledTimes(1); | ||
expect(dispatch).toHaveBeenCalledWith(actionFromWorker); | ||
done(); | ||
}, 10); | ||
}; | ||
|
||
middleware({ dispatch })(next)(actionWithWorker); | ||
}); | ||
}); |
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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.