From 921b23e9f0f6007a59901aabb6ead40e9a2b4f24 Mon Sep 17 00:00:00 2001 From: alex Date: Sun, 27 Feb 2022 23:30:00 +0000 Subject: [PATCH] Fix resolveState --- strudel.mjs | 7 ++++--- test/pattern.test.mjs | 21 +++++++++++++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/strudel.mjs b/strudel.mjs index 505cdcb4..6cbb9994 100644 --- a/strudel.mjs +++ b/strudel.mjs @@ -182,7 +182,7 @@ class Hap { this.context = context this.stateful = stateful if (stateful) { - assert(typeof this.value === "function", "Stateful values must be functions"); + console.assert(typeof this.value === "function", "Stateful values must be functions"); } } @@ -205,9 +205,10 @@ class Hap { resolveState(state) { if (this.stateful && this.hasOnset()) { + console.log("stateful") const func = this.value - [newState, newValue] = func(state) - return [newState, this.withValue(() => newValue)] + const [newState, newValue] = func(state) + return [newState, new Hap(this.whole, this.part, newValue, this.context, false)] } return [state, this] } diff --git a/test/pattern.test.mjs b/test/pattern.test.mjs index cc6069c8..9d98d0bd 100644 --- a/test/pattern.test.mjs +++ b/test/pattern.test.mjs @@ -58,6 +58,27 @@ describe('Hap', function() { assert.equal(d.spanEquals(e), true) }) }) + describe('resolveState()', () => { + it('Can increment some state', function() { + const stateful_value = state => { + const newValue = state["incrementme"] + // TODO Does the state *need* duplicating here? + const newState = {...state} + newState["incrementme"]++ + return [newState, newValue] + } + const state = {incrementme: 10} + const ev1 = new Hap(ts(0,1), ts(0,1), stateful_value, {}, true) + const [state2, ev2] = ev1.resolveState(state) + const [state3, ev3] = ev1.resolveState(state2) + assert.deepStrictEqual( + ev3, new Hap(ts(0,1),ts(0,1),11,{},false) + ) + assert.deepStrictEqual( + state3, {incrementme: 12} + ) + }) + }) }); describe('Pattern', function() {