mirror of
https://github.com/eliasstepanik/strudel.git
synced 2026-01-11 21:58:37 +00:00
Fix resolveState
This commit is contained in:
parent
f209841e06
commit
921b23e9f0
@ -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]
|
||||
}
|
||||
|
||||
@ -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() {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user