mirror of
https://github.com/eliasstepanik/strudel-docker.git
synced 2026-01-24 03:58:53 +00:00
Add event/hap context
This commit is contained in:
parent
db22653124
commit
912c36d590
16
strudel.mjs
16
strudel.mjs
@ -172,12 +172,14 @@ class Hap {
|
|||||||
then the whole will be returned as None, in which case the given
|
then the whole will be returned as None, in which case the given
|
||||||
value will have been sampled from the point halfway between the
|
value will have been sampled from the point halfway between the
|
||||||
start and end of the 'part' timespan.
|
start and end of the 'part' timespan.
|
||||||
|
The context is to store a list of source code locations causing the event
|
||||||
*/
|
*/
|
||||||
|
|
||||||
constructor(whole, part, value) {
|
constructor(whole, part, value, context=[]) {
|
||||||
this.whole = whole
|
this.whole = whole
|
||||||
this.part = part
|
this.part = part
|
||||||
this.value = value
|
this.value = value
|
||||||
|
this.context = context
|
||||||
}
|
}
|
||||||
|
|
||||||
withSpan(func) {
|
withSpan(func) {
|
||||||
@ -214,6 +216,10 @@ class Hap {
|
|||||||
show() {
|
show() {
|
||||||
return "(" + (this.whole == undefined ? "~" : this.whole.show()) + ", " + this.part.show() + ", " + this.value + ")"
|
return "(" + (this.whole == undefined ? "~" : this.whole.show()) + ", " + this.part.show() + ", " + this.value + ")"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setContext(context) {
|
||||||
|
return new Hap(this.whole, this.part, this.value, context)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class State {
|
export class State {
|
||||||
@ -298,6 +304,14 @@ class Pattern {
|
|||||||
return new Pattern(state => func(this.query(state)))
|
return new Pattern(state => func(this.query(state)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_withEvent(func) {
|
||||||
|
return this._withEvents(events => events.map(func))
|
||||||
|
}
|
||||||
|
|
||||||
|
_setContext(context) {
|
||||||
|
return this._withEvent(event => event.setContext(context))
|
||||||
|
}
|
||||||
|
|
||||||
withLocation(location) {
|
withLocation(location) {
|
||||||
return this.fmap(value => {
|
return this.fmap(value => {
|
||||||
value = typeof value === 'object' && !Array.isArray(value) ? value : { value };
|
value = typeof value === 'object' && !Array.isArray(value) ? value : { value };
|
||||||
|
|||||||
@ -9,7 +9,7 @@ const { Time } = pkg;
|
|||||||
|
|
||||||
const st = (begin, end) => new State(ts(begin, end))
|
const st = (begin, end) => new State(ts(begin, end))
|
||||||
const ts = (begin, end) => new TimeSpan(Fraction(begin), Fraction(end));
|
const ts = (begin, end) => new TimeSpan(Fraction(begin), Fraction(end));
|
||||||
const hap = (whole, part, value) => new Hap(whole, part, value)
|
const hap = (whole, part, value, context=[]) => new Hap(whole, part, value, context)
|
||||||
|
|
||||||
const third = Fraction(1,3)
|
const third = Fraction(1,3)
|
||||||
const twothirds = Fraction(2,3)
|
const twothirds = Fraction(2,3)
|
||||||
@ -330,4 +330,17 @@ describe('Pattern', function() {
|
|||||||
)
|
)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
describe('_setContext()', () => {
|
||||||
|
it('Can set the event context', () => {
|
||||||
|
assert.deepStrictEqual(
|
||||||
|
pure("a")._setContext([[[0,1],[1,2]]]).firstCycle,
|
||||||
|
[hap(ts(0,1),
|
||||||
|
ts(0,1),
|
||||||
|
"a",
|
||||||
|
[[[0,1],[1,2]]]
|
||||||
|
)
|
||||||
|
]
|
||||||
|
)
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user