From: Wick Date: Tue, 1 Jul 2025 09:57:12 +0000 (+0800) Subject: refactor: change Subscription Array to Subscription Set (#2887) X-Git-Tag: @pinia/nuxt@0.11.2~6 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=dcc34d262bf03be505770c6ee5fc6a25755523e2;p=thirdparty%2Fvuejs%2Fpinia.git refactor: change Subscription Array to Subscription Set (#2887) This reduces the size of the library a bit and should avoid the error of adding the same subscription twice --- diff --git a/packages/pinia/src/store.ts b/packages/pinia/src/store.ts index 4d10c690..7ec0e17a 100644 --- a/packages/pinia/src/store.ts +++ b/packages/pinia/src/store.ts @@ -53,7 +53,7 @@ import { addSubscription, triggerSubscriptions, noop } from './subscriptions' const fallbackRunWithContext = (fn: () => unknown) => fn() -type _ArrayType = AT extends Array ? T : never +type _SetType = AT extends Set ? T : never /** * Marks a function as an action for `$onAction` @@ -267,8 +267,8 @@ function createSetupStore< // internal state let isListening: boolean // set to true at the end let isSyncListening: boolean // set to true at the end - let subscriptions: SubscriptionCallback[] = [] - let actionSubscriptions: StoreOnActionListener[] = [] + let subscriptions: Set> = new Set() + let actionSubscriptions: Set> = new Set() let debuggerEvents: DebuggerEvent[] | DebuggerEvent const initialState = pinia.state.value[$id] as UnwrapRef | undefined @@ -350,8 +350,8 @@ function createSetupStore< function $dispose() { scope.stop() - subscriptions = [] - actionSubscriptions = [] + subscriptions.clear() + actionSubscriptions.clear() pinia._s.delete($id) } @@ -371,13 +371,13 @@ function createSetupStore< setActivePinia(pinia) const args = Array.from(arguments) - const afterCallbackList: Array<(resolvedReturn: any) => any> = [] - const onErrorCallbackList: Array<(error: unknown) => unknown> = [] - function after(callback: _ArrayType) { - afterCallbackList.push(callback) + const afterCallbackSet: Set<(resolvedReturn: any) => any> = new Set() + const onErrorCallbackSet: Set<(error: unknown) => unknown> = new Set() + function after(callback: _SetType) { + afterCallbackSet.add(callback) } - function onError(callback: _ArrayType) { - onErrorCallbackList.push(callback) + function onError(callback: _SetType) { + onErrorCallbackSet.add(callback) } // @ts-expect-error @@ -394,24 +394,24 @@ function createSetupStore< ret = fn.apply(this && this.$id === $id ? this : store, args) // handle sync errors } catch (error) { - triggerSubscriptions(onErrorCallbackList, error) + triggerSubscriptions(onErrorCallbackSet, error) throw error } if (ret instanceof Promise) { return ret .then((value) => { - triggerSubscriptions(afterCallbackList, value) + triggerSubscriptions(afterCallbackSet, value) return value }) .catch((error) => { - triggerSubscriptions(onErrorCallbackList, error) + triggerSubscriptions(onErrorCallbackSet, error) return Promise.reject(error) }) } // trigger after callbacks - triggerSubscriptions(afterCallbackList, ret) + triggerSubscriptions(afterCallbackSet, ret) return ret } as MarkedAction diff --git a/packages/pinia/src/subscriptions.ts b/packages/pinia/src/subscriptions.ts index 49b2d717..7c33f11c 100644 --- a/packages/pinia/src/subscriptions.ts +++ b/packages/pinia/src/subscriptions.ts @@ -4,19 +4,16 @@ import { _Method } from './types' export const noop = () => {} export function addSubscription( - subscriptions: T[], + subscriptions: Set, callback: T, detached?: boolean, onCleanup: () => void = noop ) { - subscriptions.push(callback) + subscriptions.add(callback) const removeSubscription = () => { - const idx = subscriptions.indexOf(callback) - if (idx > -1) { - subscriptions.splice(idx, 1) - onCleanup() - } + const isDel = subscriptions.delete(callback) + isDel && onCleanup() } if (!detached && getCurrentScope()) { @@ -27,10 +24,10 @@ export function addSubscription( } export function triggerSubscriptions( - subscriptions: T[], + subscriptions: Set, ...args: Parameters ) { - subscriptions.slice().forEach((callback) => { + subscriptions.forEach((callback) => { callback(...args) }) }