* Default store + deepFreeze on setState to make sure nothing is mutated accidentally
| 4 | * Default store + deepFreeze on setState to make sure nothing is mutated accidentally |
| 5 | */ |
| 6 | class DeepFrozenStore { |
| 7 | constructor() { |
| 8 | this.state = {} |
| 9 | this.callbacks = [] |
| 10 | } |
| 11 | |
| 12 | getState() { |
| 13 | return this.state |
| 14 | } |
| 15 | |
| 16 | setState(patch) { |
| 17 | const prevState = { ...this.state } |
| 18 | const nextState = deepFreeze({ ...this.state, ...patch }) |
| 19 | |
| 20 | this.state = nextState |
| 21 | this._publish(prevState, nextState, patch) |
| 22 | } |
| 23 | |
| 24 | subscribe(listener) { |
| 25 | this.callbacks.push(listener) |
| 26 | return () => { |
| 27 | // Remove the listener. |
| 28 | this.callbacks.splice(this.callbacks.indexOf(listener), 1) |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | _publish(...args) { |
| 33 | this.callbacks.forEach((listener) => { |
| 34 | listener(...args) |
| 35 | }) |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | export default function defaultStore() { |
| 40 | return new DeepFrozenStore() |
no outgoing calls
no test coverage detected
searching dependent graphs…