Skip to content

Commit dcc34d2

Browse files
authored
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
1 parent 24a7686 commit dcc34d2

File tree

2 files changed

+21
-24
lines changed

2 files changed

+21
-24
lines changed

packages/pinia/src/store.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ import { addSubscription, triggerSubscriptions, noop } from './subscriptions'
5353

5454
const fallbackRunWithContext = (fn: () => unknown) => fn()
5555

56-
type _ArrayType<AT> = AT extends Array<infer T> ? T : never
56+
type _SetType<AT> = AT extends Set<infer T> ? T : never
5757

5858
/**
5959
* Marks a function as an action for `$onAction`
@@ -267,8 +267,8 @@ function createSetupStore<
267267
// internal state
268268
let isListening: boolean // set to true at the end
269269
let isSyncListening: boolean // set to true at the end
270-
let subscriptions: SubscriptionCallback<S>[] = []
271-
let actionSubscriptions: StoreOnActionListener<Id, S, G, A>[] = []
270+
let subscriptions: Set<SubscriptionCallback<S>> = new Set()
271+
let actionSubscriptions: Set<StoreOnActionListener<Id, S, G, A>> = new Set()
272272
let debuggerEvents: DebuggerEvent[] | DebuggerEvent
273273
const initialState = pinia.state.value[$id] as UnwrapRef<S> | undefined
274274

@@ -350,8 +350,8 @@ function createSetupStore<
350350

351351
function $dispose() {
352352
scope.stop()
353-
subscriptions = []
354-
actionSubscriptions = []
353+
subscriptions.clear()
354+
actionSubscriptions.clear()
355355
pinia._s.delete($id)
356356
}
357357

@@ -371,13 +371,13 @@ function createSetupStore<
371371
setActivePinia(pinia)
372372
const args = Array.from(arguments)
373373

374-
const afterCallbackList: Array<(resolvedReturn: any) => any> = []
375-
const onErrorCallbackList: Array<(error: unknown) => unknown> = []
376-
function after(callback: _ArrayType<typeof afterCallbackList>) {
377-
afterCallbackList.push(callback)
374+
const afterCallbackSet: Set<(resolvedReturn: any) => any> = new Set()
375+
const onErrorCallbackSet: Set<(error: unknown) => unknown> = new Set()
376+
function after(callback: _SetType<typeof afterCallbackSet>) {
377+
afterCallbackSet.add(callback)
378378
}
379-
function onError(callback: _ArrayType<typeof onErrorCallbackList>) {
380-
onErrorCallbackList.push(callback)
379+
function onError(callback: _SetType<typeof onErrorCallbackSet>) {
380+
onErrorCallbackSet.add(callback)
381381
}
382382

383383
// @ts-expect-error
@@ -394,24 +394,24 @@ function createSetupStore<
394394
ret = fn.apply(this && this.$id === $id ? this : store, args)
395395
// handle sync errors
396396
} catch (error) {
397-
triggerSubscriptions(onErrorCallbackList, error)
397+
triggerSubscriptions(onErrorCallbackSet, error)
398398
throw error
399399
}
400400

401401
if (ret instanceof Promise) {
402402
return ret
403403
.then((value) => {
404-
triggerSubscriptions(afterCallbackList, value)
404+
triggerSubscriptions(afterCallbackSet, value)
405405
return value
406406
})
407407
.catch((error) => {
408-
triggerSubscriptions(onErrorCallbackList, error)
408+
triggerSubscriptions(onErrorCallbackSet, error)
409409
return Promise.reject(error)
410410
})
411411
}
412412

413413
// trigger after callbacks
414-
triggerSubscriptions(afterCallbackList, ret)
414+
triggerSubscriptions(afterCallbackSet, ret)
415415
return ret
416416
} as MarkedAction<Fn>
417417

packages/pinia/src/subscriptions.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,16 @@ import { _Method } from './types'
44
export const noop = () => {}
55

66
export function addSubscription<T extends _Method>(
7-
subscriptions: T[],
7+
subscriptions: Set<T>,
88
callback: T,
99
detached?: boolean,
1010
onCleanup: () => void = noop
1111
) {
12-
subscriptions.push(callback)
12+
subscriptions.add(callback)
1313

1414
const removeSubscription = () => {
15-
const idx = subscriptions.indexOf(callback)
16-
if (idx > -1) {
17-
subscriptions.splice(idx, 1)
18-
onCleanup()
19-
}
15+
const isDel = subscriptions.delete(callback)
16+
isDel && onCleanup()
2017
}
2118

2219
if (!detached && getCurrentScope()) {
@@ -27,10 +24,10 @@ export function addSubscription<T extends _Method>(
2724
}
2825

2926
export function triggerSubscriptions<T extends _Method>(
30-
subscriptions: T[],
27+
subscriptions: Set<T>,
3128
...args: Parameters<T>
3229
) {
33-
subscriptions.slice().forEach((callback) => {
30+
subscriptions.forEach((callback) => {
3431
callback(...args)
3532
})
3633
}

0 commit comments

Comments
 (0)