]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
refactor: extract PiniaPlugin
authorEduardo San Martin Morote <posva13@gmail.com>
Wed, 31 Mar 2021 09:09:32 +0000 (11:09 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Wed, 31 Mar 2021 09:09:32 +0000 (11:09 +0200)
src/index.ts
src/plugin.ts [new file with mode: 0644]
src/rootStore.ts

index 67be7fff3bb5582a879edb2b4f7015bf908a8f5c..1da0810416f121e077a1c02fa8e1cd522d056b5a 100644 (file)
@@ -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 (file)
index 0000000..d92cc12
--- /dev/null
@@ -0,0 +1,33 @@
+import { PluginFunction } from 'vue'
+import { piniaSymbol } from './rootStore'
+
+export const PiniaPlugin: PluginFunction<void> = 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
+      }
+    },
+  })
+}
index eb73082b70ce0e8dff03b7a6e60c9500d6385729..438cebd96a23e32e3e9d4281115f591812c324ea 100644 (file)
@@ -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<void> = 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
  */