Switch to proper schema

This commit is contained in:
Tony Garnock-Jones 2023-05-28 12:03:45 +02:00
parent 021fdf06bb
commit 2a2b7da4bc
3 changed files with 43 additions and 42 deletions

View File

@ -0,0 +1,14 @@
version 1 .
embeddedType EntityRef.Cap .
ActionDescription =
/ @spawnActor <spawn-actor @detail OptionalAny @initialAssertions #{protocol.Handle}>
/ @stopActor <stop-actor @error OptionalAny>
/ @inertCheck <inert-check>
/ <assert @target #!any @handle protocol.Handle @assertion any>
/ <retract @target #!any @handle protocol.Handle>
/ <message @target #!any @assertion any>
/ <sync @target #!any @callback #!any>
.
OptionalAny = <none> / <some @value any> .

View File

@ -1,12 +1,13 @@
/// SPDX-License-Identifier: GPL-3.0-or-later
/// SPDX-FileCopyrightText: Copyright © 2016-2023 Tony Garnock-Jones <tonyg@leastfixedpoint.com>
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<Ref>;
@ -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<Ref> = '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<number, Ref>();
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<V>(initial: V, name?: string): Field<V> {
@ -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<void> {
@ -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<TaskAction> = {
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);
};
}

View File

@ -1,10 +1,9 @@
/// SPDX-License-Identifier: GPL-3.0-or-later
/// SPDX-FileCopyrightText: Copyright © 2023 Tony Garnock-Jones <tonyg@leastfixedpoint.com>
// 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<TaskDescription>;
@ -14,19 +13,9 @@ export interface StructuredTask<T> {
}
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<Handle> }
| { 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;