novy-syndicate/src/worker/wload.ts

67 lines
2.4 KiB
TypeScript
Raw Normal View History

2021-03-02 08:50:23 +00:00
// Web Worker loader
import { Actor, Turn, Assertion, Ref, __setNextActorId } from '../runtime/actor.js';
import { Record } from '@preserves/core';
2021-03-02 15:42:53 +00:00
import { parentPort, threadId } from 'worker_threads';
import { Relay, spawnRelay } from '../transport/relay.js';
2021-03-02 08:50:23 +00:00
const _Instance = Symbol.for('Instance');
const Instance = Record.makeConstructor<{moduleName: string, arg: Assertion}>()(
_Instance, ['moduleName', 'arg']);
2021-03-02 15:42:53 +00:00
const STARTING_ACTOR_ID = (threadId & (2 ** 20 - 1)) * 1000000000;
__setNextActorId(STARTING_ACTOR_ID);
type TaskState =
| { readonly state: "start_pending" }
| { readonly state: "starting" }
| { readonly state: "shutdown_pending" }
| { readonly state: "running", shutdownRef: Ref };
2021-04-16 18:29:16 +00:00
new Actor(t => {
2021-03-02 08:50:23 +00:00
const p = parentPort!;
let taskState: TaskState = { state: "start_pending" };
2021-04-16 18:29:16 +00:00
t.activeFacet.preventInertCheck();
2021-03-02 08:50:23 +00:00
spawnRelay(t, {
2021-03-02 15:42:53 +00:00
nextLocalOid: STARTING_ACTOR_ID + 500000000,
2021-03-02 08:50:23 +00:00
packetWriter: bs => p.postMessage(bs),
setup(t: Turn, r: Relay) {
p.on('message', bs => r.accept(bs));
2021-04-16 18:29:16 +00:00
p.on('close', () => Turn.for(t.activeFacet, t => t.stopActor()));
2021-03-02 08:50:23 +00:00
},
2021-03-02 15:42:53 +00:00
initialRef: t.ref({
async assert(t, inst) {
2021-03-02 15:42:53 +00:00
if (!Instance.isClassOf(inst)) return;
if (taskState.state !== "start_pending") return;
taskState = { state: "starting" };
2021-03-02 15:42:53 +00:00
const m = await import(Instance._.moduleName(inst));
t.freshen(t => t.spawn(t => {
2021-04-16 18:29:16 +00:00
t.activeFacet.actor.atExit(() => {
console.log('Worker terminating');
process.exit(0);
});
if (taskState.state === "shutdown_pending") {
2021-04-16 18:29:16 +00:00
t.stopActor();
2021-03-02 15:42:53 +00:00
} else {
taskState = {
state: "running",
2021-04-16 18:29:16 +00:00
shutdownRef: t.ref({ message(t) { t.stopActor(); } }),
};
2021-03-02 15:42:53 +00:00
m.default(t, Instance._.arg(inst));
}
}));
},
retract(t) {
if (taskState.state === "running") {
t.message(taskState.shutdownRef, true);
2021-03-02 08:50:23 +00:00
} else {
taskState = { state: "shutdown_pending" };
2021-03-02 08:50:23 +00:00
}
}
2021-03-02 15:42:53 +00:00
}),
// debug: true,
});
2021-03-02 08:50:23 +00:00
});