mirror of
https://github.com/odoo/owl.git
synced 2025-10-06 19:59:41 +07:00
imp: add env to store dispatch
This is useful to allow actions to use specific environment methods. For example, one might want to add a rpc method to the environment.
This commit is contained in:
committed by
Géry Debongnie
parent
d10426c720
commit
7027ff1eb6
+6
-2
@@ -49,6 +49,7 @@ export function connect(mapStateToProps) {
|
||||
}
|
||||
|
||||
interface StoreConfig {
|
||||
env?: any;
|
||||
state?: any;
|
||||
actions?: any;
|
||||
mutations?: any;
|
||||
@@ -64,6 +65,7 @@ export class Store extends EventBus {
|
||||
_isMutating: boolean = false;
|
||||
history: any[] = [];
|
||||
debug: boolean;
|
||||
env: any;
|
||||
|
||||
constructor(config: StoreConfig, options: StoreOption = {}) {
|
||||
super();
|
||||
@@ -71,6 +73,7 @@ export class Store extends EventBus {
|
||||
this._state = Object.assign({}, config.state);
|
||||
this.actions = config.actions;
|
||||
this.mutations = config.mutations;
|
||||
this.env = config.env;
|
||||
|
||||
if (this.debug) {
|
||||
this.history.push({ state: this.state });
|
||||
@@ -81,14 +84,15 @@ export class Store extends EventBus {
|
||||
return this._clone(this._state);
|
||||
}
|
||||
|
||||
dispatch(action, payload) {
|
||||
dispatch(action, payload?: any) {
|
||||
if (!this.actions[action]) {
|
||||
throw new Error(`[Error] action ${action} is undefined`);
|
||||
}
|
||||
this.actions[action](
|
||||
{
|
||||
commit: this.commit.bind(this),
|
||||
state: this.state
|
||||
state: this.state,
|
||||
env: this.env
|
||||
},
|
||||
payload
|
||||
);
|
||||
|
||||
@@ -41,6 +41,19 @@ describe("basic use", () => {
|
||||
expect(store.state.n).toBe(15);
|
||||
});
|
||||
|
||||
test("env is given to actions", () => {
|
||||
expect.assertions(1);
|
||||
const someEnv = {};
|
||||
const actions = {
|
||||
someaction({ env }) {
|
||||
expect(env).toBe(someEnv);
|
||||
}
|
||||
};
|
||||
const store = new Store({ state: {}, actions, env: someEnv });
|
||||
|
||||
store.dispatch("someaction");
|
||||
});
|
||||
|
||||
test("multiple commits trigger one update", async () => {
|
||||
let updateCounter = 0;
|
||||
const state = { n: 1 };
|
||||
|
||||
Reference in New Issue
Block a user