Experiment: run facet teardown in the current turn, immediately, instead of delayed

This commit is contained in:
Tony Garnock-Jones 2021-12-24 17:29:26 -05:00
parent 8f2d598201
commit ed8adae3f2
1 changed files with 14 additions and 19 deletions

View File

@ -117,14 +117,14 @@ export class Actor {
this.exitHooks.push(a); this.exitHooks.push(a);
} }
terminateWith(reason: Exclude<ExitReason, null>) { _terminateWith(reason: Exclude<ExitReason, null>) {
if (this.exitReason !== null) return; if (this.exitReason !== null) return;
this.exitReason = reason; this.exitReason = reason;
if (!reason.ok) { if (!reason.ok) {
console.error(`${this} crashed:`, reason.err); console.error(`${this} crashed:`, reason.err);
} }
this.exitHooks.forEach(hook => hook()); this.exitHooks.forEach(hook => hook());
queueTask(() => Turn.for(this.root, () => this.root._terminate(reason.ok), true)); this.root._terminate(reason.ok);
} }
repairDataflowGraph() { repairDataflowGraph() {
@ -207,15 +207,13 @@ export class Facet {
this.outbound.forEach(e => Turn.active._retract(e)); this.outbound.forEach(e => Turn.active._retract(e));
if (orderly) { if (orderly) {
queueTask(() => { if (parent) {
if (parent) { if (parent.isInert()) {
if (parent.isInert()) { parent._terminate(true);
Turn.for(parent, () => parent._terminate(true));
}
} else {
Turn.for(this.actor.root, () => this.actor.terminateWith({ ok: true }), true);
} }
}); } else {
this.actor._terminateWith({ ok: true });
}
} }
}); });
} }
@ -278,7 +276,7 @@ export class Turn {
} }
t.deliver(); t.deliver();
} catch (err) { } catch (err) {
Turn.for(facet.actor.root, () => facet.actor.terminateWith({ ok: false, err })); Turn.for(facet.actor.root, () => facet.actor._terminateWith({ ok: false, err }));
} }
} }
@ -314,14 +312,11 @@ export class Turn {
} }
stop(facet: Facet = this.activeFacet, continuation?: LocalAction) { stop(facet: Facet = this.activeFacet, continuation?: LocalAction) {
const facetParent = facet.parent; if (facet.parent === null) {
if (facetParent === null) {
this.stopActor(); this.stopActor();
} else { } else {
this.enqueue(facet, () => { if (continuation) facet.onStop(continuation);
if (continuation) facet.onStop(continuation); facet._terminate(true);
facet._terminate(true);
});
} }
} }
@ -361,11 +356,11 @@ export class Turn {
} }
stopActor(): void { stopActor(): void {
this.enqueue(this.activeFacet.actor.root, () => this.activeFacet.actor.terminateWith({ ok: true })); this.enqueue(this.activeFacet.actor.root, () => this.activeFacet.actor._terminateWith({ ok: true }));
} }
crash(err: Error): void { crash(err: Error): void {
this.enqueue(this.activeFacet.actor.root, () => this.activeFacet.actor.terminateWith({ ok: false, err })); this.enqueue(this.activeFacet.actor.root, () => this.activeFacet.actor._terminateWith({ ok: false, err }));
} }
field<V extends Value<T>, T = Ref>(initial: V, name?: string): Field<V, T> { field<V extends Value<T>, T = Ref>(initial: V, name?: string): Field<V, T> {