makecode-browser
Version:
MakeCode (PXT) - web-cached build tool
161 lines • 5.5 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BrowserLanguageService = void 0;
const workerFiles_1 = require("./workerFiles");
class BrowserLanguageService {
constructor(editor) {
this.editor = editor;
this.nextID = 0;
this.pendingMessages = {};
let workerSource = `var pxtTargetBundle = ${JSON.stringify(this.editor.targetJson)};\n`;
workerSource += this.editor.pxtWorkerJs + "\n";
workerSource += workerFiles_1.workerJs;
const workerBlob = new Blob([workerSource], { type: "application/javascript" });
this.worker = new Worker(URL.createObjectURL(workerBlob));
this.worker.onmessage = (ev) => {
if (ev.data.kind) {
this.onWorkerRequestReceived(ev.data);
}
else {
this.onWorkerResponseReceived(ev.data);
}
};
}
dispose() {
this.worker.terminate();
}
async registerDriverCallbacksAsync(callbacks) {
this.driverCallbacks = callbacks;
await this.sendWorkerRequestAsync({
type: "registerDriverCallbacks"
});
}
async setWebConfigAsync(config) {
await this.sendWorkerRequestAsync({
type: "setWebConfig",
webConfig: config
});
}
async getWebConfigAsync() {
const res = await this.sendWorkerRequestAsync({
type: "getWebConfig"
});
return res.webConfig;
}
async getAppTargetAsync() {
const res = await this.sendWorkerRequestAsync({
type: "getAppTarget"
});
return res.appTarget;
}
async getTargetConfigAsync() {
return this.editor.targetConfig;
}
async supportsGhPackagesAsync() {
const res = await this.sendWorkerRequestAsync({
type: "supportsGhPackages"
});
return res.supported;
}
async setHwVariantAsync(variant) {
await this.sendWorkerRequestAsync({
type: "setHwVariant",
variant
});
}
async getHardwareVariantsAsync() {
const res = await this.sendWorkerRequestAsync({
type: "getHardwareVariants"
});
return res.configs;
}
async getBundledPackageConfigsAsync() {
const res = await this.sendWorkerRequestAsync({
type: "getBundledPackageConfigs"
});
return res.configs;
}
async getCompileOptionsAsync(prj, simpleOpts) {
const res = await this.sendWorkerRequestAsync({
type: "getCompileOptionsAsync",
opts: simpleOpts
});
return res.result;
}
async installGhPackagesAsync(projectFiles) {
const res = await this.sendWorkerRequestAsync({
type: "installGhPackagesAsync",
files: projectFiles
});
return res.result;
}
async setProjectTextAsync(projectFiles) {
await this.sendWorkerRequestAsync({
type: "setProjectText",
files: projectFiles
});
}
async performOperationAsync(op, options) {
const res = await this.sendWorkerRequestAsync({
type: "performOperation",
op: op,
data: options
});
return res.result;
}
async enableExperimentalHardwareAsync() {
await this.sendWorkerRequestAsync({
type: "enableExperimentalHardware",
});
}
async enableDebugAsync() {
await this.sendWorkerRequestAsync({
type: "enableDebug",
});
}
async setCompileSwitchesAsync(flags) {
await this.sendWorkerRequestAsync({
type: "setCompileSwitches",
flags
});
}
async buildSimJsInfoAsync(result) {
// If you want to implement this, figure out how to get the worker to run the pxtc.buildSimJsInfo function
throw new Error("Not implemented");
}
sendWorkerRequestAsync(message) {
message.id = this.nextID++;
return new Promise(resolve => {
this.pendingMessages[message.id] = resolve;
this.worker.postMessage(message);
});
}
onWorkerResponseReceived(message) {
if (this.pendingMessages[message.id]) {
this.pendingMessages[message.id](message);
delete this.pendingMessages[message.id];
}
else {
console.warn("Received message with no callback");
}
}
async onWorkerRequestReceived(message) {
switch (message.type) {
case "cacheGet":
this.sendWorkerRequestResponse(Object.assign(Object.assign({}, message), { response: true, value: await this.driverCallbacks.cacheGet(message.key) }));
break;
case "cacheSet":
await this.driverCallbacks.cacheSet(message.key, message.value);
this.sendWorkerRequestResponse(Object.assign(Object.assign({}, message), { response: true }));
break;
case "packageOverride":
this.sendWorkerRequestResponse(Object.assign(Object.assign({}, message), { response: true, files: await this.driverCallbacks.pkgOverrideAsync(message.packageId) }));
break;
}
}
sendWorkerRequestResponse(message) {
this.worker.postMessage(message);
}
}
exports.BrowserLanguageService = BrowserLanguageService;
//# sourceMappingURL=languageService.js.map