2019-03-20 11:49:41 +01:00
|
|
|
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
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-21 09:11:44 +01:00
|
|
|
async commit(type, payload) {
|
2019-03-20 11:49:41 +01:00
|
|
|
if (!this.mutations[type]) {
|
|
|
|
|
throw new Error(`[Error] mutation ${type} is undefined`);
|
|
|
|
|
}
|
|
|
|
|
this._isMutating = true;
|
2019-03-21 09:11:44 +01:00
|
|
|
|
2019-03-20 11:49:41 +01:00
|
|
|
this.mutations[type].call(null, this._state, payload);
|
2019-03-21 09:11:44 +01:00
|
|
|
await Promise.resolve();
|
|
|
|
|
if (this._isMutating) {
|
|
|
|
|
this._isMutating = false;
|
|
|
|
|
this.trigger("update");
|
|
|
|
|
}
|
2019-03-20 11:49:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_clone(obj) {
|
|
|
|
|
return JSON.parse(JSON.stringify(obj));
|
|
|
|
|
}
|
|
|
|
|
}
|