Skip to content

Commit 878d3dd

Browse files
committed
fixed shared state issues for modules
1 parent 931d41d commit 878d3dd

File tree

8 files changed

+39
-27
lines changed

8 files changed

+39
-27
lines changed

dist/proxy.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/proxy.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/proxy.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/proxy.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/actions.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export function getRawActionContext<T extends VuexModule, R>( thisArg:ThisType<T
3636

3737
function handleMutateActionMode(target:VuexModule, key:string, descriptor:ActionDescriptor) {
3838

39+
console.log( ">>", target )
3940

4041
initializeActionsCache( target );
4142

src/module.ts

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,23 @@ export function createModule( options ?:VuexModuleOptions ) {
1616
* options variable as it is an internal variable.
1717
*/
1818
(VuexModule as VuexModuleConstructor).prototype.__options__ = options;
19-
(VuexModule as VuexModuleConstructor).prototype.__namespacedPath__ = "";
20-
(VuexModule as VuexModuleConstructor).prototype.__vuex_module_cache__ = undefined;
21-
(VuexModule as VuexModuleConstructor).prototype.__vuex_proxy_cache__ = undefined;
22-
(VuexModule as VuexModuleConstructor).prototype.__vuex_local_proxy_cache__ = undefined;
23-
(VuexModule as VuexModuleConstructor).prototype.__submodules_cache__ = {};
24-
(VuexModule as VuexModuleConstructor).prototype.__context_store__ = {};
25-
(VuexModule as VuexModuleConstructor).prototype.__mutations_cache__ = {
26-
__explicit_mutations__: {},
27-
__setter_mutations__: {}
28-
};
29-
(VuexModule as VuexModuleConstructor).prototype.__explicit_mutations_names__ = [];
30-
(VuexModule as VuexModuleConstructor).prototype.__actions__ = [];
31-
(VuexModule as VuexModuleConstructor).prototype.__watch__ = {};
32-
(VuexModule as VuexModuleConstructor).prototype.__explicit_getter_names__ = [];
33-
(VuexModule as VuexModuleConstructor).prototype.__decorator_getter_names__ = [];
3419

3520
return VuexModule;
3621

3722
}
3823

24+
25+
function initializeModuleInternals( cls: VuexModuleConstructor ) {
26+
cls.prototype.__namespacedPath__ = "";
27+
cls.prototype.__vuex_module_cache__ = undefined;
28+
cls.prototype.__vuex_proxy_cache__ = undefined;
29+
cls.prototype.__vuex_local_proxy_cache__ = undefined;
30+
cls.prototype.__submodules_cache__ = {};
31+
cls.prototype.__context_store__ = {};
32+
cls.prototype.__watch__ = {};
33+
cls.prototype.__explicit_getter_names__ = [];
34+
}
35+
3936
export function extractVuexModule( cls :typeof VuexModule ) {
4037

4138
const VuexClass = cls as VuexModuleConstructor;
@@ -46,13 +43,20 @@ export function extractVuexModule( cls :typeof VuexModule ) {
4643
return VuexClass.prototype.__vuex_module_cache__;
4744
}
4845

46+
initializeModuleInternals( VuexClass );
47+
4948
// If not extract vuex module from class.
5049
const fromInstance = extractModulesFromInstance( VuexClass );
5150
const fromPrototype = extractModulesFromPrototype( VuexClass );
5251

52+
// console.log( "Class Name", VuexClass, VuexClass.name, VuexClass.prototype.__mutations_cache__ )
53+
// console.log( "Mutation Names", VuexClass.name, VuexClass.prototype.__actions__ )
54+
5355
// Cache explicit mutations and getter mutations.
54-
VuexClass.prototype.__mutations_cache__.__explicit_mutations__ = fromPrototype.mutations.explicitMutations;
55-
VuexClass.prototype.__mutations_cache__.__setter_mutations__ = fromPrototype.mutations.setterMutations;
56+
VuexClass.prototype.__mutations_cache__ = {
57+
__explicit_mutations__: fromPrototype.mutations.explicitMutations,
58+
__setter_mutations__: fromPrototype.mutations.setterMutations,
59+
}
5660
const className = VuexClass.name.toLowerCase();
5761

5862
const vuexModule :VuexObject = {
@@ -69,7 +73,7 @@ export function extractVuexModule( cls :typeof VuexModule ) {
6973

7074
const rtn = { [ path ]: vuexModule }
7175
VuexClass.prototype.__vuex_module_cache__ = rtn;
72-
76+
7377
return rtn;
7478

7579
}
@@ -260,7 +264,7 @@ function extractModulesFromPrototype( cls :VuexModuleConstructor ) {
260264

261265
}
262266

263-
function extractDecoratorGetterNames( names :string[] ) {
267+
function extractDecoratorGetterNames( names :string[] = [] ) {
264268
const decorator :Map = {};
265269
for( let name of names ) {
266270
decorator[ name ] = new Function("state", `return state.${name}`);

src/mutations.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import { Map, FieldPayload, MutationDescriptor, VuexModuleInternalsPrototype } f
66
export function mutation( target:any, key:string, descriptor:MutationDescriptor ) {
77
// Just store the name of the mutation.
88
initializeExplicitMutationsCache( target );
9-
109
(target as VuexModuleInternalsPrototype).__explicit_mutations_names__.push( key );
1110
}
1211

@@ -56,7 +55,15 @@ export const internalMutator = ( state :Map, { field, payload } :FieldPayload )
5655
}
5756

5857
function initializeExplicitMutationsCache(target :any) {
59-
if( ( target as VuexModuleInternalsPrototype).__explicit_mutations_names__ === undefined ) {
60-
(target as VuexModuleInternalsPrototype).__explicit_mutations_names__ = [];
58+
const cls = target as VuexModuleInternalsPrototype;
59+
if ( cls.__explicit_mutations_names__ === undefined ) {
60+
cls.__explicit_mutations_names__ = [];
61+
}
62+
63+
if( cls.__mutations_cache__ === undefined ) {
64+
cls.__mutations_cache__ = {
65+
__explicit_mutations__: {},
66+
__setter_mutations__: {},
67+
}
6168
}
6269
}

src/proxy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ function createGettersAndGetterMutationsProxy({ cls, getters, mutations, proxy,
451451

452452
for( let field in getters ) {
453453

454-
if( $store === undefined ) continue;
454+
if( $store === undefined || proxy[ field ] ) continue;
455455

456456
const fieldHasGetterAndMutation = getterMutations.indexOf( field ) > -1;
457457
if( fieldHasGetterAndMutation ) {

0 commit comments

Comments
 (0)