-
Notifications
You must be signed in to change notification settings - Fork 1
/
redux.js
90 lines (79 loc) · 1.83 KB
/
redux.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
'use strict';
module.exports = {
createStore: createStore,
applyMiddleware: applyMiddleware,
bindActionCreator: bindActionCreator,
compose: compose
};
function createStore (reducer,initialState) {
var currentState = initialState;
var listeners = [];
function dispatch (action) {
currentState = reducer(currentState,action);
listeners.slice().forEach(l => l());
}
function getState () {
return currentState;
}
function subscribe (listener) {
listeners.push(listener);
}
return {
dispatch: dispatch,
getState: getState,
subscribe: subscribe
};
}
function applyMiddleware (middleware) {
return function (createStore) {
return function createStoreSubstitute (reducer, initialState) {
var store = createStore(reducer, initialState);
return {
getState: store.getState,
dispatch: middleware(store)(store.dispatch),
subscribe: store.subscribe,
};
};
};
}
function bindActionCreator (actionCreators, dispatch) {
return function () { dispatch(actionCreators()); };
}
/**
* Compose function
*
function add_one (n) {
const total = n + 1;
return total;
}
function less_one (n) {
const total = n - 1;
return total;
}
function multiply_two (n) {
const total = n * 2;
return total;
}
function start (n) {
return parseInt(n);
}
const parseAddMultiplyLess = compose(
less_one,
multiply_two,
add_one,
start
);
parseAddMultiplyLess('1'); // 3
*
*/
function compose () {
const outer = ([]).slice.call(arguments,0);
return function () {
const inner = ([]).slice.call(arguments,0);
const last_func = outer[outer.length - 1];
const rest_func = outer.slice(0, -1);
return rest_func.reduceRight((composed, f) => {
return f(composed);
}, last_func.apply(undefined,inner));
};
}