From 012ca4504e961b02f1f86fd9d8711c6f96b57714 Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Wed, 21 Jul 2021 17:34:45 +0200 Subject: [PATCH] refactor: store subscriptions --- src/store.ts | 67 ++++++++------------------------------------ src/subscriptions.ts | 32 +++++++++++++++++++++ 2 files changed, 44 insertions(+), 55 deletions(-) create mode 100644 src/subscriptions.ts diff --git a/src/store.ts b/src/store.ts index bf011a1b..31575668 100644 --- a/src/store.ts +++ b/src/store.ts @@ -46,6 +46,7 @@ import { } from './rootStore' import { IS_CLIENT } from './env' import { patchObject } from './hmr' +import { addSubscription, triggerSubscriptions } from './subscriptions' function innerPatch( target: T, @@ -198,12 +199,6 @@ function createSetupStore< const hotState = ref({} as S) - const triggerSubscriptions: SubscriptionCallback = (mutation, state) => { - subscriptions.forEach((callback) => { - callback(mutation, state) - }) - } - if (__DEV__ && !pinia._e.active) { throw new Error('Pinia destroyed') } @@ -219,6 +214,7 @@ function createSetupStore< (state, oldState) => { if (isListening) { triggerSubscriptions( + subscriptions, { storeId: $id, type: MutationType.direct, @@ -269,49 +265,12 @@ function createSetupStore< isListening = true // because we paused the watcher, we need to manually call the subscriptions triggerSubscriptions( + subscriptions, subscriptionMutation, pinia.state.value[$id] as UnwrapRef ) } - // TODO: refactor duplicated code for subscriptions - function $subscribe(callback: SubscriptionCallback, detached?: boolean) { - subscriptions.push(callback) - - const removeSubscription = () => { - const idx = subscriptions.indexOf(callback) - if (idx > -1) { - subscriptions.splice(idx, 1) - } - } - - if (!detached && getCurrentInstance()) { - onUnmounted(removeSubscription) - } - - return removeSubscription - } - - function $onAction( - callback: StoreOnActionListener, - detached?: boolean - ) { - actionSubscriptions.push(callback) - - const removeSubscription = () => { - const idx = actionSubscriptions.indexOf(callback) - if (idx > -1) { - actionSubscriptions.splice(idx, 1) - } - } - - if (!detached && getCurrentInstance()) { - onUnmounted(removeSubscription) - } - - return removeSubscription - } - const $reset = __DEV__ ? () => { throw new Error( @@ -341,15 +300,13 @@ function createSetupStore< onErrorCallback = callback } - actionSubscriptions.forEach((callback) => { - // @ts-expect-error - callback({ - args, - name, - store, - after, - onError, - }) + // @ts-expect-error + triggerSubscriptions(actionSubscriptions, { + args, + name, + store, + after, + onError, }) let ret: any @@ -441,10 +398,10 @@ function createSetupStore< _p: pinia, // _s: scope, $id, - $onAction, + $onAction: addSubscription.bind(null, actionSubscriptions), $patch, $reset, - $subscribe, + $subscribe: addSubscription.bind(null, subscriptions), } const store: Store = reactive( diff --git a/src/subscriptions.ts b/src/subscriptions.ts new file mode 100644 index 00000000..aae8996d --- /dev/null +++ b/src/subscriptions.ts @@ -0,0 +1,32 @@ +import { getCurrentInstance, onUnmounted } from 'vue' +import { _Method } from './types' + +export function addSubscription( + subscriptions: T[], + callback: T, + detached?: boolean +) { + subscriptions.push(callback) + + const removeSubscription = () => { + const idx = subscriptions.indexOf(callback) + if (idx > -1) { + subscriptions.splice(idx, 1) + } + } + + if (!detached && getCurrentInstance()) { + onUnmounted(removeSubscription) + } + + return removeSubscription +} + +export function triggerSubscriptions( + subscriptions: T[], + ...args: Parameters +) { + subscriptions.forEach((callback) => { + callback(...args) + }) +} -- 2.47.2