From e005090296bd56d6d497d4cb5af992d0b90ce089 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandre=20K=C3=BChn?= Date: Mon, 15 Apr 2019 10:24:08 +0200 Subject: [PATCH] [IMP] store: return data from committing a mutation closes #29 --- src/store.ts | 3 ++- tests/store.test.ts | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/store.ts b/src/store.ts index d139513d..555755cf 100644 --- a/src/store.ts +++ b/src/store.ts @@ -74,7 +74,7 @@ export class Store extends EventBus { this._isMutating = true; this.observer.allowMutations = true; - this.mutations[type].call( + const res = this.mutations[type].call( null, { commit: this.commit.bind(this), @@ -103,6 +103,7 @@ export class Store extends EventBus { }); } this._commitLevel--; + return res; } } diff --git a/tests/store.test.ts b/tests/store.test.ts index 18232af0..3b38b944 100644 --- a/tests/store.test.ts +++ b/tests/store.test.ts @@ -95,6 +95,21 @@ describe("basic use", () => { expect(store.state.n).toBe(11); }); + test("return data from committing a mutation", () => { + const state = { n: 1 }; + const mutations = { + inc({ state }) { + return ++state.n; + }, + }; + const store = new Store({ state, mutations }); + + expect(store.state.n).toBe(1); + const res = store.commit("inc"); + expect(store.state.n).toBe(2); + expect(res).toBe(2); + }); + test("dispatch allow synchronizing between actions", async () => { const state = { n: 1 }; const mutations = {