]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
feat(plugins): pass a context object to plugins instead of app
authorEduardo San Martin Morote <posva13@gmail.com>
Tue, 27 Apr 2021 08:47:23 +0000 (10:47 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Tue, 27 Apr 2021 08:47:23 +0000 (10:47 +0200)
BREAKING CHANGE: To improve the plugin api capabilities, `pinia.use()`
now receives a context object instead of just `pinia`:
```js
// replace
pinia.use((pinia) => {})
// with
pinia.use(({ pinia, store }) => {})
```

src/index.ts
src/rootStore.ts
src/store.ts
test-dts/store.test-d.ts

index 84d1944fdc67ad867d9889023c51d3cfb93f1c2f..3e62d1238443dc7d2314240a6d878ffbb52239cf 100644 (file)
@@ -9,6 +9,8 @@ export { PiniaPlugin } from './plugin'
 export {
   StateTree,
   Store,
+  GenericStore,
+  GenericStoreDefinition,
   StoreWithGetters,
   StoreWithActions,
   StoreWithState,
index cc523f9f43e42fff9f67316801aa2d493683b6cd..24df34cddaed0b349fd115b25c7aad3ebf5c00c6 100644 (file)
@@ -29,7 +29,10 @@ export const piniaSymbol = (__DEV__
  * Plugin to extend every store
  */
 export interface PiniaStorePlugin {
-  (pinia: Pinia): Partial<PiniaCustomProperties>
+  (context: {
+    pinia: Pinia
+    store: GenericStore
+  }): Partial<PiniaCustomProperties> | void
 }
 
 /**
@@ -44,8 +47,7 @@ export interface Pinia {
   /**
    * Adds a store plugin to extend every store
    *
-   * @alpha DO NOT USE, The plugin architecture will change to provide more
-   * customization options.
+   * @alpha the plugin API could change in the future
    *
    * @param plugin - store plugin to add
    */
@@ -56,7 +58,7 @@ export interface Pinia {
    *
    * @internal
    */
-  _p: Array<() => Partial<PiniaCustomProperties>>
+  _p: PiniaStorePlugin[]
 
   /**
    * Vue constructor retrieved when installing the pinia.
@@ -74,6 +76,7 @@ declare module 'vue/types/vue' {
     /**
      * Cache of stores instantiated by the current instance. Used by map
      * helpers.
+     *
      * @internal
      */
     _pStores?: Record<string, GenericStore>
@@ -105,13 +108,7 @@ export function createPinia(): Pinia {
     Vue: {} as any,
 
     use(plugin) {
-      /* istanbul ignore next */
-      if (__DEV__ && !__TEST__) {
-        console.warn(
-          `[🍍]: The plugin API has plans to change to bring better extensibility. "pinia.use()" signature will change in the next release. It is recommended to avoid using this API.`
-        )
-      }
-      _p.push(plugin.bind(null, pinia))
+      _p.push(plugin)
     },
 
     _p,
index e6453222ffdc1d8b8c08b88b397af896da3a1ceb..388c3a6553f6144531e602ebaac51de0b58a8a8c 100644 (file)
@@ -229,15 +229,9 @@ function buildStoreToUse<
     } as StoreWithActions<A>[typeof actionName]
   }
 
-  const extensions = pinia._p.reduce(
-    (extended, extender) => assign({}, extended, extender()),
-    {} as PiniaCustomProperties
-  )
-
   const store: Store<Id, S, G, A> = reactive(
     assign(
       {},
-      extensions,
       partialStore,
       // using this means no new properties can be added as state
       computedFromState(pinia.state, $id),
@@ -251,6 +245,11 @@ function buildStoreToUse<
   // created.
   Object.defineProperty(store, '$state', descriptor)
 
+  // apply all plugins
+  pinia._p.forEach((extender) => {
+    assign(store, extender({ store, pinia }))
+  })
+
   return store
 }
 
index 5350092be18e3ba28f815f1728a882614034add9..f068a4b6c3127b8aafd815099965a9fcee12759b 100644 (file)
@@ -1,4 +1,11 @@
-import { defineStore, expectType } from './'
+import { GenericStore } from 'dist/src/types'
+import { defineStore, expectType, createPinia } from './'
+
+const pinia = createPinia()
+
+pinia.use(({ store }) => {
+  expectType<GenericStore>(store)
+})
 
 const useStore = defineStore({
   id: 'name',
@@ -10,7 +17,7 @@ const useStore = defineStore({
   },
 })
 
-let store = useStore()
+const store = useStore()
 
 expectType<{ a: 'on' | 'off' }>(store.$state)
 expectType<number>(store.nested.counter)