|
| 1 | +import { type SetStateAction, useState, useEffect } from 'react' |
| 2 | + |
| 3 | +/** |
| 4 | + * This is a set that causes rerenders on updates |
| 5 | + * @note Effects and memos that use this set should also listen for its signal: `+INSTANCE` |
| 6 | + */ |
| 7 | +export class StatefulMap<K, T> extends Map<K, T> { |
| 8 | + /** The dispatch function for the signal */ |
| 9 | + private readonly _dispatchSignal?: React.Dispatch<SetStateAction<number>> |
| 10 | + /** The update signal */ |
| 11 | + private _signal: number |
| 12 | + /** THe dispatch function for redefining the set */ |
| 13 | + private _dispatchRedefine?: React.Dispatch<SetStateAction<StatefulMap<K, T>>> |
| 14 | + |
| 15 | + /** |
| 16 | + * Construct a StatefulSet |
| 17 | + * @param initial The initial value (parameter for a vanilla set) |
| 18 | + * @param dispatchSignal The dispatch function for the signal |
| 19 | + */ |
| 20 | + constructor (initial?: Map<K, T> | Array<[K, T]>, dispatchSignal?: StatefulMap<K, T>['_dispatchSignal']) { |
| 21 | + super(initial) |
| 22 | + this._signal = 0 |
| 23 | + this._dispatchSignal = dispatchSignal |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * Set the redefine dispatch |
| 28 | + * @private |
| 29 | + * @param callback The function |
| 30 | + */ |
| 31 | + _setRedefine (callback: StatefulMap<K, T>['_dispatchRedefine']): void { |
| 32 | + this._dispatchRedefine = callback |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Force a signal update |
| 37 | + */ |
| 38 | + forceUpdate (): void { |
| 39 | + this._dispatchSignal?.(++this._signal) |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Set the instance to an entirely new instance |
| 44 | + * @param value The new instance |
| 45 | + * @returns The new instance |
| 46 | + * @throws {Error} If no redefinition callback is defined |
| 47 | + */ |
| 48 | + reset (value: Map<K, T>): Map<K, T> { |
| 49 | + if (!this._dispatchRedefine) throw new Error('Cannot redefine Set. No redefine callback set.') |
| 50 | + const instance = new StatefulMap(value, this._dispatchSignal) |
| 51 | + instance._signal = this._signal |
| 52 | + |
| 53 | + this._dispatchRedefine(instance) |
| 54 | + instance._dispatchSignal?.(++instance._signal) |
| 55 | + |
| 56 | + return instance |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * @override |
| 61 | + */ |
| 62 | + override set (key: K, value: T): this { |
| 63 | + const old = super.get(key) |
| 64 | + const newKey = !this.has(key) |
| 65 | + super.set(key, value) |
| 66 | + if (newKey || old !== value) this._dispatchSignal?.(++this._signal) |
| 67 | + return this |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Bulk set an array of items |
| 72 | + * @note Always rerenders |
| 73 | + * @param items An array of items |
| 74 | + * @param keyFn Either the name of a property of each item or a function that returns the key for each item |
| 75 | + * @returns this |
| 76 | + */ |
| 77 | + bulkSet<U extends K & keyof T> (items: T[], keyFn: U | ((i: T) => U)): this { |
| 78 | + for (const item of items) { |
| 79 | + const key = typeof keyFn === 'function' ? keyFn(item) : item[keyFn] |
| 80 | + |
| 81 | + super.set(key as K, item) |
| 82 | + } |
| 83 | + this._dispatchSignal?.(++this._signal) |
| 84 | + |
| 85 | + return this |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * @override |
| 90 | + */ |
| 91 | + override delete (key: K): boolean { |
| 92 | + const returnValue = super.delete(key) |
| 93 | + if (returnValue) this._dispatchSignal?.(++this._signal) |
| 94 | + return returnValue |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * @override |
| 99 | + */ |
| 100 | + override clear (): void { |
| 101 | + super.clear() |
| 102 | + this._dispatchSignal?.(this._signal = 0) |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Returns the set's signal. Used for effects and memos that use this set |
| 107 | + * @returns The numeric signal |
| 108 | + */ |
| 109 | + override valueOf (): number { |
| 110 | + return this._signal |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +/** |
| 115 | + * Use a stately set |
| 116 | + * @note Any effects or memos that use this set should also listen for its signal (`+INSTANCE`) |
| 117 | + * @param initial The initial set value |
| 118 | + * @returns The stately set |
| 119 | + */ |
| 120 | +export function useMap<K, T> (initial?: Map<K, T> | Array<[K, T]>): StatefulMap<K, T> { |
| 121 | + const [, setSignal] = useState(Array.isArray(initial) ? initial.length : initial?.size ?? 0) |
| 122 | + const [map, setMap] = useState(new StatefulMap(initial, setSignal)) |
| 123 | + |
| 124 | + useEffect(() => map._setRedefine(setMap), [map]) |
| 125 | + |
| 126 | + return map |
| 127 | +} |
0 commit comments