Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .textlintrc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"no-mix-dearu-desumasu": {
"preferInHeader": "",
"preferInBody": "ですます",
"preferInList": "である"
"preferInList": "である",
"strict": true
},
"prh": {
"rulePaths": [
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,4 @@ Pull Request、コミットのやりかたなどが書かれています。

コードはMITライセンスで利用できます。
文章は[CC BY-NC 4.0](http://creativecommons.org/licenses/by-nc/4.0/ "CC BY-NC 4.0")で利用できます。

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
"textlint-rule-no-doubled-conjunctive-particle-ga": "^1.0.2",
"textlint-rule-no-doubled-joshi": "^3.2.0",
"textlint-rule-no-dropping-the-ra": "^1.0.2",
"textlint-rule-no-mix-dearu-desumasu": "^2.2.1",
"textlint-rule-no-mix-dearu-desumasu": "^3.0.0",
"textlint-rule-no-nfd": "^1.0.1",
"textlint-rule-no-start-duplicated-conjunction": "^1.0.7",
"textlint-rule-preset-jtf-style": "^2.0.0",
Expand Down
36 changes: 36 additions & 0 deletions src/redux/Dispatcher.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// LICENSE : MIT
"use strict";
const assert = require("assert");
const EventEmitter = require("events");
export const ON_DISPATCH = "__ON_DISPATCH__";
/**
* payload The payload object that must have `type` property.
* @typedef {Object} DispatcherPayload
* @property {String} type The event type to dispatch.
* @public
*/
export default class Dispatcher extends EventEmitter {
/**
* add onAction handler and return unbind function
* @param {function(DispatcherPayload)} payloadHandler
* @returns {Function} call the function and release handler
* @public
*/
onDispatch(payloadHandler) {
this.on(ON_DISPATCH, payloadHandler);
return this.removeListener.bind(this, ON_DISPATCH, payloadHandler);
}

/**
* dispatch action object.
* StoreGroups receive this action and reduce state.
* @param {DispatcherPayload} payload
* @public
*/
dispatch(payload) {
assert(payload !== undefined && payload !== null, "payload should not null or undefined");
assert(typeof payload.type === "string", "payload's type should be string");
this.emit(ON_DISPATCH, payload);
}

}
22 changes: 22 additions & 0 deletions src/redux/apply-middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// LICENSE : MIT
"use strict";
// core -
// next - next function
// action - action object
const applyMiddlewares = (...middlewares) => {
return middlewareAPI => {
const originalDispatch = middlewareAPI.dispatch.bind(middlewareAPI);
const wrapMiddleware = middlewares.map(middleware => {
return middleware(middlewareAPI);
});
// apply middleware order by first
const last = wrapMiddleware[wrapMiddleware.length - 1];
const rest = wrapMiddleware.slice(0, -1);
const roundDispatch = rest.reduceRight((oneMiddle, middleware) => {
return middleware(oneMiddle);
}, last);
return roundDispatch(originalDispatch);
};
};

export default applyMiddlewares;
54 changes: 54 additions & 0 deletions test/redux/apply-middleware-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// LICENSE : MIT
"use strict";
const assert = require("power-assert");
import applyMiddleware from "../../src/redux/apply-middleware";
import Dispatcher from "../../src/redux/Dispatcher";
import timestamp from "../../src/redux/timestamp";
import createLogger from "../../src/redux/logger";
describe("middleware", function () {
it("could apply logger middleware", function () {
const dispatcher = new Dispatcher();
const logs = [];
const con = {
log(message){
logs.push(message);
}
};
const middleware = createLogger({
logger: con
});
// state
const state = {};
const middlewareAPI = {
getState(){
return state;
},
dispatch(action){
dispatcher.dispatch(action);
}
};
const dispatchWithMiddleware = applyMiddleware(middleware)(middlewareAPI);
const action = {type: "FOO"};
// then
dispatcher.onDispatch(payload => {
if(payload.type === "FOO"){
state.isUpdated = true;
}
});
// when
dispatchWithMiddleware(action);
assert.deepEqual(logs, [action, state]);
});
it("could apply timestamp middleware", function (done) {
const dispatcher = new Dispatcher();
const dispatchWithMiddleware = applyMiddleware(timestamp)(dispatcher);
const action = {type: "FOO"};
// then
dispatcher.onDispatch(payload => {
assert(typeof payload.timeStamp === "number");
done();
});
// when
dispatchWithMiddleware(action);
});
});
2 changes: 1 addition & 1 deletion test/redux/timestamp-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const reducer = (state = initialState, action) => {
}
};

describe("logger-test", function () {
describe("timestamp-test", function () {
let store, dispatch;
beforeEach(() => {
store = applyMiddleware(timestamp)(createStore)(reducer);
Expand Down