|
| 1 | + |
1 | 2 | import { getMutatedActions as getProxiedActions, ActionRegister } from "./actions"; |
2 | | -import { _state, _mutations, _getters, _proxy, _map, _store, _namespacedPath, _actions_register, _actions, MutationFunction, GetterFunction, ActionFunction, VuexMap, _submodule, SubModuleObject, _module } from "./symbols"; |
| 3 | +import { _state, _mutations, _getters, _proxy, _map, _store, _namespacedPath, _actions_register, _actions, MutationFunction, GetterFunction, ActionFunction, VuexMap, _submodule, SubModuleObject, _module, _target } from "./symbols"; |
3 | 4 | //@ts-ignore |
4 | 5 | import { Store } from "vuex"; |
5 | 6 |
|
6 | 7 | export type VuexClassConstructor<T> = new () => T |
7 | 8 |
|
8 | | -export type VuexModuleTarget = "core" | "nuxt"; |
9 | | - |
10 | 9 | export class VuexModule { |
11 | 10 |
|
12 | 11 | static CreateSubModule<V extends typeof VuexModule>(SubModule: V) { |
@@ -132,44 +131,51 @@ export interface VuexModule { |
132 | 131 | [_module]: Record<string, any>; |
133 | 132 | } |
134 | 133 |
|
| 134 | +export type VuexModuleTarget = "core" | "nuxt"; |
| 135 | + |
| 136 | +interface ModuleOptions { |
| 137 | + namespacedPath? :string; |
| 138 | + target? :VuexModuleTarget |
| 139 | +} |
| 140 | + |
135 | 141 |
|
136 | | -export function Module({ namespacedPath = "" }) { |
| 142 | +export function Module({ namespacedPath = "", target = "core" } :ModuleOptions ) { |
137 | 143 |
|
138 | | - return function( target :typeof VuexModule ) :void { |
139 | | - const targetInstance = new target(); |
| 144 | + return function( _module :typeof VuexModule ) :void { |
| 145 | + const targetInstance = new _module(); |
140 | 146 |
|
141 | 147 | const states = Object.getOwnPropertyNames( targetInstance ); |
142 | 148 | const stateObj: Record<string, any> = {} |
143 | | - if( target.prototype[ _map ] === undefined ) target.prototype[ _map ] = []; |
| 149 | + if( _module.prototype[ _map ] === undefined ) _module.prototype[ _map ] = []; |
144 | 150 |
|
145 | 151 | for( let stateField of states ) { |
146 | 152 | const stateValue = targetInstance[ stateField ]; |
147 | 153 | if ( stateValue === undefined ) continue; |
148 | 154 |
|
149 | 155 | if ( subModuleObjectIsFound( stateValue )) { |
150 | | - handleSubModule( target, stateField, stateValue ) |
| 156 | + handleSubModule( _module, stateField, stateValue ) |
151 | 157 | continue; |
152 | 158 | } |
153 | 159 | stateObj[ stateField ] = stateValue; |
154 | | - target.prototype[ _map ].push({ value: stateField, type: "state" }); |
| 160 | + _module.prototype[ _map ].push({ value: stateField, type: "state" }); |
155 | 161 | } |
156 | 162 |
|
157 | | - target.prototype[ _state ] = stateObj; |
| 163 | + _module.prototype[ _state ] = stateObj; |
158 | 164 |
|
159 | | - const fields = Object.getOwnPropertyDescriptors( target.prototype ); |
160 | | - if ( target.prototype[ _getters ] === undefined ) target.prototype[ _getters ] = {} |
| 165 | + const fields = Object.getOwnPropertyDescriptors( _module.prototype ); |
| 166 | + if ( _module.prototype[ _getters ] === undefined ) _module.prototype[ _getters ] = {} |
161 | 167 | for (let field in fields) { |
162 | 168 | const getterField = fields[ field ].get; |
163 | 169 | if ( getterField ) { |
164 | 170 | const func = function (state: any) { |
165 | 171 | return getterField.call(state); |
166 | 172 | } |
167 | | - target.prototype[_getters][field] = func; |
| 173 | + _module.prototype[_getters][field] = func; |
168 | 174 | } |
169 | 175 | } |
170 | 176 |
|
171 | | - if ( namespacedPath.length > 0 ) target.prototype[ _namespacedPath ] = namespacedPath; |
172 | | - |
| 177 | + if ( namespacedPath.length > 0 ) _module.prototype[ _namespacedPath ] = namespacedPath; |
| 178 | + _module[ _target ] = target; |
173 | 179 | } |
174 | 180 | } |
175 | 181 |
|
|
0 commit comments