})
```
+Note that custom options are passed as the 3rd argument when using the setup syntax:
+
+```js
+defineStore(
+ 'search',
+ () => {
+ // ...
+ },
+ {
+ // this will be read by a plugin later on
+ debounce: {
+ // debounce the action searchContacts by 300ms
+ searchContacts: 300,
+ },
+ }
+)
+```
+
## TypeScript
Everything shown above can be done with typing support, so you don't ever need to use `any` or `@ts-ignore`.
S extends StateTree,
G /* extends GettersTree<S> */,
A /* extends Record<string, StoreAction> */
-> extends DefineStoreOptionsBase<Store<Id, S, G, A>, S> {
+> extends DefineStoreOptionsBase<S, Store<Id, S, G, A>> {
/**
* Unique string key to identify the store across the application.
*/
S extends StateTree,
G,
A /* extends ActionsTree */
-> extends DefineStoreOptionsBase<Store<Id, S, G, A>, S> {
+> extends DefineStoreOptionsBase<S, Store<Id, S, G, A>> {
/**
* Extracted actions. Added by useStore(). SHOULD NOT be added by the user when
* creating the store. Can be used in plugins to get the list of actions in a
stateOnly: number
}
- export interface DefineStoreOptions<Id, S, G, A> {
- debounce?: {
- // Record<keyof A, number>
- [k in keyof A]?: number
- }
+ export interface DefineStoreOptionsBase<S, Store> {
+ debounce?: Partial<Record<keyof StoreActions<Store>, number>>
}
+
+ // export interface DefineStoreOptions<Id, S, G, A> {
+ // debounce?: Partial<Record<keyof A, number>>
+ // }
}
const pinia = createPinia()
},
})
+defineStore(
+ 'withSetup',
+ () => {
+ function one() {}
+ function two() {}
+ function three() {}
+
+ return { one, two, three }
+ },
+ {
+ debounce: {
+ one: 200,
+ two: 300,
+ },
+ }
+)
+
type Procedure = (...args: any[]) => any
function debounce<F extends Procedure>(fn: F, time: number = 200) {
}>(mapStores(useStore))
pinia.use(({ options, store }) => {
- if (options.debounce) {
- return Object.keys(options.debounce).reduce((debouncedActions, action) => {
+ const { debounce: debounceOptions } = options
+ if (debounceOptions) {
+ return Object.keys(debounceOptions).reduce((debouncedActions, action) => {
debouncedActions[action] = debounce(
store[action],
- options.debounce![action as keyof typeof options['actions']]
+ debounceOptions[action]
)
return debouncedActions
}, {} as Record<string, (...args: any[]) => any>)