From 6ee95c7e35ccfae037d3dc534cecae1e9a28b498 Mon Sep 17 00:00:00 2001 From: daishi Date: Wed, 16 Oct 2024 09:44:36 +0900 Subject: [PATCH] refactor --- src/vanilla/utils/proxyMap.ts | 10 +++++----- src/vanilla/utils/proxySet.ts | 24 +++++++++++------------- 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/src/vanilla/utils/proxyMap.ts b/src/vanilla/utils/proxyMap.ts index fda42d2f..40950b02 100644 --- a/src/vanilla/utils/proxyMap.ts +++ b/src/vanilla/utils/proxyMap.ts @@ -52,7 +52,7 @@ export function proxyMap(entries?: Iterable<[K, V]> | undefined | null) { const index = map.get(key) if (index === undefined) { // eslint-disable-next-line @typescript-eslint/no-unused-expressions - this.index + this.index // touch property for tracking return undefined } return this.data[index] @@ -62,7 +62,7 @@ export function proxyMap(entries?: Iterable<[K, V]> | undefined | null) { const exists = map.has(key) if (!exists) { // eslint-disable-next-line @typescript-eslint/no-unused-expressions - this.index + this.index // touch property for tracking } return exists }, @@ -71,11 +71,11 @@ export function proxyMap(entries?: Iterable<[K, V]> | undefined | null) { throw new Error('Cannot perform mutations on a snapshot') } const index = indexMap.get(key) - if (index !== undefined) { - this.data[index] = value - } else { + if (index === undefined) { indexMap.set(key, this.index) this.data[this.index++] = value + } else { + this.data[index] = value } return this }, diff --git a/src/vanilla/utils/proxySet.ts b/src/vanilla/utils/proxySet.ts index ecdddcf7..7ba52119 100644 --- a/src/vanilla/utils/proxySet.ts +++ b/src/vanilla/utils/proxySet.ts @@ -36,11 +36,11 @@ export function proxySet(initialValues?: Iterable | null) { if (typeof initialValues[Symbol.iterator] !== 'function') { throw new TypeError('not iterable') } - for (const v of initialValues) { - if (!indexMap.has(v)) { - const value = maybeProxify(v) - indexMap.set(value, initialIndex) - initialData[initialIndex++] = value + for (const value of initialValues) { + if (!indexMap.has(value)) { + const v = maybeProxify(value) + indexMap.set(v, initialIndex) + initialData[initialIndex++] = v } } } @@ -54,13 +54,13 @@ export function proxySet(initialValues?: Iterable | null) { } return indexMap.size }, - has(v: T) { + has(value: T) { const map = getMapForThis(this) - const value = maybeProxify(v) - const exists = map.has(value) + const v = maybeProxify(value) + const exists = map.has(v) if (!exists) { // eslint-disable-next-line @typescript-eslint/no-unused-expressions - this.index + this.index // touch property for tracking } return exists }, @@ -70,10 +70,8 @@ export function proxySet(initialValues?: Iterable | null) { } const v = maybeProxify(value) if (!indexMap.has(v)) { - let nextIndex = this.index - indexMap.set(v, nextIndex) - this.data[nextIndex++] = v - this.index = nextIndex + indexMap.set(v, this.index) + this.data[this.index++] = v } return this },