Skip to content

Commit df6a0e6

Browse files
committed
Fixed most implementation remaining sub modules proxy
1 parent 307a73b commit df6a0e6

File tree

3 files changed

+24
-8
lines changed

3 files changed

+24
-8
lines changed

src/interfaces.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export interface VuexModuleInternals {
1919
__explicit_mutations__: {},
2020
__setter_mutations__: {},
2121
};
22+
__context_store__: Map | undefined;
2223
}
2324
}
2425

src/module.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export function initializeStore( options ?:VuexModuleOptions ) {
1414
(VuexModule as VuexModuleConstructor).prototype.__vuex_module_cache__ = undefined;
1515
(VuexModule as VuexModuleConstructor).prototype.__vuex_proxy_cache__ = undefined;
1616
(VuexModule as VuexModuleConstructor).prototype.__vuex_local_proxy_cache__ = undefined;
17+
(VuexModule as VuexModuleConstructor).prototype.__context_store__ = undefined;
1718
(VuexModule as VuexModuleConstructor).prototype.__mutations_cache__ = {
1819
__explicit_mutations__: {},
1920
__setter_mutations__: {}
@@ -88,7 +89,7 @@ function extractFromInstance( cls :VuexModuleConstructor ) {
8889

8990
// Check if field is an explicit mutation.
9091
if( typeof instance[ field ] === "function" ) {
91-
mutations[ field ] = instance[ field ];
92+
mutations[ field ] = ( state :any, payload :any ) => instance[ field ].call( state, payload );
9293
continue;
9394
}
9495

@@ -134,6 +135,7 @@ function extractFromPrototype( cls :VuexModuleConstructor ) {
134135
if( typeof descriptor.value === "function" ) {
135136
const func = descriptor.value as Function
136137
actions[ field ] = function( context :any, payload :any ) {
138+
cls.prototype.__context_store__ = context;
137139
const proxy = createLocalProxy( cls, context );
138140
return func.call( proxy, payload )
139141
}

src/proxy.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ export function _createProxy<T>(cls: T, $store: any, namespacedPath = "") {
4545
const proxy = {};
4646
const { state, mutations, actions, getters } = extractModule(VuexClass);
4747

48-
createGettersAndMutationProxyFromState( proxy, state, $store, namespacedPath );
48+
createGettersAndMutationProxyFromState({ cls: VuexClass, proxy, state, $store, namespacedPath });
4949
createExplicitMutationsProxy({ cls: VuexClass, proxy, $store, namespacedPath });
50-
createGettersAndGetterMutationsProxy({ cls: VuexClass, getters, proxy, $store, namespacedPath });
50+
createGettersAndGetterMutationsProxy({ cls: VuexClass, mutations, getters, proxy, $store, namespacedPath });
5151
createActionProxy({ actions, proxy, $store, namespacedPath });
5252

5353
runSetterCheck( VuexClass, getters )
@@ -56,7 +56,7 @@ export function _createProxy<T>(cls: T, $store: any, namespacedPath = "") {
5656
return proxy as InstanceType<T>;
5757
}
5858

59-
function createGettersAndMutationProxyFromState(proxy :Map, state :Map, $store :any, namespacedPath = "", currentField = "") {
59+
function createGettersAndMutationProxyFromState({ cls, proxy, state, $store, namespacedPath = "", currentField = "" }: { cls: VuexModuleConstructor, proxy: Map; state: Map; $store: any; namespacedPath?: string; currentField?: string; }) {
6060
/**
6161
* 1. Go through all fields in the object and check the values of those fields.
6262
*
@@ -84,14 +84,21 @@ function createGettersAndMutationProxyFromState(proxy :Map, state :Map, $store :
8484
if( $store.getters ) return $store.getters[ namespacedPath + "__internal_getter__" ]( path )
8585
else return $store[ "__internal_getter__" ]( path )
8686
},
87-
set: payload => $store.commit( namespacedPath + "__internal_mutator__", { field: path, payload }),
87+
set: payload => {
88+
if( $store.commit ) $store.commit( namespacedPath + "__internal_mutator__", { field: path, payload });
89+
else {
90+
// We must be creating local proxies hence, $store.commit doesn't exist
91+
const store = cls.prototype.__context_store__!;
92+
store.commit( "__internal_mutator__", { field: path, payload })
93+
}
94+
},
8895
})
8996

9097
continue;
9198
}
9299

93100
proxy[field] = {};
94-
createGettersAndMutationProxyFromState(proxy[field], value, $store, namespacedPath, currentField + field);
101+
createGettersAndMutationProxyFromState({ proxy: proxy[field], state: value, $store, namespacedPath, currentField: currentField + field });
95102

96103
}
97104

@@ -106,15 +113,20 @@ function createExplicitMutationsProxy({ cls, proxy, $store, namespacedPath } :Mu
106113
}
107114
}
108115

109-
function createGettersAndGetterMutationsProxy({ cls, getters, proxy, $store, namespacedPath } :GetterProxyCreator) {
116+
function createGettersAndGetterMutationsProxy({ cls, getters, mutations, proxy, $store, namespacedPath } :GetterProxyCreator) {
110117

111118
const getterMutations = Object.keys( cls.prototype.__mutations_cache__.__setter_mutations__ );
112-
113119
// If there are defined setter mutations that do not have a corresponding getter,
114120
// throw an error.
121+
if( $store.__internal_getter__ ) {
122+
$store.__internal_mutator__ = mutations.__internal_mutator__;
123+
}
124+
125+
console.log( "proxy <<", $store );
115126

116127
for( let field in getters ) {
117128

129+
118130
const fieldHasGetterAndMutation = getterMutations.indexOf( field ) > -1;
119131
if( fieldHasGetterAndMutation ) {
120132

@@ -188,6 +200,7 @@ interface MutationProxyCreator extends ProxyCreator {
188200

189201
interface GetterProxyCreator extends MutationProxyCreator {
190202
getters :Map;
203+
mutations :Map;
191204
}
192205

193206
interface ActionProxyCreator extends ProxyCreator {

0 commit comments

Comments
 (0)