.yalc
yalc.lock
.idea
+.vitepress/cache
__build__
dist
coverage
+.vitepress/cache
+temp
} from '../src'
function defineOptions<
- O extends Omit<DefineStoreOptions<string, StateTree, any, any>, 'id'>
+ O extends Omit<DefineStoreOptions<string, StateTree, any, any>, 'id'>,
>(options: O): O {
return options
}
}),
getters: {
items: (state) =>
- state.rawItems.reduce((items, item) => {
- const existingItem = items.find((it) => it.name === item)
+ state.rawItems.reduce(
+ (items, item) => {
+ const existingItem = items.find((it) => it.name === item)
- if (!existingItem) {
- items.push({ name: item, amount: 1 })
- } else {
- existingItem.amount++
- }
+ if (!existingItem) {
+ items.push({ name: item, amount: 1 })
+ } else {
+ existingItem.amount++
+ }
- return items
- }, [] as { name: string; amount: number }[]),
+ return items
+ },
+ [] as { name: string; amount: number }[]
+ ),
},
actions: {
typeof window === 'object' && window.window === window
? window
: typeof self === 'object' && self.self === self
- ? self
- : typeof global === 'object' && global.global === global
- ? global
- : typeof globalThis === 'object'
- ? globalThis
- : { HTMLElement: null })()
+ ? self
+ : typeof global === 'object' && global.global === global
+ ? global
+ : typeof globalThis === 'object'
+ ? globalThis
+ : { HTMLElement: null })()
export interface Options {
autoBom?: boolean
export const saveAs: SaveAs = !IS_CLIENT
? () => {} // noop
: // Use download attribute first if possible (#193 Lumia mobile) unless this is a macOS WebView or mini program
- typeof HTMLAnchorElement !== 'undefined' &&
- 'download' in HTMLAnchorElement.prototype &&
- !isMacOSWebView
- ? downloadSaveAs
- : // Use msSaveOrOpenBlob as a second approach
- 'msSaveOrOpenBlob' in _navigator
- ? msSaveAs
- : // Fallback to using FileReader and a popup
- fileSaverSaveAs
+ typeof HTMLAnchorElement !== 'undefined' &&
+ 'download' in HTMLAnchorElement.prototype &&
+ !isMacOSWebView
+ ? downloadSaveAs
+ : // Use msSaveOrOpenBlob as a second approach
+ 'msSaveOrOpenBlob' in _navigator
+ ? msSaveAs
+ : // Fallback to using FileReader and a popup
+ fileSaverSaveAs
function downloadSaveAs(blob: Blob, name: string = 'download', opts?: Options) {
const a = document.createElement('a')
return {
editable: false,
key: id,
- value: store._getters!.reduce((getters, key) => {
- getters[key] = store[key]
- return getters
- }, {} as Record<string, any>),
+ value: store._getters!.reduce(
+ (getters, key) => {
+ getters[key] = store[key]
+ return getters
+ },
+ {} as Record<string, any>
+ ),
}
}),
}
Id extends string = string,
S extends StateTree = StateTree,
G extends object = _GettersTree<S>,
- A extends object = _ActionsTree
+ A extends object = _ActionsTree,
>({ app, store, options }: PiniaPluginContext<Id, S, G, A>) {
// HMR module
if (store.$id.startsWith('__hot:')) {
Id extends string = string,
S extends StateTree = StateTree,
G extends _GettersTree<S> = _GettersTree<S>,
- A = _ActionsTree
+ A = _ActionsTree,
>(initialUseStore: StoreDefinition<Id, S, G, A>, hot: any) {
// strip as much as possible from iife.prod
if (!__DEV__) {
/**
* For internal use **only**.
*/
-export type _StoreObject<S> = S extends StoreDefinition<
- infer Ids,
- infer State,
- infer Getters,
- infer Actions
->
- ? {
- [Id in `${Ids}${MapStoresCustomization extends Record<
- 'suffix',
- infer Suffix
- >
- ? Suffix
- : 'Store'}`]: () => Store<
- Id extends `${infer RealId}${MapStoresCustomization extends Record<
+export type _StoreObject<S> =
+ S extends StoreDefinition<
+ infer Ids,
+ infer State,
+ infer Getters,
+ infer Actions
+ >
+ ? {
+ [Id in `${Ids}${MapStoresCustomization extends Record<
'suffix',
infer Suffix
>
? Suffix
- : 'Store'}`
- ? RealId
- : string,
- State,
- Getters,
- Actions
- >
- }
- : {}
+ : 'Store'}`]: () => Store<
+ Id extends `${infer RealId}${MapStoresCustomization extends Record<
+ 'suffix',
+ infer Suffix
+ >
+ ? Suffix
+ : 'Store'}`
+ ? RealId
+ : string,
+ State,
+ Getters,
+ Actions
+ >
+ }
+ : {}
/**
* For internal use **only**.
export type _MapStateReturn<
S extends StateTree,
G extends _GettersTree<S>,
- Keys extends keyof S | keyof G = keyof S | keyof G
+ Keys extends keyof S | keyof G = keyof S | keyof G,
> = {
// [key in keyof S | keyof G]: () => key extends keyof S
// ? S[key]
T extends Record<
string,
keyof S | keyof G | ((store: Store<Id, S, G, A>) => any)
- > = {}
+ > = {},
> = {
[key in keyof T]: () => T[key] extends (store: any) => infer R
? R
: T[key] extends keyof Store<Id, S, G, A>
- ? Store<Id, S, G, A>[T[key]]
- : never
+ ? Store<Id, S, G, A>[T[key]]
+ : never
}
/**
KeyMapper extends Record<
string,
keyof S | keyof G | ((store: Store<Id, S, G, A>) => any)
- >
+ >,
>(
useStore: StoreDefinition<Id, S, G, A>,
keyMapper: KeyMapper
S extends StateTree,
G extends _GettersTree<S>,
A,
- Keys extends keyof S | keyof G
+ Keys extends keyof S | keyof G,
>(
useStore: StoreDefinition<Id, S, G, A>,
keys: readonly Keys[]
Id extends string,
S extends StateTree,
G extends _GettersTree<S>,
- A
+ A,
>(
useStore: StoreDefinition<Id, S, G, A>,
keysOrMapper: any
): _MapStateReturn<S, G> | _MapStateObjectReturn<Id, S, G, A> {
return Array.isArray(keysOrMapper)
- ? keysOrMapper.reduce((reduced, key) => {
- reduced[key] = function (this: ComponentPublicInstance) {
- return useStore(this.$pinia)[key]
- } as () => any
- return reduced
- }, {} as _MapStateReturn<S, G>)
- : Object.keys(keysOrMapper).reduce((reduced, key: string) => {
- // @ts-expect-error
- reduced[key] = function (this: ComponentPublicInstance) {
- const store = useStore(this.$pinia)
- const storeKey = keysOrMapper[key]
- // for some reason TS is unable to infer the type of storeKey to be a
- // function
- return typeof storeKey === 'function'
- ? (storeKey as (store: Store<Id, S, G, A>) => any).call(this, store)
- : store[storeKey]
- }
- return reduced
- }, {} as _MapStateObjectReturn<Id, S, G, A>)
+ ? keysOrMapper.reduce(
+ (reduced, key) => {
+ reduced[key] = function (this: ComponentPublicInstance) {
+ return useStore(this.$pinia)[key]
+ } as () => any
+ return reduced
+ },
+ {} as _MapStateReturn<S, G>
+ )
+ : Object.keys(keysOrMapper).reduce(
+ (reduced, key: string) => {
+ // @ts-expect-error
+ reduced[key] = function (this: ComponentPublicInstance) {
+ const store = useStore(this.$pinia)
+ const storeKey = keysOrMapper[key]
+ // for some reason TS is unable to infer the type of storeKey to be a
+ // function
+ return typeof storeKey === 'function'
+ ? (storeKey as (store: Store<Id, S, G, A>) => any).call(
+ this,
+ store
+ )
+ : store[storeKey]
+ }
+ return reduced
+ },
+ {} as _MapStateObjectReturn<Id, S, G, A>
+ )
}
/**
S extends StateTree,
G extends _GettersTree<S>,
A,
- KeyMapper extends Record<string, keyof A>
+ KeyMapper extends Record<string, keyof A>,
>(
useStore: StoreDefinition<Id, S, G, A>,
keyMapper: KeyMapper
Id extends string,
S extends StateTree,
G extends _GettersTree<S>,
- A
+ A,
>(
useStore: StoreDefinition<Id, S, G, A>,
keys: Array<keyof A>
S extends StateTree,
G extends _GettersTree<S>,
A,
- KeyMapper extends Record<string, keyof A>
+ KeyMapper extends Record<string, keyof A>,
>(
useStore: StoreDefinition<Id, S, G, A>,
keysOrMapper: Array<keyof A> | KeyMapper
}
return reduced
}, {} as _MapActionsReturn<A>)
- : Object.keys(keysOrMapper).reduce((reduced, key: keyof KeyMapper) => {
- // @ts-expect-error
- reduced[key] = function (
- this: ComponentPublicInstance,
- ...args: any[]
- ) {
- return useStore(this.$pinia)[keysOrMapper[key]](...args)
- }
- return reduced
- }, {} as _MapActionsObjectReturn<A, KeyMapper>)
+ : Object.keys(keysOrMapper).reduce(
+ (reduced, key: keyof KeyMapper) => {
+ // @ts-expect-error
+ reduced[key] = function (
+ this: ComponentPublicInstance,
+ ...args: any[]
+ ) {
+ return useStore(this.$pinia)[keysOrMapper[key]](...args)
+ }
+ return reduced
+ },
+ {} as _MapActionsObjectReturn<A, KeyMapper>
+ )
}
/**
*/
export type _MapWritableStateObjectReturn<
S extends StateTree,
- T extends Record<string, keyof S>
+ T extends Record<string, keyof S>,
> = {
[key in keyof T]: {
get: () => S[T[key]]
S extends StateTree,
G extends _GettersTree<S>,
A,
- KeyMapper extends Record<string, keyof S>
+ KeyMapper extends Record<string, keyof S>,
>(
useStore: StoreDefinition<Id, S, G, A>,
keyMapper: KeyMapper
S extends StateTree,
G extends _GettersTree<S>,
A,
- Keys extends keyof S
+ Keys extends keyof S,
>(
useStore: StoreDefinition<Id, S, G, A>,
keys: readonly Keys[]
S extends StateTree,
G extends _GettersTree<S>,
A,
- KeyMapper extends Record<string, keyof S>
+ KeyMapper extends Record<string, keyof S>,
>(
useStore: StoreDefinition<Id, S, G, A>,
keysOrMapper: Array<keyof S> | KeyMapper
}
return reduced
}, {} as _MapWritableStateReturn<S>)
- : Object.keys(keysOrMapper).reduce((reduced, key: keyof KeyMapper) => {
- // @ts-ignore
- reduced[key] = {
- get(this: ComponentPublicInstance) {
- return useStore(this.$pinia)[keysOrMapper[key]]
- },
- set(this: ComponentPublicInstance, value) {
- // it's easier to type it here as any
- return (useStore(this.$pinia)[keysOrMapper[key]] = value as any)
- },
- }
- return reduced
- }, {} as _MapWritableStateObjectReturn<S, KeyMapper>)
+ : Object.keys(keysOrMapper).reduce(
+ (reduced, key: keyof KeyMapper) => {
+ // @ts-ignore
+ reduced[key] = {
+ get(this: ComponentPublicInstance) {
+ return useStore(this.$pinia)[keysOrMapper[key]]
+ },
+ set(this: ComponentPublicInstance, value) {
+ // it's easier to type it here as any
+ return (useStore(this.$pinia)[keysOrMapper[key]] = value as any)
+ },
+ }
+ return reduced
+ },
+ {} as _MapWritableStateObjectReturn<S, KeyMapper>
+ )
}
Id extends string = string,
S extends StateTree = StateTree,
G /* extends _GettersTree<S> */ = _GettersTree<S>,
- A /* extends _ActionsTree */ = _ActionsTree
+ A /* extends _ActionsTree */ = _ActionsTree,
> {
/**
* pinia instance.
*
* @param context - Context
*/
- (context: PiniaPluginContext): Partial<
- PiniaCustomProperties & PiniaCustomStateProperties
- > | void
+ (
+ context: PiniaPluginContext
+ ): Partial<PiniaCustomProperties & PiniaCustomStateProperties> | void
}
/**
type _ArrayType<AT> = AT extends Array<infer T> ? T : never
function mergeReactiveObjects<
- T extends Record<any, unknown> | Map<unknown, unknown> | Set<unknown>
+ T extends Record<any, unknown> | Map<unknown, unknown> | Set<unknown>,
>(target: T, patchToApply: _DeepPartial<T>): T {
// Handle Map instances
if (target instanceof Map && patchToApply instanceof Map) {
Id extends string,
S extends StateTree,
G extends _GettersTree<S>,
- A extends _ActionsTree
+ A extends _ActionsTree,
>(
id: Id,
options: DefineStoreOptions<Id, S, G, A>,
return assign(
localState,
actions,
- Object.keys(getters || {}).reduce((computedGetters, name) => {
- if (__DEV__ && name in localState) {
- console.warn(
- `[🍍]: A getter cannot have the same name as another state property. Rename one of them. Found with "${name}" in store "${id}".`
- )
- }
+ Object.keys(getters || {}).reduce(
+ (computedGetters, name) => {
+ if (__DEV__ && name in localState) {
+ console.warn(
+ `[🍍]: A getter cannot have the same name as another state property. Rename one of them. Found with "${name}" in store "${id}".`
+ )
+ }
- computedGetters[name] = markRaw(
- computed(() => {
- setActivePinia(pinia)
- // it was created just before
- const store = pinia._s.get(id)!
+ computedGetters[name] = markRaw(
+ computed(() => {
+ setActivePinia(pinia)
+ // it was created just before
+ const store = pinia._s.get(id)!
- // allow cross using stores
- /* istanbul ignore if */
- if (isVue2 && !store._r) return
+ // allow cross using stores
+ /* istanbul ignore if */
+ if (isVue2 && !store._r) return
- // @ts-expect-error
- // return getters![name].call(context, context)
- // TODO: avoid reading the getter while assigning with a global variable
- return getters![name].call(store, store)
- })
- )
- return computedGetters
- }, {} as Record<string, ComputedRef>)
+ // @ts-expect-error
+ // return getters![name].call(context, context)
+ // TODO: avoid reading the getter while assigning with a global variable
+ return getters![name].call(store, store)
+ })
+ )
+ return computedGetters
+ },
+ {} as Record<string, ComputedRef>
+ )
)
}
SS extends Record<any, unknown>,
S extends StateTree,
G extends Record<string, _Method>,
- A extends _ActionsTree
+ A extends _ActionsTree,
>(
$id: Id,
setup: () => SS,
})
}
: /* istanbul ignore next */
- __DEV__
- ? () => {
- throw new Error(
- `🍍: Store "${$id}" is built using the setup syntax and does not implement $reset().`
- )
- }
- : noop
+ __DEV__
+ ? () => {
+ throw new Error(
+ `🍍: Store "${$id}" is built using the setup syntax and does not implement $reset().`
+ )
+ }
+ : noop
function $dispose() {
scope.stop()
* Extract the actions of a store type. Works with both a Setup Store or an
* Options Store.
*/
-export type StoreActions<SS> = SS extends Store<
- string,
- StateTree,
- _GettersTree<StateTree>,
- infer A
->
- ? A
- : _ExtractActionsFromSetupStore<SS>
+export type StoreActions<SS> =
+ SS extends Store<string, StateTree, _GettersTree<StateTree>, infer A>
+ ? A
+ : _ExtractActionsFromSetupStore<SS>
/**
* Extract the getters of a store type. Works with both a Setup Store or an
* Options Store.
*/
-export type StoreGetters<SS> = SS extends Store<
- string,
- StateTree,
- infer G,
- _ActionsTree
->
- ? _StoreWithGetters<G>
- : _ExtractGettersFromSetupStore<SS>
+export type StoreGetters<SS> =
+ SS extends Store<string, StateTree, infer G, _ActionsTree>
+ ? _StoreWithGetters<G>
+ : _ExtractGettersFromSetupStore<SS>
/**
* Extract the state of a store type. Works with both a Setup Store or an
* Options Store. Note this unwraps refs.
*/
-export type StoreState<SS> = SS extends Store<
- string,
- infer S,
- _GettersTree<StateTree>,
- _ActionsTree
->
- ? UnwrapRef<S>
- : _ExtractStateFromSetupStore<SS>
+export type StoreState<SS> =
+ SS extends Store<string, infer S, _GettersTree<StateTree>, _ActionsTree>
+ ? UnwrapRef<S>
+ : _ExtractStateFromSetupStore<SS>
// type a1 = _ExtractStateFromSetupStore<{ a: Ref<number>; action: () => void }>
// type a2 = _ExtractActionsFromSetupStore<{ a: Ref<number>; action: () => void }>
S extends StateTree = {},
G extends _GettersTree<S> = {},
// cannot extends ActionsTree because we loose the typings
- A /* extends ActionsTree */ = {}
+ A /* extends ActionsTree */ = {},
>(
id: Id,
options: Omit<DefineStoreOptions<Id, S, G, A>, 'id'>
S extends StateTree = {},
G extends _GettersTree<S> = {},
// cannot extends ActionsTree because we loose the typings
- A /* extends ActionsTree */ = {}
+ A /* extends ActionsTree */ = {},
>(options: DefineStoreOptions<Id, S, G, A>): StoreDefinition<Id, S, G, A>
/**
export type _Awaited<T> = T extends null | undefined
? T // special case for `null | undefined` when not in `--strictNullChecks` mode
: T extends object & { then(onfulfilled: infer F): any } // `await` only unwraps object types with a callable `then`. Non-object types are not unwrapped
- ? F extends (value: infer V, ...args: any) => any // if the argument to `then` is callable, extracts the first argument
- ? _Awaited<V> // recursively unwrap the value
- : never // the argument to `then` was not callable
- : T // non-object or non-thenable
+ ? F extends (value: infer V, ...args: any) => any // if the argument to `then` is callable, extracts the first argument
+ ? _Awaited<V> // recursively unwrap the value
+ : never // the argument to `then` was not callable
+ : T // non-object or non-thenable
/**
* Actual type for {@link StoreOnActionListenerContext}. Exists for refactoring
export interface _StoreOnActionListenerContext<
Store,
ActionName extends string,
- A
+ A,
> {
/**
* Name of the action
Id extends string,
S extends StateTree,
G /* extends GettersTree<S> */,
- A /* extends ActionsTree */
+ A /* extends ActionsTree */,
> = _ActionsTree extends A
? _StoreOnActionListenerContext<StoreGeneric, string, _ActionsTree>
: {
Id extends string,
S extends StateTree,
G /* extends GettersTree<S> */,
- A /* extends ActionsTree */
+ A /* extends ActionsTree */,
> = (
context: StoreOnActionListenerContext<
Id,
Id extends string,
S extends StateTree,
G /* extends GettersTree<StateTree> */,
- A /* extends ActionsTree */
+ A /* extends ActionsTree */,
> extends StoreProperties<Id> {
/**
* State of the Store. Setting it will internally call `$patch()` to update the state.
S extends StateTree = {},
G /* extends GettersTree<S>*/ = {},
// has the actions without the context (this) for typings
- A /* extends ActionsTree */ = {}
+ A /* extends ActionsTree */ = {},
> = _StoreWithState<Id, S, G, A> &
UnwrapRef<S> &
_StoreWithGetters<G> &
Id extends string = string,
S extends StateTree = StateTree,
G /* extends GettersTree<S>*/ = _GettersTree<S>,
- A /* extends ActionsTree */ = _ActionsTree
+ A /* extends ActionsTree */ = _ActionsTree,
> {
/**
* Returns a store, creates it if necessary.
Id extends string = string,
S extends StateTree = StateTree,
G /* extends GettersTree<S> */ = _GettersTree<S>,
- A /* extends ActionsTree */ = _ActionsTree
+ A /* extends ActionsTree */ = _ActionsTree,
> {}
/**
export type _ExtractStateFromSetupStore<SS> = SS extends undefined | void
? {}
: _ExtractStateFromSetupStore_Keys<SS> extends keyof SS
- ? _UnwrapAll<Pick<SS, _ExtractStateFromSetupStore_Keys<SS>>>
- : never
+ ? _UnwrapAll<Pick<SS, _ExtractStateFromSetupStore_Keys<SS>>>
+ : never
/**
* For internal use **only**
export type _ExtractActionsFromSetupStore<SS> = SS extends undefined | void
? {}
: _ExtractActionsFromSetupStore_Keys<SS> extends keyof SS
- ? Pick<SS, _ExtractActionsFromSetupStore_Keys<SS>>
- : never
+ ? Pick<SS, _ExtractActionsFromSetupStore_Keys<SS>>
+ : never
/**
* For internal use **only**
export type _ExtractGettersFromSetupStore<SS> = SS extends undefined | void
? {}
: _ExtractGettersFromSetupStore_Keys<SS> extends keyof SS
- ? Pick<SS, _ExtractGettersFromSetupStore_Keys<SS>>
- : never
+ ? Pick<SS, _ExtractGettersFromSetupStore_Keys<SS>>
+ : never
/**
* Options passed to `defineStore()` that are common between option and setup
Id extends string,
S extends StateTree,
G /* extends GettersTree<S> */,
- A /* extends Record<string, StoreAction> */
+ A /* extends Record<string, StoreAction> */,
> extends DefineStoreOptionsBase<S, Store<Id, S, G, A>> {
/**
* Unique string key to identify the store across the application.
// NOTE: Passing SS seems to make TS crash
S extends StateTree,
G,
- A /* extends ActionsTree */
+ A /* extends ActionsTree */,
> extends DefineStoreOptionsBase<S, Store<Id, S, G, A>> {
/**
* Extracted actions. Added by useStore(). SHOULD NOT be added by the user when
Id extends string,
S extends StateTree,
G,
- A
+ A,
> extends Omit<DefineStoreOptions<Id, S, G, A>, 'id' | 'actions'> {
/**
* Extracted object of actions. Added by useStore() when the store is built
}),
getters: {
items: (state) =>
- state.rawItems.reduce((items, item) => {
- const existingItem = items.find((it) => it.name === item)
+ state.rawItems.reduce(
+ (items, item) => {
+ const existingItem = items.find((it) => it.name === item)
- if (!existingItem) {
- items.push({ name: item, amount: 1 })
- } else {
- existingItem.amount++
- }
+ if (!existingItem) {
+ items.push({ name: item, amount: 1 })
+ } else {
+ existingItem.amount++
+ }
- return items
- }, [] as Array<{ name: string; amount: number }>),
+ return items
+ },
+ [] as Array<{ name: string; amount: number }>
+ ),
},
actions: {
addItem(name: string) {