can now have multiple triggers

+ Pattern.log now makes sense
This commit is contained in:
Felix Roos 2022-11-12 16:52:00 +01:00
parent 27fb2d2d5b
commit ff5d14fb63
9 changed files with 32 additions and 22 deletions

View file

@ -55,7 +55,8 @@ Fraction.prototype.min = function (other) {
return this.lt(other) ? this : other;
};
Fraction.prototype.show = function () {
Fraction.prototype.show = function (/* excludeWhole = false */) {
// return this.toFraction(excludeWhole);
return this.s * this.n + '/' + this.d;
};

View file

@ -85,9 +85,13 @@ export class Hap {
);
}
showWhole() {
showWhole(compact = false) {
return `${this.whole == undefined ? '~' : this.whole.show()}: ${
typeof this.value === 'object' ? JSON.stringify(this.value) : this.value
typeof this.value === 'object'
? compact
? JSON.stringify(this.value).slice(1, -1).replaceAll('"', '').replaceAll(',', ' ')
: JSON.stringify(this.value)
: this.value
}`;
}

View file

@ -12,6 +12,7 @@ import { unionWithObj } from './value.mjs';
import { compose, removeUndefineds, flatten, id, listRange, curry, mod, numeralArgs, parseNumeral } from './util.mjs';
import drawLine from './drawLine.mjs';
import { logger } from './logger.mjs';
let stringParser;
// parser is expected to turn a string into a pattern
@ -1328,22 +1329,25 @@ export class Pattern {
.unit('c')
.slow(factor);
}
onTrigger(onTrigger) {
return this._withHap((hap) => hap.setContext({ ...hap.context, onTrigger }));
}
log(func = id) {
onTrigger(onTrigger, dominant = true) {
return this._withHap((hap) =>
hap.setContext({
...hap.context,
onTrigger: (...args) => {
if (hap.context.onTrigger) {
if (!dominant && hap.context.onTrigger) {
hap.context.onTrigger(...args);
}
console.log(func(...args));
onTrigger(...args);
},
// we need this to know later if the default trigger should still fire
dominantTrigger: dominant,
}),
);
}
log(func = (_, hap) => `[hap] ${hap.showWhole(true)}`) {
return this.onTrigger((...args) => logger(func(...args)), false);
}
logValues(func = id) {
return this.log((_, hap) => func(hap.value));
}

View file

@ -17,12 +17,14 @@ export function repl({
interval,
onTrigger: async (hap, deadline, duration) => {
try {
if (!hap.context.onTrigger) {
return await defaultOutput(hap, deadline, duration);
if (!hap.context.onTrigger || !hap.context.dominantTrigger) {
await defaultOutput(hap, deadline, duration);
}
if (hap.context.onTrigger) {
const cps = 1;
// call signature of output / onTrigger is different...
await hap.context.onTrigger(getTime() + deadline, hap, getTime(), cps);
}
const cps = 1; // TODO: fix
// call signature of output / onTrigger is different...
return await hap.context.onTrigger(getTime() + deadline, hap, getTime(), cps);
} catch (err) {
logger(`[cyclist] error: ${err.message}`, 'error');
}