pdf-forge-js/src/models/Stepper.svelte.ts
2025-03-27 02:48:09 +01:00

91 lines
1.9 KiB
TypeScript

import type { OperatorListModel } from './OperatorList';
export class DebugStepper {
breakPoints: number[];
nextBreakPoint: number | null = null;
currentIdx: number = $state(0);
pageNum: number = 0;
onBreak: boolean = $state(false);
opList: OperatorListModel | null = null;
callback: (() => void) | null = null;
constructor(pageNum: number, breakPoints: Set<number>) {
this.pageNum = pageNum;
this.breakPoints = Array.from(breakPoints).sort(function (a, b) {
return b - a;
});
}
updateBreakPoints(breakPoints: Set<number>) {
this.breakPoints = Array.from(breakPoints).filter(a => a > this.currentIdx).sort(function (a, b) {
return b - a;
});
}
public async breakIt(idx: number, callback: () => void) {
this.onBreak = true;
this.currentIdx = idx;
this.callback = callback;
}
public continue() {
this.nextBreakPoint = this.popNextBreakPoint();
if (this.callback) {
this.callback();
}
this.onBreak = false;
this.callback = null;
this._resume();
}
public step() {
this.nextBreakPoint = this.currentIdx + 1;
this._resume();
}
private _resume() {
if (this.callback) {
this.callback();
}
this.onBreak = false;
this.callback = null;
}
public getNextBreakPoint() {
return this.nextBreakPoint ?? this.breakPoints.pop();
}
private popNextBreakPoint() {
return this.breakPoints.pop() ?? null;
}
init(operatorList: OperatorListModel) {
this.opList = operatorList;
}
updateOperatorList(operatorList: OperatorListModel) {
this.opList = operatorList;
}
}
export class StepperManager {
stepper: DebugStepper | null = null;
enabled: boolean = false;
register(stepper: DebugStepper) {
this.stepper = stepper;
this.enabled = true;
}
unregister(stepper: DebugStepper) {
this.stepper = null;
this.enabled = false;
}
// must be called like this for api reasons
create(pageNum: number) {
return this.stepper;
}
}