@dodona/papyros
Version:
Scratchpad for multiple programming languages in the browser.
92 lines • 3.65 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import { BackendEventType } from "./BackendEvent";
import { syncExpose } from "comsync";
import { BackendEventQueue } from "./BackendEventQueue";
export var RunMode;
(function (RunMode) {
RunMode["Run"] = "run";
RunMode["Debug"] = "debug";
RunMode["Doctest"] = "doctest";
})(RunMode || (RunMode = {}));
export class Backend {
/**
* Constructor is limited as it is meant to be used as a WebWorker
* Proper initialization occurs in the launch method when the worker is started
* Synchronously exposing methods should be done here
*/
constructor() {
this.extras = {};
this.onEvent = () => {
// Empty, initialized in launch
};
this.runCode = this.syncExpose()(this.runCode.bind(this));
this.queue = {};
}
/**
* @return {any} The function to expose methods for Comsync to allow interrupting
*/
syncExpose() {
return syncExpose;
}
/**
* Initialize the backend by doing all setup-related work
* @param {function(BackendEvent):void} onEvent Callback for when events occur
* @param {function():void} onOverflow Callback for when overflow occurs
* @return {Promise<void>} Promise of launching
*/
launch(onEvent, onOverflow) {
return __awaiter(this, void 0, void 0, function* () {
this.onEvent = (e) => {
onEvent(e);
if (e.type === BackendEventType.Sleep) {
return this.extras.syncSleep(e.data);
}
else if (e.type === BackendEventType.Input) {
return this.extras.readMessage();
}
};
this.queue = new BackendEventQueue(this.onEvent.bind(this), onOverflow);
return Promise.resolve();
});
}
/**
* Determine whether the modes supported by this Backend are active
* @param {string} code The current code in the editor
* @return {Array<RunMode>} The run modes of this Backend
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
runModes(code) {
return [];
}
/**
* @return {boolean} Whether too many output events were generated
*/
hasOverflow() {
return this.queue.hasOverflow();
}
/**
* @return {Array<BackendEvent>} The events that happened after overflow
*/
getOverflow() {
return this.queue.getOverflow();
}
/**
* Provide files to be used by the backend
* @param {Record<string, string>} inlineFiles Map of file names to their contents
* @param {Record<string, string>} hrefFiles Map of file names to URLS with their contents
* @return {Promise<void>} Resolves when the files are present in the backend
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
provideFiles(inlineFiles, hrefFiles) {
return Promise.resolve();
}
}
//# sourceMappingURL=Backend.js.map