From c318aca51aa3983b231baee85da2816b8a1e4579 Mon Sep 17 00:00:00 2001 From: Tony Garnock-Jones Date: Tue, 23 Feb 2021 10:56:12 +0100 Subject: [PATCH] Simplify --- actor.ts | 30 +++++++++++------------------- main.ts | 2 +- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/actor.ts b/actor.ts index c0f3b8d..ab89587 100644 --- a/actor.ts +++ b/actor.ts @@ -68,55 +68,47 @@ let nextHandle = 0; type LocalAction = (t: Turn) => void; export class Turn { - readonly actor: Actor | null; // whose turn it is to act during this Turn + readonly actor: Actor; readonly queues: Map = new Map(); readonly localActions: Array = []; completed = false; - static for(actor: Actor | null, f: (t: Turn) => void): void { + static for(actor: Actor, f: (t: Turn) => void): void { const t = new Turn(actor); f(t); t.complete(); } - private constructor(actor: Actor | null) { + private constructor(actor: Actor) { this.actor = actor; } - _ensureActor(what: string): Actor { - if (this.actor === null) throw new Error(`Cannot ${what} from non-Actor context`); - return this.actor; - } - - ref(t: T, what: string = "ref"): Ref { - return new Ref(this._ensureActor(what), t); + ref(t: T): Ref { + return new Ref(this.actor, t); } spawn(bootProc: (t: Turn) => void, initialAssertions = new IdentitySet()): void { - if (initialAssertions.size > 0) this._ensureActor("spawn with initialAssertions"); this.localActions.push(() => { - const child = new Actor(this.actor === null - ? void 0 - : extractFromMap(this.actor.outbound, initialAssertions)); + const child = new Actor(extractFromMap(this.actor.outbound, initialAssertions)); child.execute(() => Turn.for(child, bootProc)); }); } quit(): void { - this.localActions.push(t => this._ensureActor("quit").terminateWith(t, { ok: true })); + this.localActions.push(t => this.actor.terminateWith(t, { ok: true })); } assert(location: Ref, assertion: Assertion): Handle { const h = nextHandle++; this.enqueue(location.actor, t => { - this._ensureActor("assert").outbound.set(h, location); + this.actor.outbound.set(h, location); location.target[assert]?.(t, assertion, h); }); return h; } retract(h: Handle): void { - this._retract(this._ensureActor("retract").outbound.get(h)!, h); + this._retract(this.actor.outbound.get(h)!, h); } replace(location: Ref, h: Handle | undefined, assertion: Assertion): Handle { @@ -127,14 +119,14 @@ export class Turn { _retract(location: Ref, handle: Handle): void { this.enqueue(location.actor, t => { - this.actor!.outbound.delete(handle); + this.actor.outbound.delete(handle); location.target[retract]?.(t, handle); }); } sync(location: Ref): Promise { return new Promise(resolve => - this.enqueue(location.actor, t => location.sync(t, this.ref(resolve, "sync")))); + this.enqueue(location.actor, t => location.sync(t, this.ref(resolve)))); } message(location: Ref, assertion: Assertion): void { diff --git a/main.ts b/main.ts index 8a1ff0a..adae0e5 100644 --- a/main.ts +++ b/main.ts @@ -58,7 +58,7 @@ const SetBox = Record.makeConstructor>('SetBox', ['newValue']); let startTime = Date.now(); let prevValue = 0; const LIMIT = 500000; -Turn.for(null, async (t: Turn) => { +Turn.for(new Actor(), async (t: Turn) => { const ds = new Ref(new Actor(), new Dataspace()); // Box