Skip to content

Commit

Permalink
fix: apply mutate flag fix from 2.x
Browse files Browse the repository at this point in the history
  • Loading branch information
temper357 committed Jul 15, 2019
1 parent ed13d50 commit b07fc67
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ export default class Store {
// flag of whether state changed after mutation
private stateChanged = false;

/** Flag of how many actions are in execution */
private actionExecNum = 0;

public constructor(bindings: object) {
Object.keys(bindings).forEach((key) => {
const value = bindings[key];
Expand All @@ -27,7 +30,7 @@ export default class Store {

const handler = {
set: (target, prop, value) => {
if (!this.allowMutate) {
if (!this.actionExecNum) {
console.error('Forbid modifying state directly, use action to modify instead.');
return false;
}
Expand All @@ -50,12 +53,12 @@ export default class Store {
*/
private createAction(fun): MethodFunc {
return async (...args) => {
this.allowMutate = true;
this.actionExecNum += 1;
await fun.apply(this.bindings, args);
if (this.stateChanged) {
this.setState();
}
this.allowMutate = false;
this.actionExecNum -= 1;
this.stateChanged = false;
return this.bindings;
};
Expand Down

0 comments on commit b07fc67

Please sign in to comment.