@dodona/papyros
Version:
Scratchpad for multiple programming languages in the browser.
86 lines • 2.89 kB
JavaScript
import { Renderable, renderWithOptions } from "./util/Rendering";
import { getElement, t } from "./util/Util";
import { BackendManager } from "./BackendManager";
import { BackendEventType } from "./BackendEvent";
import "@dodona/trace-component";
const TRACE_COMPONENT_ID = "trace-component";
const EXECUTION_LIMIT = 10000;
function createDelayer() {
let timer;
return (callback, ms) => {
clearTimeout(timer);
timer = setTimeout(callback, ms);
};
}
const delay = createDelayer();
export class Debugger extends Renderable {
constructor() {
super();
this.frameStates = [];
this.currentOutputs = 0;
this.currentInputs = 0;
this.traceBuffer = [];
this.reset();
BackendManager.subscribe(BackendEventType.Start, () => {
this.reset();
});
BackendManager.subscribe(BackendEventType.Output, () => {
this.currentOutputs++;
});
BackendManager.subscribe(BackendEventType.Input, () => {
this.currentInputs++;
});
BackendManager.subscribe(BackendEventType.Frame, e => {
const frame = JSON.parse(e.data);
const frameState = {
line: frame.line,
outputs: this.currentOutputs,
inputs: this.currentInputs
};
this.frameStates.push(frameState);
this.traceBuffer.push(frame);
if (this.traceBuffer.length > 100) {
this.clearBuffer();
}
else {
delay(() => this.clearBuffer(), 100);
}
if (this.frameStates.length >= EXECUTION_LIMIT) {
BackendManager.publish({
type: BackendEventType.Stop,
data: "Execution limit reached"
});
}
});
}
_render(options) {
renderWithOptions(options, `
<tc-trace id="${TRACE_COMPONENT_ID}"></tc-trace>
`);
this.traceComponent = getElement(TRACE_COMPONENT_ID);
this.traceComponent.translations = t("Papyros.debugger");
this.traceComponent.addEventListener("frame-change", e => {
const frame = e.detail.frame;
BackendManager.publish({
type: BackendEventType.FrameChange,
data: this.frameStates[frame]
});
});
}
reset() {
this.frameStates = [];
this.currentOutputs = 0;
this.currentInputs = 0;
if (this.traceComponent) {
this.traceComponent.trace = [];
}
}
clearBuffer() {
var _a;
for (const frame of this.traceBuffer) {
(_a = this.traceComponent) === null || _a === void 0 ? void 0 : _a.addFrame(frame);
}
this.traceBuffer = [];
}
}
//# sourceMappingURL=Debugger.js.map