novy-syndicate/src/main.ts

95 lines
3.4 KiB
TypeScript
Raw Normal View History

import { Actor, Assertion, Ref, Turn } from './actor.js';
import { Dictionary, Record } from '@preserves/core';
2021-03-23 11:18:57 +00:00
import { Dataspace } from './dataspace.js';
2021-03-02 08:50:23 +00:00
import { Worker } from 'worker_threads';
import { Relay, spawnRelay } from './relay.js';
2021-03-02 12:53:33 +00:00
import path from 'path';
2021-02-23 15:16:15 +00:00
2021-03-23 11:18:57 +00:00
import { attenuate, CRec, Lit, Pattern, PCompound, rfilter, ConstructorSpec } from './rewrite.js';
import { $BoxState, $SetBox } from './gen/box-protocol.js';
import { $Observe } from './gen/dataspace.js';
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) {
2021-03-02 12:53:33 +00:00
const w = new Worker(path.join(__dirname, 'wload.js'));
2021-04-16 18:29:16 +00:00
const reenable = t.activeFacet.preventInertCheck();
2021-03-02 08:50:23 +00:00
spawnRelay(t, {
packetWriter: bs => w.postMessage(bs),
setup(t: Turn, r: Relay) {
w.on('message', bs => r.accept(bs));
2021-04-16 18:29:16 +00:00
w.on('error', err => Turn.for(t.activeFacet, t => t.crash(err)));
w.on('exit', code => Turn.for(t.activeFacet, t => {
2021-03-02 08:50:23 +00:00
if (code === 0) {
2021-04-16 18:29:16 +00:00
t.stopActor();
2021-02-22 18:37:47 +00:00
} 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,
2021-04-16 18:29:16 +00:00
}).then(factory => {
reenable();
new Actor(t => t.assert(factory, Instance(moduleName, arg)));
});
2021-03-02 08:50:23 +00:00
}
function spawnModule(t: Turn, moduleName: string, arg: Assertion) {
2021-03-03 15:23:00 +00:00
import(moduleName).then(m => t.freshen(t => t.spawn(t => m.default(t, arg))));
2021-02-23 15:16:15 +00:00
}
// __setNextActorId(1000);
2021-04-16 18:29:16 +00:00
new Actor(async (t: Turn) => {
2021-03-03 10:45:01 +00:00
const ds_unproxied = t.ref(new Dataspace());
const ds = ds_unproxied;
// const { proxy: ds, revoker } = forwarder(t, ds_unproxied);
// t.spawn(t => {
// t.assert(ds, Observe(SetBox.constructorInfo.label, t.ref({
// message(t: Turn, [newValue]: [number]): void {
// if (newValue === 30000) {
// console.log('BOOM!');
// t.message(revoker, true);
// }
// }
// })));
// });
2021-02-24 20:48:55 +00:00
const ds_for_box = attenuate(
ds,
2021-03-23 11:18:57 +00:00
rfilter(
2021-03-23 18:18:26 +00:00
Pattern.PCompound(PCompound({
ctor: ConstructorSpec.CRec(CRec({ label: $BoxState, arity: 1 })),
members: new Dictionary()
})),
Pattern.PCompound(PCompound({
ctor: ConstructorSpec.CRec(CRec({ label: $Observe, arity: 2 })),
members: new Dictionary<Ref, Pattern>([
[0, Pattern.Lit(Lit($SetBox))]])
}))));
const ds_for_client = attenuate(
2021-03-03 10:45:01 +00:00
ds_unproxied,
2021-03-23 11:18:57 +00:00
rfilter(
2021-03-23 18:18:26 +00:00
Pattern.PCompound(PCompound({
ctor: ConstructorSpec.CRec(CRec({ label: $SetBox, arity: 1 })),
members: new Dictionary()
})),
Pattern.PCompound(PCompound({
ctor: ConstructorSpec.CRec(CRec({ label: $Observe, arity: 2 })),
members: new Dictionary<Ref, Pattern>([
[0, Pattern.Lit(Lit($BoxState))]])
}))));
2021-03-02 12:53:33 +00:00
const boxpath = path.join(__dirname, 'box.js');
const clientpath = path.join(__dirname, 'client.js');
2021-03-23 18:18:26 +00:00
// spawnModule(t, boxpath, [ds_for_box, 500000, 25000]);
spawnWorker(t, boxpath, [ds_for_box, 50000, 2500]);
2021-02-24 20:48:55 +00:00
2021-03-02 12:53:33 +00:00
spawnModule(t, clientpath, ds_for_client);
// spawnWorker(t, clientpath, ds_for_client);
2021-02-22 09:12:10 +00:00
});