novy-syndicate/main.ts

64 lines
2.4 KiB
TypeScript
Raw Normal View History

2021-03-02 08:50:23 +00:00
import { Actor, Assertion, Turn } from './actor.js';
import { Record } from 'preserves';
import { Dataspace } from './dataspace.js';
import { Worker } from 'worker_threads';
import { Relay, spawnRelay } from './relay.js';
2021-02-23 15:16:15 +00:00
2021-03-02 08:50:23 +00:00
const Instance = Record.makeConstructor<{moduleName: string, arg: Assertion}>()(
Symbol.for('Instance'), ['moduleName', 'arg']);
2021-02-22 18:37:47 +00:00
2021-03-02 08:50:23 +00:00
function spawnWorker(t: Turn, moduleName: string, arg: Assertion) {
const w = new Worker('./wload.js');
spawnRelay(t, {
packetWriter: bs => w.postMessage(bs),
setup(t: Turn, r: Relay) {
w.on('message', bs => r.accept(bs));
w.on('error', err => Turn.for(t.actor, t => t.crash(err)));
w.on('exit', code => Turn.for(t.actor, t => {
if (code === 0) {
2021-02-22 18:37:47 +00:00
t.quit();
} else {
2021-03-02 08:50:23 +00:00
t.crash(new Error(`Worker crashed with code ${code}`));
2021-02-22 18:37:47 +00:00
}
2021-03-02 08:50:23 +00:00
}));
},
initialOid: 0,
}).then(factory => Turn.for(new Actor(), t => {
t.assert(factory!, Instance(moduleName, arg));
}));
}
function spawnModule(t: Turn, moduleName: string, arg: Assertion) {
import(moduleName).then(m => t.freshen(t => t.spawn(t =>
m.default(t, arg))));
2021-02-23 15:16:15 +00:00
}
Turn.for(new Actor(), async (t: Turn) => {
const ds = t.ref(new Dataspace());
2021-02-24 20:48:55 +00:00
2021-03-02 08:50:23 +00:00
// spawnModule(t, './box.js', [ds, 500000, 25000]);
spawnWorker(t, './box.js', [ds, 50000, 2500]);
spawnModule(t, './client.js', ds);
// spawnWorker(t, './client.js', ds);
// spawnBox(t, attenuate(
// ds,
// rfilter(PCompound(CRec(BoxState.constructorInfo.label,
// BoxState.constructorInfo.arity),
// new Dictionary()),
// PCompound(CRec(Observe.constructorInfo.label,
// Observe.constructorInfo.arity),
// new Dictionary([[0, Lit(SetBox.constructorInfo.label)]])))));
// spawnClient(t, attenuate(
// ds,
// rfilter(PCompound(CRec(SetBox.constructorInfo.label,
// SetBox.constructorInfo.arity),
// new Dictionary()),
// PCompound(CRec(Observe.constructorInfo.label,
// Observe.constructorInfo.arity),
// new Dictionary([[0, Lit(BoxState.constructorInfo.label)]])))));
2021-02-24 20:48:55 +00:00
2021-02-22 09:12:10 +00:00
});