mirror of
https://github.com/odoo/owl.git
synced 2025-10-06 19:59:41 +07:00
[IMP] store: commit a mutation in a mutation
This commit is contained in:
committed by
Géry Debongnie
parent
9ee85e220b
commit
7e7226213a
+26
-16
@@ -21,6 +21,7 @@ export class Store extends EventBus {
|
|||||||
state: any;
|
state: any;
|
||||||
actions: any;
|
actions: any;
|
||||||
mutations: any;
|
mutations: any;
|
||||||
|
_commitLevel: number = 0;
|
||||||
_isMutating: boolean = false;
|
_isMutating: boolean = false;
|
||||||
history: any[] = [];
|
history: any[] = [];
|
||||||
debug: boolean;
|
debug: boolean;
|
||||||
@@ -64,35 +65,44 @@ export class Store extends EventBus {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async commit(type, payload?: any) {
|
commit(type, payload?: any) {
|
||||||
if (!this.mutations[type]) {
|
if (!this.mutations[type]) {
|
||||||
throw new Error(`[Error] mutation ${type} is undefined`);
|
throw new Error(`[Error] mutation ${type} is undefined`);
|
||||||
}
|
}
|
||||||
|
this._commitLevel++;
|
||||||
const currentRev = this.observer.rev;
|
const currentRev = this.observer.rev;
|
||||||
|
|
||||||
this._isMutating = true;
|
this._isMutating = true;
|
||||||
this.observer.allowMutations = true;
|
this.observer.allowMutations = true;
|
||||||
|
|
||||||
this.mutations[type].call(
|
this.mutations[type].call(
|
||||||
null,
|
null,
|
||||||
{ state: this.state, set: this.observer.set },
|
{
|
||||||
|
commit: this.commit.bind(this),
|
||||||
|
state: this.state,
|
||||||
|
set: this.observer.set
|
||||||
|
},
|
||||||
payload
|
payload
|
||||||
);
|
);
|
||||||
this.observer.allowMutations = false;
|
|
||||||
|
|
||||||
if (this.debug) {
|
if (this._commitLevel === 1) {
|
||||||
this.history.push({
|
this.observer.allowMutations = false;
|
||||||
state: this.state,
|
if (this.debug) {
|
||||||
mutation: type,
|
this.history.push({
|
||||||
payload: payload
|
state: this.state,
|
||||||
|
mutation: type,
|
||||||
|
payload: payload
|
||||||
|
});
|
||||||
|
}
|
||||||
|
Promise.resolve().then(() => {
|
||||||
|
if (this._isMutating) {
|
||||||
|
this._isMutating = false;
|
||||||
|
if (currentRev !== this.observer.rev) {
|
||||||
|
this.trigger("update", this.state);
|
||||||
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
await Promise.resolve();
|
this._commitLevel--;
|
||||||
if (this._isMutating) {
|
|
||||||
this._isMutating = false;
|
|
||||||
if (currentRev !== this.observer.rev) {
|
|
||||||
this.trigger("update", this.state);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -76,6 +76,25 @@ describe("basic use", () => {
|
|||||||
expect(store.state.n).toBe(101);
|
expect(store.state.n).toBe(101);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("can commit a mutation in a mutation", () => {
|
||||||
|
const state = { n: 1 };
|
||||||
|
const mutations = {
|
||||||
|
inc({ state }) {
|
||||||
|
state.n++;
|
||||||
|
},
|
||||||
|
inc10({ commit }) {
|
||||||
|
for (let i = 0; i < 10; i++) {
|
||||||
|
commit("inc");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
const store = new Store({ state, mutations });
|
||||||
|
|
||||||
|
expect(store.state.n).toBe(1);
|
||||||
|
store.commit("inc10");
|
||||||
|
expect(store.state.n).toBe(11);
|
||||||
|
});
|
||||||
|
|
||||||
test("dispatch allow synchronizing between actions", async () => {
|
test("dispatch allow synchronizing between actions", async () => {
|
||||||
const state = { n: 1 };
|
const state = { n: 1 };
|
||||||
const mutations = {
|
const mutations = {
|
||||||
|
|||||||
Reference in New Issue
Block a user