add store class to core

This commit is contained in:
Géry Debongnie
2019-03-20 11:49:41 +01:00
parent 6a2e4dbf2f
commit c9796e6bdc
2 changed files with 65 additions and 1 deletions
+2 -1
View File
@@ -1,5 +1,6 @@
import { QWeb } from "./qweb";
import { EventBus } from "./event_bus";
import { Component } from "./component";
import { Store, StoreMixin } from "./store";
export const core = { QWeb, EventBus, Component };
export const core = { QWeb, EventBus, Component, Store, StoreMixin };
+63
View File
@@ -0,0 +1,63 @@
import { EventBus } from "./event_bus";
export function StoreMixin(Component) {
return class extends Component {
mounted() {
this.env.store.on("update", this, this.render);
}
};
}
interface StoreConfig {
state?: any;
actions?: any;
mutations?: any;
}
export class Store extends EventBus {
_state: any;
actions: any;
mutations: any;
_isMutating: boolean = false;
constructor(config: StoreConfig = {}) {
super();
this._state = Object.assign({}, config.state);
this.actions = config.actions;
this.mutations = config.mutations;
}
get state() {
return this._clone(this._state);
}
dispatch(action, payload) {
if (!this.actions[action]) {
throw new Error(`[Error] action ${action} is undefined`);
}
this.actions[action](
{
commit: this.commit.bind(this),
state: this.state
},
payload
);
}
commit(type, payload) {
if (!this.mutations[type]) {
throw new Error(`[Error] mutation ${type} is undefined`);
}
this._isMutating = true;
this.mutations[type].call(null, this._state, payload);
Promise.resolve().then(() => {
if (this._isMutating) {
this._isMutating = false;
this.trigger("update");
}
});
}
_clone(obj) {
return JSON.parse(JSON.stringify(obj));
}
}