From 0095bfa61f508f5eb2f316ccd634eba3e2ccc687 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandre=20K=C3=BChn?= Date: Wed, 26 Jun 2019 18:11:11 +0200 Subject: [PATCH] [IMP] store: positional arguments with dispatch/commit --- doc/store.md | 25 +++++++++++++++++++++++-- src/store.ts | 14 +++++++------- tests/store.test.ts | 25 +++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 9 deletions(-) diff --git a/doc/store.md b/doc/store.md index 866e3b1e..c4c7111b 100644 --- a/doc/store.md +++ b/doc/store.md @@ -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 diff --git a/src/store.ts b/src/store.ts index f8fb34d4..895c61a7 100644 --- a/src/store.ts +++ b/src/store.ts @@ -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 { + dispatch(action: string, ...payload: any): Promise | 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] }); } } diff --git a/tests/store.test.ts b/tests/store.test.ts index 2efbaa35..6e3bbe20 100644 --- a/tests/store.test.ts +++ b/tests/store.test.ts @@ -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 = {