diff --git a/actor.ts b/actor.ts index 8d9e074..f3a5ea1 100644 --- a/actor.ts +++ b/actor.ts @@ -9,8 +9,6 @@ export type ExitReason = null | { ok: true } | { ok: false, err: Error }; export const assert = Symbol('assert'); export const retract = Symbol('retract'); export const message = Symbol('message'); -export const sync = Symbol('sync'); -export const synced = Symbol('synced'); export interface Entity { [assert](turn: Turn, assertion: Assertion, handle: Handle): void; @@ -18,26 +16,23 @@ export interface Entity { [message](turn: Turn, message: Assertion): void; } -export interface SyncTarget { - [sync](turn: Turn, source: Ref): void; -} - -export interface SyncSource { - [synced](turn: Turn): void; -} - -export class Ref { +export class Ref { readonly actor: Actor; readonly target: T; + constructor(actor: Actor, target: T) { this.actor = actor; this.target = target; } + + sync(turn: Turn, syncable: Ref) { + turn.enqueue(syncable.actor, t => syncable.target(t)); + } } -export type OutboundMap = Map; +export type OutboundMap = Map, Assertion]>; -export class Actor implements SyncTarget { +export class Actor { readonly outbound: OutboundMap; exitReason: ExitReason = null; @@ -76,10 +71,6 @@ export class Actor implements SyncTarget { } }); } - - [sync](t: Turn, source: Ref): void { - t.synced(source); - } } let nextHandle = 0; @@ -131,7 +122,7 @@ export class Turn { this.localActions.push(t => actor.stop(t)); } - assert(location: Ref, assertion: Assertion): Handle { + assert(location: Ref, assertion: Assertion): Handle { this._ensureActor("assert"); const h = nextHandle++; this.enqueue(location.actor, t => { @@ -145,31 +136,26 @@ export class Turn { this._retract(this._ensureActor("retract").outbound.get(h)![0], h); } - replace(location: Ref, h: Handle | undefined, assertion: Assertion): Handle { + replace(location: Ref, h: Handle | undefined, assertion: Assertion): Handle { const newHandle = this.assert(location, assertion); if (h !== void 0) this.retract(h); return newHandle; } - _retract(location: Ref, handle: Handle): void { + _retract(location: Ref, handle: Handle): void { this.enqueue(location.actor, t => { this.actor!.outbound.delete(handle); location.target[retract](t, handle); }); } - sync(location: Ref): Promise { + sync(location: Ref): Promise { return new Promise(resolve => { - const k = this.ref({ [synced]: resolve }, "sync"); - this.enqueue(location.actor, t => location.target[sync](t, k)); + this.enqueue(location.actor, t => location.sync(t, this.ref(resolve, "sync"))); }); } - synced(syncable: Ref): void { - this.enqueue(syncable.actor, t => syncable.target[synced](t)); - } - - message(location: Ref, assertion: Assertion): void { + message(location: Ref, assertion: Assertion): void { this.enqueue(location.actor, t => location.target[message](t, assertion)); }