1 | export default function createStore(initialState) {
|
2 | let state = initialState;
|
3 | const listeners = [];
|
4 |
|
5 | function setState(partial) {
|
6 | state = { ...state, ...partial };
|
7 | for (let i = 0; i < listeners.length; i++) {
|
8 | listeners[i]();
|
9 | }
|
10 | }
|
11 |
|
12 | function getState() {
|
13 | return state;
|
14 | }
|
15 |
|
16 | function subscribe(listener) {
|
17 | listeners.push(listener);
|
18 |
|
19 | return function unsubscribe() {
|
20 | const index = listeners.indexOf(listener);
|
21 | listeners.splice(index, 1);
|
22 | };
|
23 | }
|
24 |
|
25 | return {
|
26 | setState,
|
27 | getState,
|
28 | subscribe,
|
29 | };
|
30 | }
|
31 | |
\ | No newline at end of file |