[IMP] store: propagate 'set' function to commit parameters

This commit is contained in:
Géry Debongnie
2019-04-12 12:42:14 +02:00
parent 111080fdbf
commit e0608aa7fd
2 changed files with 40 additions and 16 deletions
+9 -3
View File
@@ -4,11 +4,12 @@ import { Component } from "./component";
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Store Definition // Store Definition
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
interface StoreConfig { interface StoreConfig {
env?: any; env?: any;
state?: any; state?: any;
actions?: any; actions?: any;
mutations?: any; mutations?: { [name: string]: any };
} }
interface StoreOption { interface StoreOption {
@@ -70,7 +71,11 @@ export class Store extends EventBus {
this._isMutating = true; this._isMutating = true;
this.observer.allowMutations = true; this.observer.allowMutations = true;
this.mutations[type].call(null, this.state, payload); this.mutations[type].call(
null,
{ state: this.state, set: this.observer.set },
payload
);
this.observer.allowMutations = false; this.observer.allowMutations = false;
if (this.debug) { if (this.debug) {
@@ -83,7 +88,6 @@ export class Store extends EventBus {
await Promise.resolve(); await Promise.resolve();
if (this._isMutating) { if (this._isMutating) {
this._isMutating = false; this._isMutating = false;
// this.observer.disableMutations();
if (currentRev !== this.observer.__rev__) { if (currentRev !== this.observer.__rev__) {
this.trigger("update", this.state); this.trigger("update", this.state);
} }
@@ -111,6 +115,7 @@ export function makeObserver(): Observer {
function set(target: any, key: number | string, value: any) { function set(target: any, key: number | string, value: any) {
if (Array.isArray(target)) { if (Array.isArray(target)) {
// todo
} else { } else {
addProp(target, key as string, value); addProp(target, key as string, value);
} }
@@ -124,6 +129,7 @@ export function makeObserver(): Observer {
value: any value: any
) { ) {
Object.defineProperty(obj, key, { Object.defineProperty(obj, key, {
enumerable: true,
get() { get() {
return value; return value;
}, },
+31 -13
View File
@@ -11,7 +11,7 @@ describe("basic use", () => {
test("commit a mutation", () => { test("commit a mutation", () => {
const state = { n: 1 }; const state = { n: 1 };
const mutations = { const mutations = {
inc(state, delta) { inc({ state }, delta) {
state.n += delta; state.n += delta;
} }
}; };
@@ -25,7 +25,7 @@ describe("basic use", () => {
test("dispatch an action", () => { test("dispatch an action", () => {
const state = { n: 1 }; const state = { n: 1 };
const mutations = { const mutations = {
inc(state, delta) { inc({ state }, delta) {
state.n += delta; state.n += delta;
} }
}; };
@@ -57,7 +57,7 @@ describe("basic use", () => {
test("can dispatch an action in an action", () => { test("can dispatch an action in an action", () => {
const state = { n: 1 }; const state = { n: 1 };
const mutations = { const mutations = {
inc(state, delta) { inc({ state }, delta) {
state.n += delta; state.n += delta;
} }
}; };
@@ -79,10 +79,10 @@ describe("basic use", () => {
test("dispatch allow synchronizing between actions", async () => { test("dispatch allow synchronizing between actions", async () => {
const state = { n: 1 }; const state = { n: 1 };
const mutations = { const mutations = {
inc(state, delta) { inc({ state }, delta) {
state.n += delta; state.n += delta;
}, },
setN(state, n) { setN({ state }, n) {
state.n = n; state.n = n;
} }
}; };
@@ -117,6 +117,24 @@ describe("basic use", () => {
store.dispatch("someaction"); store.dispatch("someaction");
}); });
test("set function is given to mutations", async () => {
let updateCounter = 0;
const state = { bertinchamps: "brune" };
const mutations = {
addInfo({ state, set }) {
set(state, "chouffe", "blonde");
}
};
const store = new Store({ state, mutations, actions: {} });
store.on("update", null, () => updateCounter++);
expect(updateCounter).toBe(0);
store.commit("addInfo");
await nextMicroTick();
expect(updateCounter).toBe(1);
expect(store.state).toEqual({ bertinchamps: "brune", chouffe: "blonde" });
});
}); });
describe("observer", () => { describe("observer", () => {
@@ -269,7 +287,7 @@ describe("advanced state properties", () => {
expect.assertions(3); expect.assertions(3);
const state = { a: [1, 2, 3] }; const state = { a: [1, 2, 3] };
const mutations = { const mutations = {
m(state) { m({ state }) {
expect(state.a.length).toBe(3); expect(state.a.length).toBe(3);
const l = state.a.push(53); const l = state.a.push(53);
expect(l).toBe(4); expect(l).toBe(4);
@@ -282,7 +300,7 @@ describe("advanced state properties", () => {
test("can use object assign in store", async () => { test("can use object assign in store", async () => {
const mutations = { const mutations = {
dosomething(state) { dosomething({ state }) {
Object.assign(state.westmalle, { a: 3, b: 4 }); Object.assign(state.westmalle, { a: 3, b: 4 });
} }
}; };
@@ -296,7 +314,7 @@ describe("advanced state properties", () => {
test("aku reactive store state 1", async () => { test("aku reactive store state 1", async () => {
const mutations = { const mutations = {
inc(state) { inc({ state }) {
state.counter++; state.counter++;
} }
}; };
@@ -313,7 +331,7 @@ describe("updates triggered by the store", () => {
let updateCounter = 0; let updateCounter = 0;
const state = { n: 1 }; const state = { n: 1 };
const mutations = { const mutations = {
inc(state, delta) { inc({ state }, delta) {
state.n += delta; state.n += delta;
} }
}; };
@@ -332,11 +350,11 @@ describe("updates triggered by the store", () => {
let updateCounter = 0; let updateCounter = 0;
const state = { n: 1 }; const state = { n: 1 };
const mutations = { const mutations = {
inc(state, delta) { inc({ state }, delta) {
state.n += delta; state.n += delta;
}, },
noop() {}, noop() {},
noop2(state) { noop2({ state }) {
const val = state.n; const val = state.n;
state.n = val; state.n = val;
} }
@@ -387,7 +405,7 @@ describe("connecting a component to store", () => {
test("connecting a component works", async () => { test("connecting a component works", async () => {
const state = { todos: [] }; const state = { todos: [] };
const mutations = { const mutations = {
addTodo(state, msg) { addTodo({ state }, msg) {
state.todos.push({ msg }); state.todos.push({ msg });
} }
}; };
@@ -449,7 +467,7 @@ describe("connecting a component to store", () => {
const state = { todos: [{ id: 1, text: "jupiler" }] }; const state = { todos: [{ id: 1, text: "jupiler" }] };
let nextId = 2; let nextId = 2;
const mutations = { const mutations = {
addTodo(state, text) { addTodo({ state }, text) {
state.todos.push({ text, id: nextId++ }); state.todos.push({ text, id: nextId++ });
} }
}; };