Skip to content

Commit 9c52358

Browse files
committed
fix: improve observing state
1 parent 356a273 commit 9c52358

File tree

2 files changed

+49
-31
lines changed

2 files changed

+49
-31
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,13 @@
3535
},
3636
"homepage": "https://github.com/almin/almin-devtools",
3737
"devDependencies": {
38+
"almin": "^0.11.0",
3839
"babel-cli": "^6.23.0",
3940
"babel-preset-jsdoc-to-assert": "^4.0.0",
4041
"babel-preset-latest": "^6.22.0",
4142
"babel-preset-power-assert": "^1.0.0",
4243
"babel-register": "^6.23.0",
43-
"cross-env": "^3.2.3",
44+
"cross-env": "^4.0.0",
4445
"mocha": "^3.2.0",
4546
"power-assert": "^1.4.2"
4647
}

src/almin-devtools.js

Lines changed: 47 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,52 @@ const withDevTools = (
99
typeof window !== 'undefined' && window.devToolsExtension
1010
);
1111

12+
/**
13+
* This connect from Almin's context to DevTools
14+
*
15+
* - If the state is changed by dispatching
16+
* - When anyone payload is dispatched and the UseCase did executed, send to devTools.
17+
* - When anyone payload is dispatched and the state is changed, send to devTools
18+
* - If the state is changed by UseCase
19+
* - When anyone UseCase is completed, send to devTools
20+
*
21+
* @param {Context} alminContext
22+
* @param {*} devTools
23+
*/
24+
const contextToDevTools = (alminContext, devTools) => {
25+
/**
26+
* @type {Object[]}
27+
*/
28+
let currentDispatching = [];
29+
const sendDispatched = () => {
30+
if (currentDispatching.length > 0) {
31+
const currentState = alminContext.getState();
32+
currentDispatching.forEach(payload => {
33+
devTools.send(payload.type, currentState);
34+
});
35+
currentDispatching = [];
36+
}
37+
};
38+
alminContext.onDispatch((payload, meta) => {
39+
currentDispatching.push(payload);
40+
});
41+
alminContext.onChange(() => {
42+
sendDispatched()
43+
});
44+
alminContext.onDidExecuteEachUseCase(() => {
45+
sendDispatched();
46+
});
47+
alminContext.onCompleteEachUseCase((payload, meta) => {
48+
requestAnimationFrame(() => {
49+
devTools.send(`UseCase:${meta.useCase.name}`, alminContext.getState());
50+
});
51+
});
52+
alminContext.onErrorDispatch((payload) => {
53+
devTools.error(payload.error.message);
54+
});
55+
}
56+
57+
1258
const DefaultDevToolsOptions = {
1359
features: {
1460
pause: true, // start/pause recording of dispatched actions
@@ -43,36 +89,7 @@ module.exports = class AlminDevTools {
4389
return;
4490
}
4591
this.devTools = window.devToolsExtension.connect(options);
46-
/*
47-
will
48-
execute
49-
did
50-
complete
51-
onChange store
52-
log
53-
54-
*/
55-
let currentDispatching = [];
56-
this.alminContext.onDispatch((payload, meta) => {
57-
currentDispatching.push(payload);
58-
});
59-
this.alminContext.onDidExecuteEachUseCase((payload, meta) => {
60-
if (currentDispatching.length > 0) {
61-
const currentState = this.alminContext.getState();
62-
currentDispatching.forEach(payload => {
63-
this.devTools.send(payload.type, currentState);
64-
});
65-
}
66-
currentDispatching = [];
67-
});
68-
this.alminContext.onCompleteEachUseCase((payload, meta) => {
69-
requestAnimationFrame(() => {
70-
this.devTools.send(`UseCase:${meta.useCase.name}`, this.alminContext.getState());
71-
});
72-
});
73-
this.alminContext.onErrorDispatch((payload, meta) => {
74-
this.devTools.error(payload.error.message);
75-
});
92+
contextToDevTools(this.alminContext, this.devTools);
7693
}
7794

7895
/**

0 commit comments

Comments
 (0)