From e1d5e4cd5397a02cb5216b7941c87c08834b4f9c Mon Sep 17 00:00:00 2001 From: Tony Garnock-Jones Date: Mon, 22 Feb 2021 20:08:11 +0100 Subject: [PATCH] Tighter types --- actor.ts | 10 ++++++++++ main.ts | 9 ++++----- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/actor.ts b/actor.ts index f3a5ea1..8b5f0d2 100644 --- a/actor.ts +++ b/actor.ts @@ -104,6 +104,10 @@ export class Turn { return this._ensureActor(what).ref(t); } + entity(t: Partial): Ref { + return this.ref({ ... NULL_ENTITY, ... t }, "entity"); + } + spawn(bootProc: (t: Turn) => void, initialAssertions?: IdentitySet): void { if ((initialAssertions !== void 0) && (initialAssertions.size > 0)) { this._ensureActor("spawn with initialAssertions"); @@ -181,6 +185,12 @@ export class Turn { } } +export const NULL_ENTITY: Entity = { + [assert](_t: Turn, _assertion: Assertion, _handle: Handle): void {}, + [retract](_t: Turn, _handle: Handle): void {}, + [message](_t: Turn, _assertion: Assertion): void {}, +}; + function extractFromMap(map: Map, keys?: IdentitySet): Map { const result: Map = new Map(); if (keys !== void 0) { diff --git a/main.ts b/main.ts index 4d14ccd..0187821 100644 --- a/main.ts +++ b/main.ts @@ -111,7 +111,7 @@ Turn.for(null, async (t: Turn) => { valueHandle = t.replace(ds, valueHandle, BoxState(value)); } setValue(t, 0); - t.assert(ds, Observe(SetBox.constructorInfo.label, t.ref({ + t.assert(ds, Observe(SetBox.constructorInfo.label, t.entity({ [message](t: Turn, [newValue]: [number]): void { if (newValue % 25000 === 0) { const endTime = Date.now(); @@ -131,7 +131,7 @@ Turn.for(null, async (t: Turn) => { t.spawn(t => { console.log('Spawning Client'); let count = 0; - t.assert(ds, Observe(BoxState.constructorInfo.label, t.ref({ + t.assert(ds, Observe(BoxState.constructorInfo.label, t.entity({ [assert](t: Turn, [currentValue]: [number]): void { // console.log(`Client: got ${currentValue}`); if (currentValue === 300000) { @@ -140,10 +140,9 @@ Turn.for(null, async (t: Turn) => { } else { t.message(ds, SetBox(currentValue + 1)); } - }, - [retract]() {} + } }))); - t.assert(ds, Observe(BoxState.constructorInfo.label, t.ref({ + t.assert(ds, Observe(BoxState.constructorInfo.label, t.entity({ [assert](_t: Turn, _assertion: Assertion): void { count++; }, [retract](t: Turn, _handle: Handle) { if (--count === 0) {