Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat(redux): dispatcher with middleware#97

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
azu merged 4 commits intomasterfromredux-player
May 15, 2016
Merged
Show file tree
Hide file tree
Changes fromall 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
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionsREADME.md
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionpackage.json
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionssrc/redux/Dispatcher.js
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionssrc/redux/apply-middleware.js
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionstest/redux/apply-middleware-test.js
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletiontest/redux/timestamp-test.js
View file
Open in desktop
Original file line numberDiff line numberDiff 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

[8]ページ先頭

©2009-2025 Movatter.jp