|
| 1 | +import { batch } from "../batch"; |
| 2 | +import { type AddEventListener, event, send, size } from "../event"; |
| 3 | +import { writable } from "../readable"; |
| 4 | +import { type Disposer, type OwnedWritable, type Readable } from "../typings"; |
| 5 | +import { strictEqual } from "../utils"; |
| 6 | + |
| 7 | +export interface ReactiveMapChanged<K, V> { |
| 8 | + readonly upsert: readonly [K, V][]; |
| 9 | + readonly delete: readonly K[]; |
| 10 | +} |
| 11 | + |
| 12 | +export class OwnedReactiveMap<K, V> extends Map<K, V> { |
| 13 | + /** |
| 14 | + * A Readable that emits the map itself whenever it changes. |
| 15 | + */ |
| 16 | + public get $(): Readable<ReactiveMap<K, V>> { |
| 17 | + return (this._$ ??= writable(this, { equal: false })); |
| 18 | + } |
| 19 | + |
| 20 | + /** |
| 21 | + * Subscribe to changes in the map. |
| 22 | + * |
| 23 | + * @param fn - The function to call when the map is changed. |
| 24 | + * @returns A disposer function to unsubscribe from the event. |
| 25 | + */ |
| 26 | + public onChanged(fn: (changed: ReactiveMapChanged<K, V>) => void): Disposer { |
| 27 | + if (!this._onChanged_) { |
| 28 | + this._onChanged_ = event({ delete: new Set(), upsert: new Map() }); |
| 29 | + const handler = () => { |
| 30 | + if (this._onChanged_ && size(this._onChanged_)) { |
| 31 | + const { data_ } = this._onChanged_; |
| 32 | + if (data_.upsert.size > 0 || data_.delete.size > 0) { |
| 33 | + const changedData = { |
| 34 | + upsert: [...data_.upsert], |
| 35 | + delete: [...data_.delete], |
| 36 | + }; |
| 37 | + data_.upsert.clear(); |
| 38 | + data_.delete.clear(); |
| 39 | + send(this._onChanged_, changedData); |
| 40 | + } |
| 41 | + } else { |
| 42 | + this._onChanged_ = undefined; |
| 43 | + this.$.unsubscribe(handler); |
| 44 | + } |
| 45 | + }; |
| 46 | + this.$.onReaction_(handler); |
| 47 | + } |
| 48 | + return this._onChanged_(fn); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Subscribe to value removal events. |
| 53 | + * This is useful for further processing values that are removed from the map. |
| 54 | + * |
| 55 | + * A value is considered removed when: |
| 56 | + * - it is deleted from the map. |
| 57 | + * - it is replaced by another value (the old value is removed). |
| 58 | + * - it is cleared from the map. |
| 59 | + * |
| 60 | + * Note that for performance reasons, it does not handle the case where multiple keys map to the same value. |
| 61 | + * |
| 62 | + * @param fn - The function to call when a value is removed. |
| 63 | + * @returns A disposer function to unsubscribe from the event. |
| 64 | + */ |
| 65 | + public onValueRemoved(fn: (value: V) => void): Disposer { |
| 66 | + if (!this._onValueRemoved_) { |
| 67 | + this._onValueRemoved_ = event(new Set<V>()); |
| 68 | + const handler = () => { |
| 69 | + if (this._onValueRemoved_ && size(this._onValueRemoved_)) { |
| 70 | + const { data_ } = this._onValueRemoved_; |
| 71 | + if (data_.size) { |
| 72 | + const removedValues = [...data_]; |
| 73 | + data_.clear(); |
| 74 | + for (const value of removedValues) { |
| 75 | + send(this._onValueRemoved_, value); |
| 76 | + } |
| 77 | + } |
| 78 | + } else { |
| 79 | + this._onValueRemoved_ = undefined; |
| 80 | + this.$.unsubscribe(handler); |
| 81 | + } |
| 82 | + }; |
| 83 | + this.$.onReaction_(handler); |
| 84 | + } |
| 85 | + return this._onValueRemoved_(fn); |
| 86 | + } |
| 87 | + |
| 88 | + public constructor(entries?: readonly (readonly [K, V])[] | null) { |
| 89 | + super(); |
| 90 | + |
| 91 | + if (entries) { |
| 92 | + batch(() => { |
| 93 | + for (const [key, value] of entries) { |
| 94 | + this.set(key, value); |
| 95 | + } |
| 96 | + }); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + public dispose(): void { |
| 101 | + this._$?.dispose(); |
| 102 | + this._onChanged_ = this._onValueRemoved_ = undefined; |
| 103 | + super.clear(); |
| 104 | + } |
| 105 | + |
| 106 | + public override set(key: K, value: V): this { |
| 107 | + if (this.has(key)) { |
| 108 | + const oldValue = this.get(key)!; |
| 109 | + if (!strictEqual(oldValue, value)) { |
| 110 | + this._onValueRemoved_?.data_.add(oldValue); |
| 111 | + this._upsert_(key, value); |
| 112 | + } |
| 113 | + } else { |
| 114 | + this._upsert_(key, value); |
| 115 | + } |
| 116 | + return this; |
| 117 | + } |
| 118 | + |
| 119 | + public override delete(key: K): boolean { |
| 120 | + if (this.has(key)) { |
| 121 | + this._onValueRemoved_?.data_.add(this.get(key)!); |
| 122 | + this._onChanged_?.data_.delete.add(key); |
| 123 | + this._onChanged_?.data_.upsert.delete(key); |
| 124 | + this._$?.set(this); |
| 125 | + } |
| 126 | + return super.delete(key); |
| 127 | + } |
| 128 | + |
| 129 | + public override clear(): void { |
| 130 | + if (this.size) { |
| 131 | + if (this._onValueRemoved_ || this._onChanged_) { |
| 132 | + for (const [key, value] of this) { |
| 133 | + this._onValueRemoved_?.data_.add(value); |
| 134 | + this._onChanged_?.data_.delete.add(key); |
| 135 | + this._onChanged_?.data_.upsert.delete(key); |
| 136 | + } |
| 137 | + } |
| 138 | + super.clear(); |
| 139 | + this._$?.set(this); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + public rename(key: K, newKey: K): void { |
| 144 | + batch(() => { |
| 145 | + if (this.has(key)) { |
| 146 | + const value = this.get(key)!; |
| 147 | + this.delete(key); |
| 148 | + this.set(newKey, value); |
| 149 | + } |
| 150 | + }); |
| 151 | + } |
| 152 | + |
| 153 | + /** @internal */ |
| 154 | + private _$?: OwnedWritable<this>; |
| 155 | + |
| 156 | + /** @internal */ |
| 157 | + private _onChanged_?: AddEventListener< |
| 158 | + ReactiveMapChanged<K, V>, |
| 159 | + { |
| 160 | + readonly upsert: Map<K, V>; |
| 161 | + readonly delete: Set<K>; |
| 162 | + } |
| 163 | + >; |
| 164 | + |
| 165 | + /** @internal */ |
| 166 | + private _onValueRemoved_?: AddEventListener<V, Set<V>>; |
| 167 | + |
| 168 | + /** @internal */ |
| 169 | + private _upsert_(key: K, value: V): void { |
| 170 | + this._onValueRemoved_?.data_.delete(value); |
| 171 | + this._onChanged_?.data_.upsert.set(key, value); |
| 172 | + this._onChanged_?.data_.delete.delete(key); |
| 173 | + super.set(key, value); |
| 174 | + this._$?.set(this); |
| 175 | + } |
| 176 | +} |
| 177 | + |
| 178 | +export type ReactiveMap<K, V> = Omit<OwnedReactiveMap<K, V>, "dispose">; |
| 179 | + |
| 180 | +export const reactiveMap = <K, V>(entries?: readonly (readonly [K, V])[] | null): OwnedReactiveMap<K, V> => |
| 181 | + new OwnedReactiveMap(entries); |
0 commit comments