diff --git a/src/vuexok.ts b/src/vuexok.ts index 541f0d3..cd4e7d2 100644 --- a/src/vuexok.ts +++ b/src/vuexok.ts @@ -73,6 +73,15 @@ export interface ModuleInstance> { mutations: ModuleMutations>, getters: ModuleGetters>, modules: ModuleSubmodules, + /** + * Reactively watch fn's return value, and call the callback when + * the value changes. fn receives the store's state as the first + * argument, and getters as the second argument. Accepts an + * optional options object that takes the same options as Vue's + * vm.$watch method (opens new window). + * + * To stop watching, call the returned unwatch function. + */ watch: , getters:ModuleGetters> @@ -81,6 +90,18 @@ export interface ModuleInstance> { callback: (value: ReturnType, oldValue: ReturnType) => void, options?: WatchOptions ) => Unwatch + /** + * You may check if the module is already registered to the store or not via hasModule method. + */ + hasModule: () => boolean, + /** + * You can remove a registered module with unregister method. + */ + unregister: () => void, + /** + * + */ + register: (moduleOptions?: ModuleOptions) => void, } const helperReduce = < @@ -162,12 +183,39 @@ export const buildModuleObject = < callback, options, ) + }, + hasModule() { + return store.hasModule(path) + }, + unregister() { + store.unregisterModule(path) + }, + register(moduleOptions) { + store.registerModule( + path, + moduleRaw, + moduleOptions, + ) } } + modules.set(path, module) + return module } +// Взможно лучше сделать так: +// +// let vuexStore:Store|null = null + +// export const vuexokUseStore = (store:Store) => { +// if (vuexStore) { +// throw new Error() +// } + +// vuexStore = store +// } + export const createModule = < S, R, M extends Module >( @@ -176,12 +224,6 @@ export const createModule = < moduleRaw: M extends Module ? M : Module, moduleOptions?: ModuleOptions ) => { - store.registerModule( - path, - moduleRaw, - moduleOptions, - ) - const module = buildModuleObject< S, R, @@ -189,7 +231,7 @@ export const createModule = < // @ts-ignore >(store, path, moduleRaw) - modules.set(path, module) + module.register(moduleOptions) return module }