From 5de3cbbe77ed2b442960c3e318879cbc19dbaa5b Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Tue, 20 Jul 2021 10:00:53 +0200 Subject: [PATCH] docs(hmr): document hmr code --- src/hmr.ts | 29 +++++++++++++++++++++++++++++ src/index.ts | 2 +- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/hmr.ts b/src/hmr.ts index 68b37b97..ab2e76d1 100644 --- a/src/hmr.ts +++ b/src/hmr.ts @@ -2,10 +2,25 @@ import { isRef, isReactive } from 'vue' import { Pinia } from './rootStore' import { isPlainObject, Store, StoreDefinition, _Method } from './types' +/** + * Checks if a function is a `StoreDefinition` + * + * @param fn - object to test + * @returns true if `fn` is a StoreDefinition + */ export const isUseStore = (fn: any): fn is StoreDefinition => { return typeof fn === 'function' && typeof fn.$id === 'string' } +/** + * Mutates in place `newState` with `oldState` to _hot update_ it. It will + * remove any key not existing in `newState` and recursively merge plain + * objects. + * + * @param newState - new state object to be patched + * @param oldState - old state that should be used to patch newState + * @returns - newState + */ export function patchObject( newState: Record, oldState: Record @@ -37,6 +52,20 @@ export function patchObject( return newState } +/** + * Creates an _accept_ function to pass to `import.meta.hot` in Vite applications. + * + * @example + * ```js + * const useUser = defineStore(...) + * if (import.meta.hot) { + * import.meta.hot.accept(acceptHMRUpdate(useUser, import.meta.hot)) + * } + * ``` + * + * @param initialUseStore - return of the defineStore to hot update + * @param hot - `import.meta.hot` + */ export function acceptHMRUpdate( initialUseStore: StoreDefinition, hot: any diff --git a/src/index.ts b/src/index.ts index a94b847e..06ef34ae 100644 --- a/src/index.ts +++ b/src/index.ts @@ -55,4 +55,4 @@ export type { export { createTestingPinia } from './testing' export type { TestingOptions } from './testing' -export * from './hmr' +export { acceptHMRUpdate } from './hmr' -- 2.47.2