Lazy web worker loader fixes
This commit is contained in:
parent
01ea4c9f15
commit
b7f4f282e6
@ -2,6 +2,7 @@ import { Manifest, RuntimeEnvironment } from "./types.ts";
|
|||||||
import { Sandbox } from "./sandbox.ts";
|
import { Sandbox } from "./sandbox.ts";
|
||||||
import { System } from "./system.ts";
|
import { System } from "./system.ts";
|
||||||
import { AssetBundle, AssetJson } from "./asset_bundle/bundle.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> {
|
export class Plug<HookT> {
|
||||||
system: System<HookT>;
|
system: System<HookT>;
|
||||||
@ -27,9 +28,13 @@ export class Plug<HookT> {
|
|||||||
this.version = new Date().getTime();
|
this.version = new Date().getTime();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private sandboxInitialized: Promise<void> | undefined = undefined;
|
||||||
// Lazy load sandbox, guarantees that the sandbox is loaded
|
// Lazy load sandbox, guarantees that the sandbox is loaded
|
||||||
async ensureSandbox() {
|
lazyInitSandbox(): Promise<void> {
|
||||||
if (!this.sandbox) {
|
if (this.sandboxInitialized) {
|
||||||
|
return this.sandboxInitialized;
|
||||||
|
}
|
||||||
|
this.sandboxInitialized = Promise.resolve().then(async () => {
|
||||||
console.log("Now starting sandbox for", this.name);
|
console.log("Now starting sandbox for", this.name);
|
||||||
// Kick off worker
|
// Kick off worker
|
||||||
this.sandbox = this.sandboxFactory(this);
|
this.sandbox = this.sandboxFactory(this);
|
||||||
@ -40,7 +45,8 @@ export class Plug<HookT> {
|
|||||||
await this.sandbox.loadDependency(dep, code);
|
await this.sandbox.loadDependency(dep, code);
|
||||||
}
|
}
|
||||||
await this.system.emit("sandboxInitialized", this.sandbox, this);
|
await this.system.emit("sandboxInitialized", this.sandbox, this);
|
||||||
}
|
});
|
||||||
|
return this.sandboxInitialized;
|
||||||
}
|
}
|
||||||
|
|
||||||
load(manifest: Manifest<HookT>) {
|
load(manifest: Manifest<HookT>) {
|
||||||
@ -72,7 +78,7 @@ export class Plug<HookT> {
|
|||||||
if (!funDef) {
|
if (!funDef) {
|
||||||
throw new Error(`Function ${name} not found in manifest`);
|
throw new Error(`Function ${name} not found in manifest`);
|
||||||
}
|
}
|
||||||
await this.ensureSandbox();
|
await this.lazyInitSandbox();
|
||||||
const sandbox = this.sandbox!;
|
const sandbox = this.sandbox!;
|
||||||
if (funDef.redirect) {
|
if (funDef.redirect) {
|
||||||
// Function redirect, look up
|
// Function redirect, look up
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import { LogEntry } from "../sandbox.ts";
|
import type { LogEntry } from "../sandbox.ts";
|
||||||
import { SysCallMapping, System } from "../system.ts";
|
import type { SysCallMapping, System } from "../system.ts";
|
||||||
|
|
||||||
export default function sandboxSyscalls(system: System<any>): SysCallMapping {
|
export default function sandboxSyscalls(system: System<any>): SysCallMapping {
|
||||||
return {
|
return {
|
||||||
|
@ -115,7 +115,7 @@ export class System<HookT> extends EventEmitter<SystemEvents<HookT>> {
|
|||||||
// Ok, let's load this thing!
|
// Ok, let's load this thing!
|
||||||
const plug = new Plug(this, name, sandboxFactory);
|
const plug = new Plug(this, name, sandboxFactory);
|
||||||
console.log("Loading", name);
|
console.log("Loading", name);
|
||||||
await plug.load(manifest);
|
plug.load(manifest);
|
||||||
this.plugs.set(name, plug);
|
this.plugs.set(name, plug);
|
||||||
await this.emit("plugLoaded", plug);
|
await this.emit("plugLoaded", plug);
|
||||||
return plug;
|
return plug;
|
||||||
|
Loading…
Reference in New Issue
Block a user