imp: store dispatch functions can be synchronized

This commit is contained in:
Géry Debongnie
2019-04-09 13:16:22 +02:00
parent 94c33af8e9
commit 816ffe1b44
2 changed files with 37 additions and 2 deletions
+8 -2
View File
@@ -45,11 +45,11 @@ export class Store extends EventBus {
}
}
dispatch(action, payload?: any) {
dispatch(action, payload?: any): Promise<void> | void {
if (!this.actions[action]) {
throw new Error(`[Error] action ${action} is undefined`);
}
this.actions[action](
const result = this.actions[action](
{
commit: this.commit.bind(this),
dispatch: this.dispatch.bind(this),
@@ -58,6 +58,12 @@ export class Store extends EventBus {
},
payload
);
if (result instanceof Promise) {
return new Promise((resolve, reject) => {
result.then(() => resolve());
result.catch(reject);
});
}
}
async commit(type, payload?: any) {