vue-vuex-promise-store
Wraps promises to save resolved data to vuex store.
Usage
const store = plugins: PromiseStore const uniqueKey = 'unique key is required' // first time for the unique key { const context = storegetters'promise/init'uniqueKey { console } console // context.promise is a Promise const last = await contextpromise console // '!'} await { // second time for the unique key const context = storegetters'promise/init'uniqueKey { // a result for the `uniqueKey` is already in the store. // this promise executor is not invoked, // and following thenSync()s are executed synchronously. } console const last = await ppromise console // 'HELLO'}
Usage (resolve, reject, wrap)
const store = plugins: PromiseStore let cache = null { if useCache && cache return cachestatus ? : const cb = if ignoreStore return return storegetters'promise/init''remoteData' cb }
API
Types
type OnFulfilledSync<T, U> = (value: T) => U
type OnRejectedSync<T> = (reason: Error) => T
type CatchSync<T> = (onRejected: OnRejectedSync<T>) => Context<T>
type ThenSync<T> = (onFulfilled: OnFulfilled<T>, onRejected?: OnRejected<T>) => Context<T>
type Context<T> = {
isFulfilled: boolean, // true if or when the promise is fulfilled
isPending: boolean, // ditto but is nether fulfilled nor rejected
isRejected: boolean, // ditto but is rejected
promise: Promise<T> | void,
reason: Error | void, // the result of the rejected promise
value: T | void, // the result of the fulfilled promise
catchSync<U>: CatchSync<U> | void
thenSync<U>: ThenSync<U> | void
}
type Resolve<T> = (value: T) => void
type Reject = (reason: Error) => void
type Executor<T> = (resolve: Resolve<T>, reject?: Reject) => void
type PromiseOrExecutor<T> = Promise<T> | Executor<T>
type ContextOptions = {
refresh?: boolean
}
type PluginOptions = {
moduleName?: string
}
type PluginInstaller = (store: Vuex.Store) => void
Exports
MODULE_NAME: string
- is the default module name (is
'promise'
).
- is the default module name (is
VERSION: string
- is the version number (like
'1.0.0'
).
- is the version number (like
plugin: (options?: PluginOptions) => PluginInstaller
- returns a plugin installer function with given options.
reject: (reason: Error) => Context<void>
resolve: (value: T) => Context<T>
wrap: (promise: Promise<T>) => Context<T>
- returns a new
Context
object without stores.
- returns a new
State
contexts: { [string]: Context }
Context
objects.
disabled: boolean
- true if store binding is disabled.
- @see
disable()
action
Actions
enable() => Promise<void>
- enables the store binding.
disable() => Promise<void>
- disables the store binding.
- After disabling the store binding,
Context
objects are not stored instate.contexts
.
finalize() => Promise<void>
- resolves all
Context
objects instate.contexts
, then setpromise
catchSync
thenSync
toundefined
. - Use this action to serialize
state
.
- resolves all
resolveAll() => Promise<void>
- resolves all pending
Context
objects.
- resolves all pending
Getters
hasPendingPromises: boolean
- true if any pending
Context
objects are instate.contexts
.
- true if any pending
init: (key: string, promiseOrExecutor: PromiseOrExecutor<T>, options?: ContextOptions) => Context<T>
- is a function creates a new
Context
object, stores it instate.contexts
with a givenkey
if the store binding is not disabled, and finally returns it. - If a given
key
exists instate.contexts
then returns it (instead of creating and storing a new object).
- is a function creates a new
pendingPromises: Array<Context>
- is an array of pending
Context
objects instate.contexts
.
- is an array of pending