From 6b98e414c6da860faa462d3354831f4869aed729 Mon Sep 17 00:00:00 2001 From: Felix Roos Date: Tue, 19 Mar 2024 08:44:05 +0100 Subject: [PATCH] make onPaint work with mutiple repls on screen + add warning if stock onPaint is not overloaded --- packages/codemirror/codemirror.mjs | 16 ++++++++-------- packages/draw/draw.mjs | 5 ++--- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/packages/codemirror/codemirror.mjs b/packages/codemirror/codemirror.mjs index c06ac4a0..e59249ee 100644 --- a/packages/codemirror/codemirror.mjs +++ b/packages/codemirror/codemirror.mjs @@ -141,7 +141,6 @@ export class StrudelMirror { this.painters = []; this.drawTime = drawTime; this.onDraw = onDraw; - const self = this; this.id = id || s4(); this.drawer = new Drawer((haps, time) => { @@ -150,13 +149,6 @@ export class StrudelMirror { this.onDraw?.(haps, time, currentFrame, this.painters); }, drawTime); - // this approach does not work with multiple repls on screen - // TODO: refactor onPaint usages + find fix, maybe remove painters here? - Pattern.prototype.onPaint = function (onPaint) { - self.painters.push(onPaint); - return this; - }; - this.prebaked = prebake(); autodraw && this.drawFirstFrame(); @@ -182,6 +174,14 @@ export class StrudelMirror { beforeEval: async () => { cleanupDraw(); this.painters = []; + const self = this; + // this is similar to repl.mjs > injectPatternMethods + // maybe there is a solution without prototype hacking, but hey, it works + // we need to do this befor every eval to make sure it works with multiple StrudelMirror's side by side + Pattern.prototype.onPaint = function (onPaint) { + self.painters.push(onPaint); + return this; + }; await this.prebaked; await replOptions?.beforeEval?.(); }, diff --git a/packages/draw/draw.mjs b/packages/draw/draw.mjs index 4e84878f..0b5a1a4f 100644 --- a/packages/draw/draw.mjs +++ b/packages/draw/draw.mjs @@ -96,9 +96,8 @@ export const cleanupDraw = (clearScreen = true) => { } }; -Pattern.prototype.onPaint = function (onPaint) { - // this is evil! TODO: add pattern.context - this.context = { onPaint }; +Pattern.prototype.onPaint = function () { + console.warn('[draw] onPaint was not overloaded. Some drawings might not work'); return this; };