Fix resolveState

This commit is contained in:
alex 2022-02-27 23:30:00 +00:00
parent f209841e06
commit 921b23e9f0
2 changed files with 25 additions and 3 deletions

View File

@ -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]
}

View File

@ -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() {