diff --git a/plugos/plug.ts b/plugos/plug.ts index 968870fb..bbb9bdad 100644 --- a/plugos/plug.ts +++ b/plugos/plug.ts @@ -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 { system: System; @@ -27,9 +28,13 @@ export class Plug { this.version = new Date().getTime(); } + private sandboxInitialized: Promise | undefined = undefined; // Lazy load sandbox, guarantees that the sandbox is loaded - async ensureSandbox() { - if (!this.sandbox) { + lazyInitSandbox(): Promise { + 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 { await this.sandbox.loadDependency(dep, code); } await this.system.emit("sandboxInitialized", this.sandbox, this); - } + }); + return this.sandboxInitialized; } load(manifest: Manifest) { @@ -72,7 +78,7 @@ export class Plug { 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 diff --git a/plugos/syscalls/sandbox.ts b/plugos/syscalls/sandbox.ts index ab1e1b1b..a7e79b81 100644 --- a/plugos/syscalls/sandbox.ts +++ b/plugos/syscalls/sandbox.ts @@ -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): SysCallMapping { return { diff --git a/plugos/system.ts b/plugos/system.ts index 126f48c1..ab0d25a3 100644 --- a/plugos/system.ts +++ b/plugos/system.ts @@ -115,7 +115,7 @@ export class System extends EventEmitter> { // 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;