(
initialState: Partial<S> | S = {},
middleware: any = null
)
| 10 | middleware?: any |
| 11 | ): Store<Partial<S>>; |
| 12 | function createStore<S extends object = any>( |
| 13 | initialState: Partial<S> | S = {}, |
| 14 | middleware: any = null |
| 15 | ): Store<S> | Store<Partial<S>> { |
| 16 | let state: Partial<S> & object = initialState || {}; |
| 17 | const listeners: Function[] = []; |
| 18 | |
| 19 | function dispatchListeners() { |
| 20 | listeners.forEach(f => f(state)); |
| 21 | } |
| 22 | |
| 23 | return { |
| 24 | middleware, |
| 25 | setState(update: ((state: Partial<S>) => Partial<S>) | Partial<S>) { |
| 26 | state = { |
| 27 | ...(state as object), |
| 28 | ...(typeof update === "function" ? update(state) : (update as object)) |
| 29 | }; |
| 30 | |
| 31 | dispatchListeners(); |
| 32 | }, |
| 33 | subscribe(f: Function) { |
| 34 | listeners.push(f); |
| 35 | return () => { |
| 36 | listeners.splice(listeners.indexOf(f), 1); |
| 37 | }; |
| 38 | }, |
| 39 | getState() { |
| 40 | return state; |
| 41 | }, |
| 42 | reset() { |
| 43 | state = initialState; |
| 44 | dispatchListeners(); |
| 45 | } |
| 46 | }; |
| 47 | } |
| 48 | export default createStore; |
no outgoing calls
no test coverage detected