From 2a2b7da4bc6ee27419a176104f73d92fcf8ce1b2 Mon Sep 17 00:00:00 2001 From: Tony Garnock-Jones Date: Sun, 28 May 2023 12:03:45 +0200 Subject: [PATCH] Switch to proper schema --- .../local-protocols/schemas/queuedTasks.prs | 14 +++++ packages/core/src/runtime/actor.ts | 52 +++++++++---------- packages/core/src/runtime/task.ts | 19 ++----- 3 files changed, 43 insertions(+), 42 deletions(-) create mode 100644 packages/core/local-protocols/schemas/queuedTasks.prs diff --git a/packages/core/local-protocols/schemas/queuedTasks.prs b/packages/core/local-protocols/schemas/queuedTasks.prs new file mode 100644 index 0000000..7c4877d --- /dev/null +++ b/packages/core/local-protocols/schemas/queuedTasks.prs @@ -0,0 +1,14 @@ +version 1 . +embeddedType EntityRef.Cap . + +ActionDescription = +/ @spawnActor +/ @stopActor +/ @inertCheck +/ +/ +/ +/ +. + +OptionalAny = / . diff --git a/packages/core/src/runtime/actor.ts b/packages/core/src/runtime/actor.ts index f781193..ec3bbc8 100644 --- a/packages/core/src/runtime/actor.ts +++ b/packages/core/src/runtime/actor.ts @@ -1,12 +1,13 @@ /// SPDX-License-Identifier: GPL-3.0-or-later /// SPDX-FileCopyrightText: Copyright © 2016-2023 Tony Garnock-Jones -import { IdentitySet, Value, embeddedId, is, fromJS, stringify, Dictionary } from '@preserves/core'; +import { IdentitySet, Value, embeddedId, is, fromJS, stringify, Dictionary, KeyedSet } from '@preserves/core'; import { Cell, Field, Graph } from './dataflow.js'; import { Caveat, runRewrites } from './rewrite.js'; import { ActorSpace } from './space.js'; -import type { ActionDescription, StructuredTask, TaskAction } from './task.js'; +import { ActionDescription, StructuredTask, TaskAction } from './task.js'; import { randomId } from './randomid.js'; +import * as Q from '../gen/queuedTasks.js'; export type AnyValue = Value; @@ -352,7 +353,7 @@ export class Turn { // ^ we trust initialAssertions, so can use `!` safely const newActor = Actor.__unsafeNew(this.activeFacet.actor.space, newOutbound); - const detail = 'detail' in bootProc ? bootProc.detail : void 0; + const detail: Q.OptionalAny = 'detail' in bootProc ? Q.OptionalAny.some(bootProc.detail) : Q.OptionalAny.none(); const spawningFacet = this.activeFacet; this.enqueue(spawningFacet, () => { @@ -362,7 +363,11 @@ export class Turn { describe() { return { type: 'bootActor', detail }; }, }); }, - { type: 'spawnActor', detail, initialAssertions }); + () => { + const a = new KeyedSet(); + initialAssertions.forEach(h => a.add(h)); + return Q.ActionDescription.spawnActor({ detail, initialAssertions: a }); + }); return newActor; } @@ -382,19 +387,16 @@ export class Turn { stopActor(): void { this.enqueue(this.activeFacet.actor.root, () => this.activeFacet.actor._terminateWith({ ok: true }), - { type: 'stopActor', err: void 0 }); + () => Q.ActionDescription.stopActor(Q.OptionalAny.none())); } crash(err: Error): void { this.enqueue(this.activeFacet.actor.root, () => this.activeFacet.actor._terminateWith({ ok: false, err }), - { - type: 'stopActor', - err: Dictionary.fromJS({ - message: err.message, - stack: err.stack ? err.stack : false, - }), - }); + () => Q.ActionDescription.stopActor(Q.OptionalAny.some(Dictionary.fromJS({ + message: err.message, + stack: err.stack ? err.stack : false, + })))); } field(initial: V, name?: string): Field { @@ -448,12 +450,11 @@ export class Turn { e.established = true; ref.target.assert?.(a, h); }, - { - type: 'assert', + () => Q.ActionDescription.assert({ target: ref, handle: h, assertion, - }); + })); } } @@ -482,11 +483,10 @@ export class Turn { e.peer.target.retract?.(e.handle); } }, - { - type: 'retract', + () => Q.ActionDescription.retract({ target: e.peer, handle: e.handle, - }); + })); } sync(ref: Ref): Promise { @@ -496,11 +496,10 @@ export class Turn { _sync(ref: Ref, peer: Ref): void { this.enqueue(ref.relay, () => _sync_impl(ref.target, peer), - { - type: 'sync', + () => Q.ActionDescription.sync({ target: ref, callback: peer, - }); + })); } message(ref: Ref, assertable: Assertable): void { @@ -509,11 +508,10 @@ export class Turn { if (a !== null) { this.enqueue(ref.relay, () => ref.target.message?.(assertion), - { - type: 'message', + () => Q.ActionDescription.message({ target: ref, assertion, - }); + })); } } @@ -541,13 +539,13 @@ export class Turn { }, delayMilliseconds); } - enqueue(relay: Facet, a0: LocalAction, detail: ActionDescription): void { + enqueue(relay: Facet, a0: LocalAction, detail: () => ActionDescription): void { if (this.queues === null) { throw new Error("Attempt to reuse a committed Turn"); } const a: StructuredTask = { perform() { Turn.active._inFacet(relay, a0); }, - describe() { return { targetFacet: relay, action: detail }; }, + describe() { return { targetFacet: relay, action: detail() }; }, }; this.queues.get(relay.actor)?.push(a) ?? this.queues.set(relay.actor, [a]); } @@ -572,6 +570,6 @@ function stopIfInertAfter(a: LocalAction): LocalAction { Turn.active.stop(facet); } }, - { type: 'inertCheck' }); + Q.ActionDescription.inertCheck); }; } diff --git a/packages/core/src/runtime/task.ts b/packages/core/src/runtime/task.ts index 15fb068..6ab4b2c 100644 --- a/packages/core/src/runtime/task.ts +++ b/packages/core/src/runtime/task.ts @@ -1,10 +1,9 @@ /// SPDX-License-Identifier: GPL-3.0-or-later /// SPDX-FileCopyrightText: Copyright © 2023 Tony Garnock-Jones -// Each Turn executes one Task +import type { Facet } from './actor.js'; -import type { AnyValue, Handle, Ref, Facet } from './actor.js'; -import type { IdentitySet } from '@preserves/core'; +import * as q from '../gen/queuedTasks.js'; export type Task = StructuredTask; @@ -14,19 +13,9 @@ export interface StructuredTask { } export type TaskDescription = - | { type: 'bootActor', detail?: AnyValue } + | { type: 'bootActor', detail: q.OptionalAny } | { type: 'turn', tasks: TaskAction[] } ; export type TaskAction = { targetFacet: Facet, action: ActionDescription }; - -export type ActionDescription = - | { type: 'spawnActor', detail?: AnyValue, initialAssertions: IdentitySet } - | { type: 'stopActor', err?: AnyValue } - | { type: 'inertCheck' } - - | { type: 'assert', target: Ref, handle: Handle, assertion: AnyValue } - | { type: 'retract', target: Ref, handle: Handle } - | { type: 'message', target: Ref, assertion: AnyValue } - | { type: 'sync', target: Ref, callback: Ref } -; +export type ActionDescription = q.ActionDescription;