Lazy web worker loader fixes

pull/109/head
Zef Hemel 2022-11-01 15:00:59 +01:00
parent 01ea4c9f15
commit b7f4f282e6
3 changed files with 13 additions and 7 deletions

View File

@ -2,6 +2,7 @@ import { Manifest, RuntimeEnvironment } from "./types.ts";
import { Sandbox } from "./sandbox.ts";
import { System } from "./system.ts";
import { AssetBundle, AssetJson } from "./asset_bundle/bundle.ts";
import { resolve } from "https://deno.land/std@0.158.0/path/win32.ts";
export class Plug<HookT> {
system: System<HookT>;
@ -27,9 +28,13 @@ export class Plug<HookT> {
this.version = new Date().getTime();
}
private sandboxInitialized: Promise<void> | undefined = undefined;
// Lazy load sandbox, guarantees that the sandbox is loaded
async ensureSandbox() {
if (!this.sandbox) {
lazyInitSandbox(): Promise<void> {
if (this.sandboxInitialized) {
return this.sandboxInitialized;
}
this.sandboxInitialized = Promise.resolve().then(async () => {
console.log("Now starting sandbox for", this.name);
// Kick off worker
this.sandbox = this.sandboxFactory(this);
@ -40,7 +45,8 @@ export class Plug<HookT> {
await this.sandbox.loadDependency(dep, code);
}
await this.system.emit("sandboxInitialized", this.sandbox, this);
}
});
return this.sandboxInitialized;
}
load(manifest: Manifest<HookT>) {
@ -72,7 +78,7 @@ export class Plug<HookT> {
if (!funDef) {
throw new Error(`Function ${name} not found in manifest`);
}
await this.ensureSandbox();
await this.lazyInitSandbox();
const sandbox = this.sandbox!;
if (funDef.redirect) {
// Function redirect, look up

View File

@ -1,5 +1,5 @@
import { LogEntry } from "../sandbox.ts";
import { SysCallMapping, System } from "../system.ts";
import type { LogEntry } from "../sandbox.ts";
import type { SysCallMapping, System } from "../system.ts";
export default function sandboxSyscalls(system: System<any>): SysCallMapping {
return {

View File

@ -115,7 +115,7 @@ export class System<HookT> extends EventEmitter<SystemEvents<HookT>> {
// Ok, let's load this thing!
const plug = new Plug(this, name, sandboxFactory);
console.log("Loading", name);
await plug.load(manifest);
plug.load(manifest);
this.plugs.set(name, plug);
await this.emit("plugLoaded", plug);
return plug;