mirror of
https://github.com/eliasstepanik/strudel-docker.git
synced 2026-01-27 05:28:41 +00:00
Merge pull request #22 from tidalcycles/stateful-events
Fix resolveState
This commit is contained in:
commit
349aa45f54
@ -182,7 +182,7 @@ class Hap {
|
|||||||
this.context = context
|
this.context = context
|
||||||
this.stateful = stateful
|
this.stateful = stateful
|
||||||
if (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) {
|
resolveState(state) {
|
||||||
if (this.stateful && this.hasOnset()) {
|
if (this.stateful && this.hasOnset()) {
|
||||||
|
console.log("stateful")
|
||||||
const func = this.value
|
const func = this.value
|
||||||
[newState, newValue] = func(state)
|
const [newState, newValue] = func(state)
|
||||||
return [newState, this.withValue(() => newValue)]
|
return [newState, new Hap(this.whole, this.part, newValue, this.context, false)]
|
||||||
}
|
}
|
||||||
return [state, this]
|
return [state, this]
|
||||||
}
|
}
|
||||||
|
|||||||
@ -58,6 +58,27 @@ describe('Hap', function() {
|
|||||||
assert.equal(d.spanEquals(e), true)
|
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() {
|
describe('Pattern', function() {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user