The local tasks are just another queue

This commit is contained in:
Tony Garnock-Jones 2021-02-23 15:40:43 +01:00
parent 4045d0d0ed
commit 5669154e96
1 changed files with 4 additions and 7 deletions

View File

@ -80,13 +80,11 @@ type LocalAction = (t: Turn) => void;
export class Turn { export class Turn {
readonly actor: Actor; readonly actor: Actor;
readonly queues: Map<Actor, LocalAction[]> = new Map(); readonly queues: Map<Actor, LocalAction[]> = new Map();
readonly tasks: Array<LocalAction> = [];
static for(actor: Actor, f: LocalAction): void { static for(actor: Actor, f: LocalAction): void {
const t = new Turn(actor); const t = new Turn(actor);
f(t); f(t);
t.queues.forEach((q, a) => a.execute(() => q.forEach(f => Turn.for(a, f)))); t.queues.forEach((q, a) => a.execute(() => q.forEach(f => Turn.for(a, f))));
t.tasks.length && queueMicrotask(() => t.tasks.forEach(f => Turn.for(actor, f)));
} }
private constructor(actor: Actor) { private constructor(actor: Actor) {
@ -98,7 +96,7 @@ export class Turn {
} }
spawn(bootProc: LocalAction, initialAssertions = new IdentitySet<Handle>()): void { spawn(bootProc: LocalAction, initialAssertions = new IdentitySet<Handle>()): void {
this.enqueue(void 0, () => { this.enqueue(this.actor, () => {
const newOutbound: OutboundMap = new Map(); const newOutbound: OutboundMap = new Map();
initialAssertions.forEach(key => { initialAssertions.forEach(key => {
newOutbound.set(key, this.actor.outbound.get(key)!); // we trust initialAssertions newOutbound.set(key, this.actor.outbound.get(key)!); // we trust initialAssertions
@ -110,7 +108,7 @@ export class Turn {
} }
quit(): void { quit(): void {
this.enqueue(void 0, t => this.actor.terminateWith(t, { ok: true })); this.enqueue(this.actor, t => this.actor.terminateWith(t, { ok: true }));
} }
assert(ref: Ref, assertion: Assertion): Handle { assert(ref: Ref, assertion: Assertion): Handle {
@ -148,8 +146,7 @@ export class Turn {
this.enqueue(ref.relay, t => ref[message]?.(t, assertion)); this.enqueue(ref.relay, t => ref[message]?.(t, assertion));
} }
enqueue(relay: Actor | undefined, a: LocalAction): void { enqueue(relay: Actor, a: LocalAction): void {
(relay === void 0) ? this.tasks.push(a) this.queues.get(relay)?.push(a) ?? this.queues.set(relay, [a]);
: this.queues.get(relay)?.push(a) ?? this.queues.set(relay, [a]);
} }
} }