From: Eduardo San Martin Morote Date: Wed, 31 Mar 2021 09:09:32 +0000 (+0200) Subject: refactor: extract PiniaPlugin X-Git-Tag: v0.2.2~3 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=5eda7653164eebdde8c2491a78a7423af5a9a855;p=thirdparty%2Fvuejs%2Fpinia.git refactor: extract PiniaPlugin --- diff --git a/src/index.ts b/src/index.ts index 67be7fff..1da08104 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,11 +2,11 @@ export { setActivePinia, createPinia, Pinia, - PiniaPlugin, PiniaStorePlugin, PiniaCustomProperties, } from './rootStore' export { defineStore } from './store' +export { PiniaPlugin } from './plugin' export { StateTree, Store, diff --git a/src/plugin.ts b/src/plugin.ts new file mode 100644 index 00000000..d92cc12b --- /dev/null +++ b/src/plugin.ts @@ -0,0 +1,33 @@ +import { PluginFunction } from 'vue' +import { piniaSymbol } from './rootStore' + +export const PiniaPlugin: PluginFunction = function (_Vue) { + // Equivalent of + // app.config.globalProperties.$pinia = pinia + _Vue.mixin({ + beforeCreate() { + const options = this.$options + if (options.pinia) { + options.pinia.Vue = _Vue + // HACK: taken from provide(): https://github.com/vuejs/composition-api/blob/master/src/apis/inject.ts#L25 + /* istanbul ignore else */ + if (!(this as any)._provided) { + const provideCache = {} + Object.defineProperty(this, '_provided', { + get: () => provideCache, + set: (v) => Object.assign(provideCache, v), + }) + } + ;(this as any)._provided[piniaSymbol as any] = options.pinia + + // propagate the pinia instance in an SSR friendly way + // avoid adding it to nuxt twice + if (!this.$pinia) { + this.$pinia = options.pinia + } + } else if (!this.$pinia && options.parent && options.parent.$pinia) { + this.$pinia = options.parent.$pinia + } + }, + }) +} diff --git a/src/rootStore.ts b/src/rootStore.ts index eb73082b..438cebd9 100644 --- a/src/rootStore.ts +++ b/src/rootStore.ts @@ -1,6 +1,6 @@ import { InjectionKey, ref, Ref } from '@vue/composition-api' import { StateTree, StoreWithState, StateDescriptor } from './types' -import { PluginFunction, VueConstructor } from 'vue' +import { VueConstructor } from 'vue' import type Vue from 'vue' /** @@ -76,34 +76,6 @@ declare module 'vue/types/options' { } } -export const PiniaPlugin: PluginFunction = function (_Vue) { - // Equivalent of - // app.config.globalProperties.$pinia = pinia - _Vue.mixin({ - beforeCreate() { - const options = this.$options - if (options.pinia) { - options.pinia.Vue = _Vue - // HACK: taken from provide(): https://github.com/vuejs/composition-api/blob/master/src/apis/inject.ts#L25 - /* istanbul ignore else */ - if (!(this as any)._provided) { - const provideCache = {} - Object.defineProperty(this, '_provided', { - get: () => provideCache, - set: (v) => Object.assign(provideCache, v), - }) - } - ;(this as any)._provided[piniaSymbol as any] = options.pinia - - // propagate the pinia instance in an SSR friendly way - this.$pinia = options.pinia - } else if (options.parent && options.parent.$pinia) { - this.$pinia = options.parent.$pinia - } - }, - }) -} - /** * Creates a Pinia instance to be used by the application */