()
| 12 | } |
| 13 | |
| 14 | export function createStore(): Store { |
| 15 | const state = new Map<string, unknown>(); |
| 16 | const listeners = new Set<() => void>(); |
| 17 | let snapshot: Record<string, unknown> = {}; |
| 18 | |
| 19 | function notify() { |
| 20 | const currentListeners = [...listeners]; |
| 21 | for (const listener of currentListeners) { |
| 22 | listener(); |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | function rebuildSnapshot() { |
| 27 | snapshot = Object.fromEntries(state); |
| 28 | } |
| 29 | |
| 30 | function get(name: string): unknown { |
| 31 | return state.get(name); |
| 32 | } |
| 33 | |
| 34 | function set(name: string, value: unknown): void { |
| 35 | const existing = state.get(name); |
| 36 | if (Object.is(existing, value)) return; |
| 37 | // Shallow-compare plain objects (form data) |
| 38 | if ( |
| 39 | value && |
| 40 | existing && |
| 41 | typeof value === "object" && |
| 42 | typeof existing === "object" && |
| 43 | !Array.isArray(value) && |
| 44 | !Array.isArray(existing) |
| 45 | ) { |
| 46 | const nk = Object.keys(value as Record<string, unknown>); |
| 47 | const ok = Object.keys(existing as Record<string, unknown>); |
| 48 | if ( |
| 49 | nk.length === ok.length && |
| 50 | nk.every((k) => |
| 51 | Object.is( |
| 52 | (value as Record<string, unknown>)[k], |
| 53 | (existing as Record<string, unknown>)[k], |
| 54 | ), |
| 55 | ) |
| 56 | ) { |
| 57 | return; |
| 58 | } |
| 59 | } |
| 60 | state.set(name, value); |
| 61 | rebuildSnapshot(); |
| 62 | notify(); |
| 63 | } |
| 64 | |
| 65 | function subscribe(listener: () => void): () => void { |
| 66 | listeners.add(listener); |
| 67 | return () => { |
| 68 | listeners.delete(listener); |
| 69 | }; |
| 70 | } |
| 71 |
no outgoing calls
no test coverage detected