Move exception handling to the right place: inside each turn

This commit is contained in:
Tony Garnock-Jones 2021-03-04 11:29:28 +01:00
parent 01fb3a471a
commit 81a307e03c
1 changed files with 14 additions and 20 deletions

View File

@ -125,20 +125,10 @@ export class Actor {
console.error(`Actor ${this.id} crashed:`, this.exitReason.err);
}
this.exitHooks.forEach(hook => hook(t));
queueTask(() =>
t.freshen(t =>
this.outbound.forEach((peer, h) => t._retract(peer, h))));
}
execute(proc: () => void): void {
queueTask(() => {
if (this.exitReason !== null) return;
try {
proc();
} catch (err) {
Turn.for(this, t => this.terminateWith(t, { ok: false, err }));
}
});
queueTask(() => Turn.for(
t.actor,
t => this.outbound.forEach((peer, h) => t._retract(peer, h)),
true));
}
}
@ -158,11 +148,16 @@ export class Turn {
readonly actor: Actor;
queues: Map<Actor, LocalAction[]> | null = new Map();
static for(actor: Actor, f: LocalAction): void {
static for(actor: Actor, f: LocalAction, zombieTurn = false): void {
if ((actor.exitReason === null) === zombieTurn) return;
const t = new Turn(actor);
f(t);
t.queues!.forEach((q, a) => a.execute(() => q.forEach(f => Turn.for(a, f))));
t.queues = null;
try {
f(t);
t.queues!.forEach((q, a) => queueTask(() => q.forEach(f => Turn.for(a, f))));
t.queues = null;
} catch (err) {
Turn.for(actor, t => actor.terminateWith(t, { ok: false, err }));
}
}
private constructor(actor: Actor) {
@ -180,8 +175,7 @@ export class Turn {
newOutbound.set(key, this.actor.outbound.get(key)!); // we trust initialAssertions
this.actor.outbound.delete(key);
});
const child = new Actor(newOutbound);
child.execute(() => Turn.for(child, bootProc));
queueTask(() => Turn.for(new Actor(newOutbound), bootProc));
});
}