fix: drawer performance issue

This commit is contained in:
Felix Roos 2023-12-28 17:54:51 +01:00
parent 624affe092
commit a9dc0912d0
3 changed files with 12 additions and 4 deletions

View File

@ -126,6 +126,7 @@ export class StrudelMirror {
this.miniLocations = [];
this.widgets = [];
this.painters = [];
this.drawTime = drawTime;
this.onDraw = onDraw;
const self = this;
this.id = id || s4();
@ -151,6 +152,7 @@ export class StrudelMirror {
onToggle: (started) => {
replOptions?.onToggle?.(started);
if (started) {
this.adjustDrawTime();
this.drawer.start(this.repl.scheduler);
// stop other repls when this one is started
document.dispatchEvent(
@ -177,6 +179,7 @@ export class StrudelMirror {
updateWidgets(this.editor, this.widgets);
updateMiniLocations(this.editor, this.miniLocations);
replOptions?.afterEval?.(options);
this.adjustDrawTime();
this.drawer.invalidate();
},
});
@ -212,6 +215,11 @@ export class StrudelMirror {
};
document.addEventListener('start-repl', this.onStartRepl);
}
// adjusts draw time depending on if there are painters
adjustDrawTime() {
// when no painters are set, [0,0] is enough (just highlighting)
this.drawer.setDrawTime(!!this.painters.length ? this.drawTime : [0, 0]);
}
async drawFirstFrame() {
if (!this.onDraw) {
return;

View File

@ -145,6 +145,9 @@ export class Drawer {
},
);
}
setDrawTime(drawTime) {
this.drawTime = drawTime;
}
invalidate(scheduler = this.scheduler, t) {
if (!scheduler) {
return;

View File

@ -57,10 +57,7 @@ export function Repl2({ embedded = false }) {
const shouldDraw = true;
const init = useCallback(({ shouldDraw }) => {
// TODO: find way to make spiral & punchcard work (if there's any)
// upping the 2nd value leads to slow eval times
// because Drawer.invalidate might query alot at one time
const drawTime = [0, 0];
const drawTime = [-2, 2];
const drawContext = shouldDraw ? getDrawContext() : null;
let onDraw;
if (shouldDraw) {