1 | const createStoreImpl = (createState) => {
|
2 | let state;
|
3 | const listeners = new Set();
|
4 | const setState = (partial, replace) => {
|
5 | const nextState = typeof partial === "function" ? partial(state) : partial;
|
6 | if (!Object.is(nextState, state)) {
|
7 | const previousState = state;
|
8 | state = (replace != null ? replace : typeof nextState !== "object" || nextState === null) ? nextState : Object.assign({}, state, nextState);
|
9 | listeners.forEach((listener) => listener(state, previousState));
|
10 | }
|
11 | };
|
12 | const getState = () => state;
|
13 | const getInitialState = () => initialState;
|
14 | const subscribe = (listener) => {
|
15 | listeners.add(listener);
|
16 | return () => listeners.delete(listener);
|
17 | };
|
18 | const api = { setState, getState, getInitialState, subscribe };
|
19 | const initialState = state = createState(setState, getState, api);
|
20 | return api;
|
21 | };
|
22 | const createStore = (createState) => createState ? createStoreImpl(createState) : createStoreImpl;
|
23 |
|
24 | export { createStore };
|