Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: change Subscription Array to Subscription Set #2862

Open
wants to merge 1 commit into
base: v2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions packages/pinia/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ import { addSubscription, triggerSubscriptions, noop } from './subscriptions'

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

type _ArrayType<AT> = AT extends Array<infer T> ? T : never
type _SetType<AT> = AT extends Set<infer T> ? T : never

/**
* Marks a function as an action for `$onAction`
Expand Down Expand Up @@ -273,8 +273,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<S>[] = []
let actionSubscriptions: StoreOnActionListener<Id, S, G, A>[] = []
let subscriptions: Set<SubscriptionCallback<S>> = new Set()
let actionSubscriptions: Set<StoreOnActionListener<Id, S, G, A>> = new Set()
let debuggerEvents: DebuggerEvent[] | DebuggerEvent
const initialState = pinia.state.value[$id] as UnwrapRef<S> | undefined

Expand Down Expand Up @@ -360,8 +360,8 @@ function createSetupStore<

function $dispose() {
scope.stop()
subscriptions = []
actionSubscriptions = []
subscriptions.clear()
actionSubscriptions.clear()
pinia._s.delete($id)
}

Expand All @@ -381,13 +381,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<typeof afterCallbackList>) {
afterCallbackList.push(callback)
const afterCallbackSet: Set<(resolvedReturn: any) => any> = new Set()
const onErrorCallbackSet: Set<(error: unknown) => unknown> = new Set()
function after(callback: _SetType<typeof afterCallbackSet>) {
afterCallbackSet.add(callback)
}
function onError(callback: _ArrayType<typeof onErrorCallbackList>) {
onErrorCallbackList.push(callback)
function onError(callback: _SetType<typeof onErrorCallbackSet>) {
onErrorCallbackSet.add(callback)
}

// @ts-expect-error
Expand All @@ -404,24 +404,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<Fn>

Expand Down
15 changes: 6 additions & 9 deletions packages/pinia/src/subscriptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,16 @@ import { _Method } from './types'
export const noop = () => {}

export function addSubscription<T extends _Method>(
subscriptions: T[],
subscriptions: Set<T>,
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()) {
Expand All @@ -27,10 +24,10 @@ export function addSubscription<T extends _Method>(
}

export function triggerSubscriptions<T extends _Method>(
subscriptions: T[],
subscriptions: Set<T>,
...args: Parameters<T>
) {
subscriptions.slice().forEach((callback) => {
subscriptions.forEach((callback) => {
callback(...args)
})
}
Loading