// @ts-check
+/// <reference types="./types" />
import Vue from 'vue'
// @ts-ignore: this must be pinia to load the local module
import { setActivePinia, PiniaPlugin, createPinia } from 'pinia'
const myPlugin = (context, inject) => {
// console.log(context)
+ /** @type {import('../src').Pinia} */
const pinia = createPinia()
context.app.pinia = pinia
context.pinia = pinia
setActivePinia(pinia)
- pinia.use(() => ({ $nuxt: context }))
+ // we bypass warnings
+ // @ts-ignore
+ pinia._p.push(() => ({ $nuxt: context }))
if (process.server) {
context.beforeNuxtRender(({ nuxtState }) => {
+import { Pinia } from './rootStore'
import { DevtoolHook, StateTree, StoreWithState } from './types'
import { assign } from './utils'
_devtoolHook: DevtoolHook
_vm: { $options: { computed: {} } }
_mutations: {}
- // we neeed to store modules names
+ // we need to store modules names
_modulesNamespaceMap: Record<string, boolean>
_modules: {
// we only need this specific method to let devtools retrieve the module name
let rootStore: RootState
-export function useStoreDevtools(store: StoreWithState<string, StateTree>) {
+export function useStoreDevtools(
+ store: StoreWithState<string, StateTree>,
+ stateDescriptor: { get: () => StateTree; set: (newValue: StateTree) => void }
+): void {
if (!devtoolHook) return
if (!rootStore) {
_devtoolHook: devtoolHook,
_vm: { $options: { computed: {} } },
_mutations: {},
- // we neeed to store modules names
+ // we need to store modules names
_modulesNamespaceMap: {},
_modules: {
// we only need this specific method to let devtools retrieve the module name
devtoolHook.emit('vuex:init', rootStore)
}
- rootStore.state[store.$id] = store.$state
-
// tell the devtools we added a module
rootStore.registerModule(store.$id, store)
- Object.defineProperty(rootStore.state, store.$id, {
- get: () => store.$state,
- set: (state) => (store.$state = state),
- })
+ Object.defineProperty(rootStore.state, store.$id, stateDescriptor)
// Vue.set(rootStore.state, store.name, store.state)
// the trailing slash is removed by the devtools
rootStore._modulesNamespaceMap[store.$id + '/'] = true
devtoolHook.on('vuex:travel-to-state', (targetState) => {
- store.$state = targetState[store.$id]
+ stateDescriptor.set(targetState[store.$id])
})
store.$subscribe((mutation, state) => {
/**
* Creates a store with its state object. This is meant to be augmented with getters and actions
*
- * @param id - unique identifier of the store, like a name. eg: main, cart, user
+ * @param $id - unique identifier of the store, like a name. eg: main, cart, user
* @param buildState - function to build the initial state
* @param initialState - initial state applied to the store, Must be correctly typed to infer typings
*/
stores.set(id, storeAndDescriptor)
+ if (__DEV__ && isClient) {
+ useStoreDevtools(storeAndDescriptor[0], storeAndDescriptor[1])
+ }
+
const store = buildStoreToUse(
storeAndDescriptor[0],
storeAndDescriptor[1],
actions as Record<string, Method> | undefined
)
- if (isClient) useStoreDevtools(store)
-
return store
}