Skip to content

Commit

Permalink
feat: added unregister and register methods
Browse files Browse the repository at this point in the history
  • Loading branch information
spb-web committed Dec 11, 2020
1 parent 6be3828 commit 6e02c65
Showing 1 changed file with 49 additions and 7 deletions.
56 changes: 49 additions & 7 deletions src/vuexok.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@ export interface ModuleInstance<M extends Module<any, any>> {
mutations: ModuleMutations<NonUndefined<M['mutations']>>,
getters: ModuleGetters<NonUndefined<M['getters']>>,
modules: ModuleSubmodules<M['modules']>,
/**
* 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: <T extends (
state:ModuleState<M['state']>,
getters:ModuleGetters<NonUndefined<M['getters']>>
Expand All @@ -81,6 +90,18 @@ export interface ModuleInstance<M extends Module<any, any>> {
callback: (value: ReturnType<T>, oldValue: ReturnType<T>) => 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 = <
Expand Down Expand Up @@ -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<any>|null = null

// export const vuexokUseStore = <R>(store:Store<R>) => {
// if (vuexStore) {
// throw new Error()
// }

// vuexStore = store
// }

export const createModule = <
S, R, M extends Module<S, R>
>(
Expand All @@ -176,20 +224,14 @@ export const createModule = <
moduleRaw: M extends Module<S, R> ? M : Module<S, R>,
moduleOptions?: ModuleOptions
) => {
store.registerModule(
path,
moduleRaw,
moduleOptions,
)

const module = buildModuleObject<
S,
R,
typeof moduleRaw & M
// @ts-ignore
>(store, path, moduleRaw)

modules.set(path, module)
module.register(moduleOptions)

return module
}

0 comments on commit 6e02c65

Please sign in to comment.