-
Notifications
You must be signed in to change notification settings - Fork 1
/
redux-service.js
45 lines (35 loc) · 915 Bytes
/
redux-service.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
'use strict';
var redux = require('./redux.js');
function createService (store, services) {
services.forEach(function (service) {
service.initialize(store.getState(),store.dispatch);
});
store.subscribe(function () {
services.forEach(function (service) {
service.update(store.getState(),store.dispatch);
});
});
}
function actionCreator () {
return {type:'hello'};
}
function reducer (state, action) {
if (action.type === 'hello') return 'one';
return 'bye';
}
var store = redux.createStore(reducer,{});
var simpleService = {
initialize: function (state,dispatch) {
console.log('service:initialize');
},
update: function (state,dispatch) {
console.log('service:update',state);
if (state === 'one') {
dispatch({type:'bye'});
}
}
};
createService(store,[simpleService]);
// service:initialize
store.dispatch(actionCreator());
// service:update