mirror of
https://github.com/odoo/owl.git
synced 2025-10-06 19:59:41 +07:00
imp: store dispatch functions can be synchronized
This commit is contained in:
+8
-2
@@ -45,11 +45,11 @@ export class Store extends EventBus {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
dispatch(action, payload?: any) {
|
dispatch(action, payload?: any): Promise<void> | void {
|
||||||
if (!this.actions[action]) {
|
if (!this.actions[action]) {
|
||||||
throw new Error(`[Error] action ${action} is undefined`);
|
throw new Error(`[Error] action ${action} is undefined`);
|
||||||
}
|
}
|
||||||
this.actions[action](
|
const result = this.actions[action](
|
||||||
{
|
{
|
||||||
commit: this.commit.bind(this),
|
commit: this.commit.bind(this),
|
||||||
dispatch: this.dispatch.bind(this),
|
dispatch: this.dispatch.bind(this),
|
||||||
@@ -58,6 +58,12 @@ export class Store extends EventBus {
|
|||||||
},
|
},
|
||||||
payload
|
payload
|
||||||
);
|
);
|
||||||
|
if (result instanceof Promise) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
result.then(() => resolve());
|
||||||
|
result.catch(reject);
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async commit(type, payload?: any) {
|
async commit(type, payload?: any) {
|
||||||
|
|||||||
@@ -63,6 +63,35 @@ describe("basic use", () => {
|
|||||||
expect(store.state.n).toBe(101);
|
expect(store.state.n).toBe(101);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("dispatch allow synchronizing between actions", async () => {
|
||||||
|
const state = { n: 1 };
|
||||||
|
const mutations = {
|
||||||
|
inc(state, delta) {
|
||||||
|
state.n += delta;
|
||||||
|
},
|
||||||
|
setN(state, n) {
|
||||||
|
state.n = n;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
const actions = {
|
||||||
|
async dosomething({ commit, dispatch }) {
|
||||||
|
await dispatch("setTo10");
|
||||||
|
commit("inc", 3);
|
||||||
|
},
|
||||||
|
async setTo10({ commit }) {
|
||||||
|
await Promise.resolve();
|
||||||
|
commit("setN", 10);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
const store = new Store({ state, mutations, actions });
|
||||||
|
|
||||||
|
expect(store.state.n).toBe(1);
|
||||||
|
store.dispatch("dosomething");
|
||||||
|
expect(store.state.n).toBe(1);
|
||||||
|
await nextTick();
|
||||||
|
expect(store.state.n).toBe(13);
|
||||||
|
});
|
||||||
|
|
||||||
test("env is given to actions", () => {
|
test("env is given to actions", () => {
|
||||||
expect.assertions(1);
|
expect.assertions(1);
|
||||||
const someEnv = {};
|
const someEnv = {};
|
||||||
|
|||||||
Reference in New Issue
Block a user