diff --git a/src/vanilla/utils/proxyMap.ts b/src/vanilla/utils/proxyMap.ts index 12219f00..6bcc24e3 100644 --- a/src/vanilla/utils/proxyMap.ts +++ b/src/vanilla/utils/proxyMap.ts @@ -178,12 +178,36 @@ export function proxyMap(entries?: Iterable<[K, V]> | undefined | null) { toJSON(): Map { return new Map(this.entries()) }, - // [ONLY-TS-5.9.3] [ONLY-TS-5.8.3] [ONLY-TS-5.7.3] [ONLY-TS-5.6.3] @ts-expect-error ignore - getOrInsert() { - throw new Error('not implemented') + // [ONLY-TS-5.9.3] [ONLY-TS-5.8.3] [ONLY-TS-5.7.3] [ONLY-TS-5.6.3] @ts-ignore ignore + getOrInsert(key: K, defaultValue: V): V { + if (!isProxy(this)) { + throw new Error('Cannot perform mutations on a snapshot') + } + const index = indexMap.get(key) + if (index !== undefined) { + return this.data[index]! + } + indexMap.set(key, this.index) + this.data[this.index] = defaultValue + this.index++ + this.epoch++ + return defaultValue }, - getOrInsertComputed() { - throw new Error('not implemented') + // [ONLY-TS-5.9.3] [ONLY-TS-5.8.3] [ONLY-TS-5.7.3] [ONLY-TS-5.6.3] @ts-ignore ignore + getOrInsertComputed(key: K, callbackFn: (key: K) => V): V { + if (!isProxy(this)) { + throw new Error('Cannot perform mutations on a snapshot') + } + const index = indexMap.get(key) + if (index !== undefined) { + return this.data[index]! + } + const value = callbackFn(key) + indexMap.set(key, this.index) + this.data[this.index] = value + this.index++ + this.epoch++ + return value }, }