From dc5c97b027b55c3910187ff84100549f731f5c51 Mon Sep 17 00:00:00 2001 From: Tony Garnock-Jones Date: Mon, 17 May 2021 16:26:01 +0200 Subject: [PATCH] Major update to @preserves/core from old preserves package. --- packages/compiler/src/compiler/codegen.ts | 8 ++--- packages/core/package.json | 2 +- packages/core/src/index.ts | 2 +- packages/core/src/runtime/assertions.ts | 2 +- packages/core/src/runtime/bag.ts | 7 ++-- packages/core/src/runtime/dataflow.ts | 2 +- packages/core/src/runtime/dataspace.ts | 39 +++++++++++------------ packages/core/src/runtime/mapset.ts | 2 +- packages/core/src/runtime/randomid.ts | 2 +- packages/core/src/runtime/relay.ts | 4 +-- packages/core/src/runtime/skeleton.ts | 6 ++-- packages/core/src/runtime/worker.ts | 2 +- packages/html/examples/table/src/index.ts | 4 +-- packages/html/src/index.ts | 12 +++---- yarn.lock | 10 +++--- 15 files changed, 51 insertions(+), 53 deletions(-) diff --git a/packages/compiler/src/compiler/codegen.ts b/packages/compiler/src/compiler/codegen.ts index 8b69c16..816f1c9 100644 --- a/packages/compiler/src/compiler/codegen.ts +++ b/packages/compiler/src/compiler/codegen.ts @@ -207,14 +207,14 @@ export function expand(tree: Items, ctx: ExpansionContext): Items { // TODO: untyped template const sa = compilePattern(s.pattern); return t`withSelfDo(function (thisFacet) { - const _Facets = new __SYNDICATE__.Dictionary<__SYNDICATE__.Facet>(); + const _Facets = new __SYNDICATE__.Dictionary>(); on asserted ${patternText(s.pattern)} => react { _Facets.set([${commaJoin(sa.captureBinders.map(t=>[t.id]))}], thisFacet); dataflow { } // TODO: horrible hack to keep the facet alive if no other endpoints ${s.body} } on retracted ${patternText(s.pattern)} => { - const ${ctx.argDecl(t, '_Key', '__SYNDICATE__.Value[]')} = + const ${ctx.argDecl(t, '_Key', '__SYNDICATE__.Value[]')} = [${commaJoin(sa.captureBinders.map(t=>[t.id]))}]; _Facets.get(_Key)?._stop(); _Facets.delete(_Key); @@ -280,7 +280,7 @@ export function expand(tree: Items, ctx: ExpansionContext): Items { constPaths: ${JSON.stringify(sa.constPaths)}, constVals: [${commaJoin(sa.constVals.map(walk))}], capturePaths: ${JSON.stringify(sa.capturePaths)}, - callback: thisFacet.wrap((thisFacet, __Evt, ${ctx.argDecl(t, '__vs', 'Array<__SYNDICATE__.Value>')}) => { + callback: thisFacet.wrap((thisFacet, __Evt, ${ctx.argDecl(t, '__vs', 'Array<__SYNDICATE__.Value>')}) => { if (__Evt === __SYNDICATE__.Skeleton.EventType.${expectedEvt}) { ${joinItems(sa.captureBinders.map(binderTypeGuard(t)), '\n')} thisFacet.scheduleScript(() => {${terminalWrap(t, s.terminal, walk(s.body))}}); @@ -296,7 +296,7 @@ ${joinItems(sa.captureBinders.map(binderTypeGuard(t)), '\n')} const l = `Symbol.for(${JSON.stringify(s.label.text)})`; const fns = JSON.stringify(s.fields.map(f => f.id.text)); const fs = ctx.typescript - ? t`<${facetFieldObjectType(t, s.fields, t`__SYNDICATE__.Value`)}>` + ? t`<${facetFieldObjectType(t, s.fields, t`__SYNDICATE__.Value`)}, any>` : ''; return t`const ${[s.label]} = __SYNDICATE__.Record.makeConstructor${fs}()(${maybeWalk(s.wireName) ?? l}, ${fns});`; }); diff --git a/packages/core/package.json b/packages/core/package.json index c12384e..916a8af 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -22,6 +22,6 @@ "types": "lib/index.d.ts", "author": "Tony Garnock-Jones ", "dependencies": { - "preserves": "0.6.2" + "@preserves/core": "0.15.0" } } diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index da0b36d..f5bea2a 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -16,7 +16,7 @@ // along with this program. If not, see . //--------------------------------------------------------------------------- -export * from 'preserves'; +export * from '@preserves/core'; export * from './runtime/randomid.js'; export * from './runtime/assertions.js'; diff --git a/packages/core/src/runtime/assertions.ts b/packages/core/src/runtime/assertions.ts index 99e26ac..15b3179 100644 --- a/packages/core/src/runtime/assertions.ts +++ b/packages/core/src/runtime/assertions.ts @@ -16,7 +16,7 @@ // along with this program. If not, see . //--------------------------------------------------------------------------- -import { Record, RecordConstructor, AsPreserve, Value } from 'preserves'; +import { Record, RecordConstructor, AsPreserve, Value } from '@preserves/core'; export class Seal { readonly contents: any; diff --git a/packages/core/src/runtime/bag.ts b/packages/core/src/runtime/bag.ts index 5acea49..12f79d2 100644 --- a/packages/core/src/runtime/bag.ts +++ b/packages/core/src/runtime/bag.ts @@ -18,7 +18,8 @@ // Bags and Deltas (which are Bags where item-counts can be negative). -import { Value, Set, Dictionary } from 'preserves'; +import { GenericEmbedded } from '@preserves/core'; +import { Value, Set, Dictionary } from '@preserves/core'; export enum ChangeDescription { PRESENT_TO_ABSENT = -1, @@ -28,7 +29,7 @@ export enum ChangeDescription { } export class Bag { - _items: Dictionary; + _items: Dictionary; constructor(s?: Set) { this._items = new Dictionary(); @@ -83,7 +84,7 @@ export class Bag { this._items.forEach(f); } - snapshot(): Dictionary { + snapshot(): Dictionary { return this._items.clone(); } diff --git a/packages/core/src/runtime/dataflow.ts b/packages/core/src/runtime/dataflow.ts index fef04d7..8b99e04 100644 --- a/packages/core/src/runtime/dataflow.ts +++ b/packages/core/src/runtime/dataflow.ts @@ -18,7 +18,7 @@ // Property-based "dataflow" -import { FlexSet, FlexMap, Canonicalizer } from 'preserves'; +import { FlexSet, FlexMap, Canonicalizer } from '@preserves/core'; import * as MapSet from './mapset.js'; export interface PropertyOptions { diff --git a/packages/core/src/runtime/dataspace.ts b/packages/core/src/runtime/dataspace.ts index d13eb7e..70afe17 100644 --- a/packages/core/src/runtime/dataspace.ts +++ b/packages/core/src/runtime/dataspace.ts @@ -16,7 +16,7 @@ // along with this program. If not, see . //--------------------------------------------------------------------------- -import { Value, fromJS, is, Set } from 'preserves'; +import { Value, is, Set } from '@preserves/core'; import * as Skeleton from './skeleton.js'; import { Bag, ChangeDescription } from './bag.js'; @@ -42,11 +42,11 @@ export type EndpointId = ActorId; export type Task = () => T; export type Script = (this: Fields & DataflowObservableObject, f: Facet) => T; -export type MaybeValue = Value | undefined; +export type MaybeValue = Value | undefined; export type EndpointSpec = { assertion: MaybeValue, analysis: Skeleton.Analysis | null }; export type ObserverCallback = - (this: Fields, facet: Facet, bindings: Array) => void; + (this: Fields, facet: Facet, bindings: Array>) => void; export type ObserverCallbacks = { add?: ObserverCallback; @@ -150,7 +150,7 @@ export abstract class Dataspace { applyPatch(ac: Actor, delta: Bag) { // if (!delta.isEmpty()) debug('applyPatch BEGIN', ac && ac.toString()); - let removals: Array<[number, Value]> = []; + let removals: Array<[number, Value]> = []; delta.forEach((count, a) => { if (count > 0) { // debug('applyPatch +', a && a.toString()); @@ -167,7 +167,7 @@ export abstract class Dataspace { // if (!delta.isEmpty()) debug('applyPatch END'); } - deliverMessage(m: Value, _sendingActor: Actor | null) { + deliverMessage(m: Value, _sendingActor: Actor | null) { // debug('deliverMessage', sendingActor && sendingActor.toString(), m.toString()); this.index.deliverMessage(m); // this.index.deliverMessage(m, (leaf, _m) => { @@ -175,7 +175,7 @@ export abstract class Dataspace { // }); } - adjustIndex(a: Value, count: number) { + adjustIndex(a: Value, count: number) { return this.index.adjustAssertion(a, count); } @@ -303,18 +303,16 @@ export class Actor { return p; } - assert(a: Value) { this.pendingPatch().adjust(a, +1); } - retract(a: Value) { this.pendingPatch().adjust(a, -1); } + assert(a: Value) { this.pendingPatch().adjust(a, +1); } + retract(a: Value) { this.pendingPatch().adjust(a, -1); } - adhocRetract(a: Value) { - a = fromJS(a); + adhocRetract(a: Value) { if (this.adhocAssertions.change(a, -1, true) === ChangeDescription.PRESENT_TO_ABSENT) { this.retract(a); } } - adhocAssert(a: Value) { - a = fromJS(a); + adhocAssert(a: Value) { if (this.adhocAssertions.change(a, +1) === ChangeDescription.ABSENT_TO_PRESENT) { this.assert(a); } @@ -343,17 +341,17 @@ class Patch extends Action { ds.applyPatch(ac!, this.changes); } - adjust(a: Value, count: number) { - this.changes.change(fromJS(a), count); + adjust(a: Value, count: number) { + this.changes.change(a, count); } } class Message extends Action { - readonly body: Value; + readonly body: Value; - constructor(body: any) { + constructor(body: Value) { super(); - this.body = fromJS(body); + this.body = body; } perform(ds: Dataspace, ac: Actor | null): void { @@ -577,7 +575,7 @@ export class Facet { addObserverEndpoint(specThunk: (facet: Facet) => MaybeValue, callbacks: ObserverCallbacks): Endpoint { const scriptify = (f?: ObserverCallback) => - f && ((facet: Facet, vs: Array) => + f && ((facet: Facet, vs: Array>) => facet.scheduleScript(() => f.call(facet.fields, facet, vs))); return this._addRawObserverEndpoint(specThunk, { add: scriptify(callbacks.add), @@ -667,11 +665,11 @@ export class Facet { } // This alias exists because of the naive expansion done by the parser. - _send(body: any) { + _send(body: Value) { this.send(body); } - send(body: any) { + send(body: Value) { this.ensureNonFacetSetup('`send`'); this.enqueueScriptAction(new Message(body)); } @@ -772,7 +770,6 @@ export class Endpoint { refresh() { let newSpec = this.updateFun.call(this.facet.fields, this.facet); - if (newSpec.assertion !== void 0) newSpec.assertion = fromJS(newSpec.assertion); if (!is(newSpec.assertion, this.spec.assertion)) { this._uninstall(true); this._install(newSpec); diff --git a/packages/core/src/runtime/mapset.ts b/packages/core/src/runtime/mapset.ts index 411166d..41e9649 100644 --- a/packages/core/src/runtime/mapset.ts +++ b/packages/core/src/runtime/mapset.ts @@ -18,7 +18,7 @@ // Utilities for Maps of Sets -import { FlexSet, FlexMap, Canonicalizer } from 'preserves'; +import { FlexSet, FlexMap, Canonicalizer } from '@preserves/core'; export function add(m: FlexMap>, k: K, v: V, c: Canonicalizer) { let s = m.get(k); diff --git a/packages/core/src/runtime/randomid.ts b/packages/core/src/runtime/randomid.ts index f202813..ed6aa9e 100644 --- a/packages/core/src/runtime/randomid.ts +++ b/packages/core/src/runtime/randomid.ts @@ -16,7 +16,7 @@ // along with this program. If not, see . //--------------------------------------------------------------------------- -import { Bytes } from 'preserves'; +import { Bytes } from '@preserves/core'; import * as node_crypto from 'crypto'; export function _btoa(s: string): string { diff --git a/packages/core/src/runtime/relay.ts b/packages/core/src/runtime/relay.ts index 03dfe3c..05ebc98 100644 --- a/packages/core/src/runtime/relay.ts +++ b/packages/core/src/runtime/relay.ts @@ -16,7 +16,7 @@ // along with this program. If not, see . //--------------------------------------------------------------------------- -import { Value, Record } from 'preserves'; +import { embed, Value } from '@preserves/core'; import { $Special } from './special.js'; import { Dataspace, Facet, Actor, Endpoint, Script } from './dataspace.js'; @@ -25,7 +25,7 @@ import { ChangeDescription } from './bag.js'; import { EventType, Analysis } from './skeleton.js'; import { Ground } from './ground.js'; -export const $QuitDataspace = new $Special("quit-dataspace"); +export const $QuitDataspace = embed(new $Special("quit-dataspace")); export class NestedDataspace extends Dataspace { readonly outerFacet: Facet<{}>; diff --git a/packages/core/src/runtime/skeleton.ts b/packages/core/src/runtime/skeleton.ts index 428698a..050c888 100644 --- a/packages/core/src/runtime/skeleton.ts +++ b/packages/core/src/runtime/skeleton.ts @@ -17,7 +17,7 @@ //--------------------------------------------------------------------------- import { IdentitySet } from './idcoll.js'; -import { is, Value, Record, Set, Dictionary, canonicalString, RecordConstructorInfo, Tuple } from 'preserves'; +import { is, Value, Record, Set, Dictionary, canonicalString, RecordConstructorInfo, GenericEmbedded } from '@preserves/core'; import { Bag, ChangeDescription } from './bag.js'; import { Discard, Capture } from './assertions.js'; @@ -255,7 +255,7 @@ function parseSelector(s: string): { popCount: number, index: number } { class Continuation { readonly cachedAssertions: Set; - readonly leafMap: Dictionary> = new Dictionary(); + readonly leafMap: Dictionary> = new Dictionary(); constructor(cachedAssertions: Set) { this.cachedAssertions = cachedAssertions; @@ -264,7 +264,7 @@ class Continuation { class Leaf { readonly cachedAssertions: Set = new Set(); - readonly handlerMap: Dictionary = new Dictionary(); + readonly handlerMap: Dictionary = new Dictionary(); isEmpty(): boolean { return this.cachedAssertions.size === 0 && this.handlerMap.size === 0; diff --git a/packages/core/src/runtime/worker.ts b/packages/core/src/runtime/worker.ts index 23db2da..989ae16 100644 --- a/packages/core/src/runtime/worker.ts +++ b/packages/core/src/runtime/worker.ts @@ -20,7 +20,7 @@ // import { Observe, Outbound, Inbound, Capture, Discard } from './assertions.js'; // import * as Skeleton from './skeleton.js'; -// import { preserves, Value, Bytes, Record, Dictionary, encode, decode } from 'preserves'; +// import { preserves, Value, Bytes, Record, Dictionary, encode, decode } from '@preserves/core'; // type MessageHandler = (e: Bytes) => void; // type ImplementationType = 'node.js' | 'browser' | 'none'; diff --git a/packages/html/examples/table/src/index.ts b/packages/html/examples/table/src/index.ts index 8f18643..8ceb685 100644 --- a/packages/html/examples/table/src/index.ts +++ b/packages/html/examples/table/src/index.ts @@ -16,7 +16,7 @@ // along with this program. If not, see . //--------------------------------------------------------------------------- -import { Value } from '@syndicate-lang/core'; +import { Embedded, Value } from '@syndicate-lang/core'; activate import { UIEvent, GlobalEvent, HtmlFragments, template, Anchor } from '@syndicate-lang/html'; assertion type Person(id, firstName, lastName, address, age); @@ -56,7 +56,7 @@ boot { spawn named 'controller' { on message GlobalEvent('table#the-table th', 'click', $e) => { send message SetSortColumn( - JSON.parse(((e as unknown as Event).target as HTMLElement).dataset.column!)); + JSON.parse(((e as Embedded).embeddedValue.target as HTMLElement).dataset.column!)); } } diff --git a/packages/html/src/index.ts b/packages/html/src/index.ts index 85e8fd9..6952830 100644 --- a/packages/html/src/index.ts +++ b/packages/html/src/index.ts @@ -16,7 +16,7 @@ // along with this program. If not, see . //--------------------------------------------------------------------------- -import { randomId, Facet, Observe, FlexMap, Value, Record } from "@syndicate-lang/core"; +import { randomId, Facet, Observe, FlexMap, Value, embed, Embedded } from "@syndicate-lang/core"; import * as P from "./protocol"; export * from "./protocol"; @@ -52,7 +52,7 @@ export function spawnGlobalEventFactory(thisFacet: Facet) { during Observe(P.GlobalEvent($selector: string, $eventType: string, _)) => spawn named ['GlobalEvent', selector, eventType] { let sender = thisFacet.wrapExternal((thisFacet, e: Event) => { - send message P.GlobalEvent(selector, eventType, e); + send message P.GlobalEvent(selector, eventType, embed(e)); }); function handler(event: Event) { @@ -81,7 +81,7 @@ export function spawnWindowEventFactory(thisFacet: Facet) { during Observe(P.WindowEvent($eventType: string, _)) => spawn named ['WindowEvent', eventType] { let sender = thisFacet.wrapExternal((thisFacet, e: Event) => { - send message P.WindowEvent(eventType, e); + send message P.WindowEvent(eventType, embed(e)); }); let handler = function (event: Event) { @@ -148,7 +148,7 @@ function spawnUIFragmentFactory(thisFacet: Facet) { removeNodes(); selector = newSelector; - html = newHtml as Array; + html = (newHtml as Embedded>).embeddedValue; orderBy = newOrderBy; anchorNodes = (selector !== null) ? selectorMatch(document.body, selector) : []; @@ -188,7 +188,7 @@ function spawnUIFragmentFactory(thisFacet: Facet) { let handlerClosure: HandlerClosure; if (!eventRegistrations.has(key)) { let sender = thisFacet.wrapExternal((thisFacet, e: Event) => { - send message P.UIEvent(fragmentId, selector, eventType, e); + send message P.UIEvent(fragmentId, selector, eventType, embed(e)); }); function handler(event: Event) { sender(event); @@ -478,7 +478,7 @@ export class Anchor { } html(selector: string, html: HtmlFragments, orderBy: NodeOrderKey = ''): ReturnType { - return P.UIFragment(this.fragmentId, selector, html.nodes(), orderBy); + return P.UIFragment(this.fragmentId, selector, embed(html.nodes()), orderBy); } } diff --git a/yarn.lock b/yarn.lock index 7e16e2b..3017c23 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1366,6 +1366,11 @@ dependencies: "@octokit/openapi-types" "^6.1.1" +"@preserves/core@0.15.0": + version "0.15.0" + resolved "https://registry.yarnpkg.com/@preserves/core/-/core-0.15.0.tgz#55a14288442d404d20a2906b92b7a7cc9e522a18" + integrity sha512-PoEvwlqNNXpYykwkiD7KyjT6kfo78XXEMwJ5yOhOiEF6nVD167NVv801/DR7xIINtPDaDjdqBtqY/tamyzi7vA== + "@rollup/plugin-node-resolve@^11.0.1": version "11.2.1" resolved "https://registry.yarnpkg.com/@rollup/plugin-node-resolve/-/plugin-node-resolve-11.2.1.tgz#82aa59397a29cd4e13248b106e6a4a1880362a60" @@ -5781,11 +5786,6 @@ prelude-ls@~1.1.2: resolved "https://registry.yarnpkg.com/prelude-ls/-/prelude-ls-1.1.2.tgz#21932a549f5e52ffd9a827f570e04be62a97da54" integrity sha1-IZMqVJ9eUv/ZqCf1cOBL5iqX2lQ= -preserves@0.6.2: - version "0.6.2" - resolved "https://registry.yarnpkg.com/preserves/-/preserves-0.6.2.tgz#9102dae1e619c4d88459cd6a0d49c745bb687d1d" - integrity sha512-Hinv4i8HM9iUti4TzdFBkJOzQxn9LhcDWqbH4NgUTN39CBwwEkTEDCPMilf49OU+JEQgwvCIsfO/566j1dELUQ== - pretty-format@^26.0.0, pretty-format@^26.6.2: version "26.6.2" resolved "https://registry.yarnpkg.com/pretty-format/-/pretty-format-26.6.2.tgz#e35c2705f14cb7fe2fe94fa078345b444120fc93"