export {
StateTree,
Store,
+ GenericStore,
+ GenericStoreDefinition,
StoreWithGetters,
StoreWithActions,
StoreWithState,
* Plugin to extend every store
*/
export interface PiniaStorePlugin {
- (pinia: Pinia): Partial<PiniaCustomProperties>
+ (context: {
+ pinia: Pinia
+ store: GenericStore
+ }): Partial<PiniaCustomProperties> | void
}
/**
/**
* 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
*/
*
* @internal
*/
- _p: Array<() => Partial<PiniaCustomProperties>>
+ _p: PiniaStorePlugin[]
/**
* Vue constructor retrieved when installing the pinia.
/**
* Cache of stores instantiated by the current instance. Used by map
* helpers.
+ *
* @internal
*/
_pStores?: Record<string, GenericStore>
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,
} 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),
// created.
Object.defineProperty(store, '$state', descriptor)
+ // apply all plugins
+ pinia._p.forEach((extender) => {
+ assign(store, extender({ store, pinia }))
+ })
+
return store
}
-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',
},
})
-let store = useStore()
+const store = useStore()
expectType<{ a: 'on' | 'off' }>(store.$state)
expectType<number>(store.nested.counter)