} from './rootStore'
import { IS_CLIENT } from './env'
import { patchObject } from './hmr'
+import { addSubscription, triggerSubscriptions } from './subscriptions'
function innerPatch<T extends StateTree>(
target: T,
const hotState = ref({} as S)
- const triggerSubscriptions: SubscriptionCallback<S> = (mutation, state) => {
- subscriptions.forEach((callback) => {
- callback(mutation, state)
- })
- }
-
if (__DEV__ && !pinia._e.active) {
throw new Error('Pinia destroyed')
}
(state, oldState) => {
if (isListening) {
triggerSubscriptions(
+ subscriptions,
{
storeId: $id,
type: MutationType.direct,
isListening = true
// because we paused the watcher, we need to manually call the subscriptions
triggerSubscriptions(
+ subscriptions,
subscriptionMutation,
pinia.state.value[$id] as UnwrapRef<S>
)
}
- // TODO: refactor duplicated code for subscriptions
- function $subscribe(callback: SubscriptionCallback<S>, 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<Id, S, G, A>,
- 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(
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
_p: pinia,
// _s: scope,
$id,
- $onAction,
+ $onAction: addSubscription.bind(null, actionSubscriptions),
$patch,
$reset,
- $subscribe,
+ $subscribe: addSubscription.bind(null, subscriptions),
}
const store: Store<Id, S, G, A> = reactive(
--- /dev/null
+import { getCurrentInstance, onUnmounted } from 'vue'
+import { _Method } from './types'
+
+export function addSubscription<T extends _Method>(
+ 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<T extends _Method>(
+ subscriptions: T[],
+ ...args: Parameters<T>
+) {
+ subscriptions.forEach((callback) => {
+ callback(...args)
+ })
+}