From: Eduardo San Martin Morote Date: Mon, 8 Mar 2021 14:46:31 +0000 (+0100) Subject: refactor: small fixes and changes in devtools X-Git-Tag: v0.2.0~1 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=9af665977bd59c9e80acc49cd5011162c94990b5;p=thirdparty%2Fvuejs%2Fpinia.git refactor: small fixes and changes in devtools --- diff --git a/nuxt/plugin.js b/nuxt/plugin.js index c06f3b99..329acc98 100644 --- a/nuxt/plugin.js +++ b/nuxt/plugin.js @@ -1,4 +1,5 @@ // @ts-check +/// import Vue from 'vue' // @ts-ignore: this must be pinia to load the local module import { setActivePinia, PiniaPlugin, createPinia } from 'pinia' @@ -9,12 +10,15 @@ Vue.use(PiniaPlugin) 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 }) => { diff --git a/nuxt/types.d.ts b/nuxt/types.d.ts index f9564c93..fbe2e2d6 100644 --- a/nuxt/types.d.ts +++ b/nuxt/types.d.ts @@ -1,8 +1,16 @@ import '@nuxt/types' -import { Pinia } from '../dist/src' +import 'pinia' +import { Pinia } from 'pinia' +import { Context } from '@nuxt/types' declare module '@nuxt/types' { export interface Context { pinia: Pinia } } + +declare module 'pinia' { + export interface PiniaCustomProperties { + $nuxt: Context + } +} diff --git a/src/devtools.ts b/src/devtools.ts index 03a4a479..c0df6709 100644 --- a/src/devtools.ts +++ b/src/devtools.ts @@ -1,3 +1,4 @@ +import { Pinia } from './rootStore' import { DevtoolHook, StateTree, StoreWithState } from './types' import { assign } from './utils' @@ -15,7 +16,7 @@ interface RootState { _devtoolHook: DevtoolHook _vm: { $options: { computed: {} } } _mutations: {} - // we neeed to store modules names + // we need to store modules names _modulesNamespaceMap: Record _modules: { // we only need this specific method to let devtools retrieve the module name @@ -30,7 +31,10 @@ interface RootState { let rootStore: RootState -export function useStoreDevtools(store: StoreWithState) { +export function useStoreDevtools( + store: StoreWithState, + stateDescriptor: { get: () => StateTree; set: (newValue: StateTree) => void } +): void { if (!devtoolHook) return if (!rootStore) { @@ -38,7 +42,7 @@ export function useStoreDevtools(store: StoreWithState) { _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 @@ -58,22 +62,17 @@ export function useStoreDevtools(store: StoreWithState) { 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) => { diff --git a/src/store.ts b/src/store.ts index 15042c6d..61ceedf9 100644 --- a/src/store.ts +++ b/src/store.ts @@ -80,7 +80,7 @@ function computedFromState( /** * 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 */ @@ -281,6 +281,10 @@ export function defineStore< stores.set(id, storeAndDescriptor) + if (__DEV__ && isClient) { + useStoreDevtools(storeAndDescriptor[0], storeAndDescriptor[1]) + } + const store = buildStoreToUse( storeAndDescriptor[0], storeAndDescriptor[1], @@ -289,8 +293,6 @@ export function defineStore< actions as Record | undefined ) - if (isClient) useStoreDevtools(store) - return store }