Global Dataspace and ActorSpace available

This commit is contained in:
Tony Garnock-Jones 2023-12-01 20:53:18 +01:00
parent 1979a439f8
commit ed605718e5
8 changed files with 46 additions and 17 deletions

View File

@ -182,7 +182,7 @@ ${joinItems(sa.captureBinders.map(binderTypeGuard(t)), '\n')}
: ``;
*/
const n = spawn.name === void 0 ? '' : t` __SYNDICATE__.Turn.activeFacet.actor.name = ${walk(spawn.name)};`;
return t`__SYNDICATE__.Turn.active._spawn${spawn.linkedToken ? 'Link': ''}(() => {${n} ${inject} ${walk(spawn.body)} });`;
return t`__SYNDICATE__.Dataspace._spawn${spawn.linkedToken ? 'Link': ''}(() => {${n} ${inject} ${walk(spawn.body)} });`;
}
x(ctx.parser.spawn, expandSpawn);

View File

@ -29,6 +29,7 @@
"types": "lib/index.d.ts",
"author": "Tony Garnock-Jones <tonyg@leastfixedpoint.com>",
"devDependencies": {
"@preserves/core": "0.992",
"@preserves/schema-cli": "0.992"
},
"peerDependencies": {

View File

@ -8,6 +8,7 @@ import { ActorSpace } from './space.js';
import { ActionDescription, StructuredTask, TaskAction } from './task.js';
import { randomId } from './randomid.js';
import * as Q from '../gen/queuedTasks.js';
import { Dataspace } from './dataspace.js';
export type AnyValue = Value<Ref>;
@ -101,15 +102,11 @@ export class Actor {
exitReason: ExitReason = null;
readonly exitHooks: Array<LocalAction> = [];
static boot(bootProc: LocalAction, space: ActorSpace, initialAssertions?: OutboundMap): Actor;
static boot(bootProc: LocalAction, initialAssertions?: OutboundMap): Actor;
static boot(bootProc: LocalAction, spaceOrInitialAssertions?: ActorSpace | OutboundMap, initialAssertions0?: OutboundMap): Actor {
const space: ActorSpace = (spaceOrInitialAssertions && !Map.isMap(spaceOrInitialAssertions))
? spaceOrInitialAssertions
: new ActorSpace();
const initialAssertions: OutboundMap | undefined = (spaceOrInitialAssertions && Map.isMap(spaceOrInitialAssertions))
? spaceOrInitialAssertions
: (spaceOrInitialAssertions ? initialAssertions0 : void 0);
static boot(
bootProc: LocalAction,
initialAssertions: OutboundMap = new Map(),
space = new ActorSpace(),
): Actor {
const newActor = new Actor(space, initialAssertions);
newActor._boot(bootProc);
return newActor;

View File

@ -3,7 +3,7 @@
import { IdentityMap, KeyedDictionary, stringify } from '@preserves/core';
import { Index, IndexObserver } from './skeleton.js';
import { Actor, AnyValue, Assertion, Entity, Facet, Handle, LocalAction, Ref, Turn } from './actor.js';
import { Actor, AnyValue, Assertion, DetailedAction, Entity, Facet, Handle, LocalAction, Ref, Turn } from './actor.js';
import { Observe, toObserve } from '../gen/dataspace.js';
import * as P from '../gen/dataspacePatterns.js';
@ -46,6 +46,36 @@ export class DataspaceObserver implements IndexObserver<Turn> {
}
export class Dataspace implements Partial<Entity> {
private static _global: Ref | undefined = void 0;
static get global(): Ref {
if (Dataspace._global === void 0) {
Dataspace.boot(ds => {
// Cast to any because `global` is otherwise readonly (!)
Dataspace._global = ds;
});
}
return Dataspace._global!;
}
// Alias for syndicatec code generator to use, plus hook for fallback use outside a Turn
static _spawnLink(bootProc: LocalAction | DetailedAction): Actor | null {
if (Turn.active) {
return Turn.active._spawnLink(bootProc);
} else {
throw new Error("Cannot spawnLink outside an active Turn");
}
}
// Alias for syndicatec code generator to use, plus hook for fallback use outside a Turn
static _spawn(bootProc: LocalAction | DetailedAction): Actor {
if (Turn.active) {
return Turn.active._spawn(bootProc);
} else {
return Actor.boot(bootProc, void 0, Dataspace.global.relay.actor.space);
}
}
readonly options: DataspaceOptions;
readonly index = new Index();
readonly handleMap = new IdentityMap<Handle, Assertion>();

View File

@ -1,7 +1,7 @@
/// SPDX-License-Identifier: GPL-3.0-or-later
/// SPDX-FileCopyrightText: Copyright © 2016-2023 Tony Garnock-Jones <tonyg@leastfixedpoint.com>
import { randomId, Observe, FlexMap, embed, Embedded, Ref, Turn, AnyValue } from "@syndicate-lang/core";
import { randomId, Observe, FlexMap, embed, Embedded, Ref, Turn, AnyValue, Dataspace } from "@syndicate-lang/core";
import { QuasiValue as Q } from "@syndicate-lang/core";
import * as P from "./protocol";
@ -11,7 +11,7 @@ export type UIFragmentRecord = ReturnType<typeof P.UIFragment>;
import { HtmlFragments } from "./html";
export * from "./html";
export function boot(ds: Ref) {
export function boot(ds = Dataspace.global) {
spawnGlobalEventFactory(ds);
spawnWindowEventFactory(ds);
spawnUIFragmentFactory(ds);

View File

@ -1,7 +1,7 @@
/// SPDX-License-Identifier: GPL-3.0-or-later
/// SPDX-FileCopyrightText: Copyright © 2016-2023 Tony Garnock-Jones <tonyg@leastfixedpoint.com>
import { preserves, DoubleFloat, Observe, floatValue, Turn, Ref, Schemas } from "@syndicate-lang/core";
import { preserves, DoubleFloat, Observe, floatValue, Turn, Ref, Schemas, Dataspace } from "@syndicate-lang/core";
import { QuasiValue as Q } from "@syndicate-lang/core";
export message type PeriodicTick(interval: DoubleFloat);
@ -20,7 +20,7 @@ export function sleep(ds: Ref, seconds: number, cb: () => void): void {
}
}
export function boot(ds: Ref) {
export function boot(ds = Dataspace.global) {
spawn named 'timer/PeriodicTick' {
at ds {
during Observe({

View File

@ -4,6 +4,7 @@
import {
Assertion,
Bytes,
Dataspace,
Embedded,
IdentitySet,
Observe,
@ -31,7 +32,7 @@ type TransportState = {
peer: Ref,
};
export function boot(ds: Ref, debug: boolean = false, WebSocketConstructor?: typeof WebSocket) {
export function boot(ds = Dataspace.global, debug: boolean = false, WebSocketConstructor?: typeof WebSocket) {
spawn named 'transportConnector' {
at ds {
during Observe({ "pattern": :pattern G.TransportConnection({

View File

@ -1403,7 +1403,7 @@
dependencies:
"@octokit/openapi-types" "^12.11.0"
"@preserves/core@*", "@preserves/core@^0.992.3":
"@preserves/core@*", "@preserves/core@0.992", "@preserves/core@^0.992.3":
version "0.992.3"
resolved "https://registry.yarnpkg.com/@preserves/core/-/core-0.992.3.tgz#776a43db71c95cc4d1c276ebe85aef39587a6f4c"
integrity sha512-thDNEIAvD1j4OZqAItXEdKSR/wklKp/lCY2Lxdmj8oLRM82jaTzvlvkQwUIK08zcV3QVBaIed/UeG4J1W4Jlrw==