[IMP] store: positional arguments with dispatch/commit

This commit is contained in:
Alexandre Kühn
2019-06-26 18:11:11 +02:00
committed by Géry Debongnie
parent 1b12cf9b91
commit 0095bfa61f
3 changed files with 55 additions and 9 deletions
+23 -2
View File
@@ -86,6 +86,20 @@ object, or modifying an array with the `arr[i] = newValue` syntax). See the
Mutations are the only way to modify the state. Changing the state outside a
mutation is not allowed (and should throw an error). Mutations are synchronous.
```js
const mutations = {
setLoginState({ state }, loginState) {
state.loginState = loginState;
}
};
```
Mutations are called with the `commit` method on the store, and can receive an arbitrary number of arguments.
```js
store.commit("setLoginState", "error");
```
### Actions
Actions are used to coordinate state changes. It is also useful whenever some
@@ -94,10 +108,10 @@ in an action.
```js
const actions = {
async login({ commit }) {
async login({ commit }, info) {
commit("setLoginState", "pending");
try {
const loginInfo = await doSomeRPC("/login/", "someinfo");
const loginInfo = await doSomeRPC("/login/", info);
commit("setLoginState", loginInfo);
} catch {
commit("setLoginState", "error");
@@ -106,6 +120,13 @@ const actions = {
};
```
Actions are called with the `dispatch` method on the store, and can receive an
arbitrary number of arguments.
```js
store.dispatch("login", someInfo);
```
### Getters
Usually, data contained in the store will be stored in a normalized way. For
+7 -7
View File
@@ -21,8 +21,8 @@ import { Observer } from "./observer";
// Store Definition
//------------------------------------------------------------------------------
type Mutation = ({ state, commit, getters }, payload: any) => void;
type Action = ({ commit, state, dispatch, env, getters }, payload: any) => void;
type Mutation = ({ state, commit, getters }, ...payload: any) => void;
type Action = ({ commit, state, dispatch, env, getters }, ...payload: any) => void;
type Getter = ({ state, getters }, payload) => any;
interface StoreConfig {
@@ -85,7 +85,7 @@ export class Store extends EventBus {
}
}
dispatch(action: string, payload?: any): Promise<void> | void {
dispatch(action: string, ...payload: any): Promise<void> | void {
if (!this.actions[action]) {
throw new Error(`[Error] action ${action} is undefined`);
}
@@ -97,7 +97,7 @@ export class Store extends EventBus {
state: this.state,
getters: this.getters
},
payload
...payload
);
if (result instanceof Promise) {
return new Promise((resolve, reject) => {
@@ -107,7 +107,7 @@ export class Store extends EventBus {
}
}
commit(type: string, payload?: any): any {
commit(type: string, ...payload: any): any {
if (!this.mutations[type]) {
throw new Error(`[Error] mutation ${type} is undefined`);
}
@@ -121,7 +121,7 @@ export class Store extends EventBus {
state: this.state,
getters: this.getters
},
payload
...payload
);
if (this._commitLevel === 1) {
@@ -130,7 +130,7 @@ export class Store extends EventBus {
this.history.push({
state: this.state,
mutation: type,
payload: payload
payload: [...payload]
});
}
}
+25
View File
@@ -42,6 +42,31 @@ describe("basic use", () => {
expect(store.state.n).toBe(15);
});
test("dispatch an action + commit a mutation with positional arguments", () => {
const state = { n1: 1, n2: 1, n3: 1 };
const mutations = {
batchInc({ state }, delta1, delta2, delta3) {
state.n1 += delta1;
state.n2 += delta2;
state.n3 += delta3;
}
};
const actions = {
batchInc({ commit }, delta1, delta2, delta3) {
commit("batchInc", delta1, delta2, delta3);
}
};
const store = new Store({ state, mutations, actions });
expect(store.state.n1).toBe(1);
expect(store.state.n2).toBe(1);
expect(store.state.n3).toBe(1);
store.dispatch("batchInc", 14, 30, 88);
expect(store.state.n1).toBe(15);
expect(store.state.n2).toBe(31);
expect(store.state.n3).toBe(89);
});
test("modifying state outside of mutations trigger error", () => {
const state = { n: 1 };
const actions = {