|
|
|
@ -16,7 +16,7 @@
@@ -16,7 +16,7 @@
|
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
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;
@@ -42,11 +42,11 @@ export type EndpointId = ActorId;
|
|
|
|
|
export type Task<T> = () => T; |
|
|
|
|
export type Script<T, Fields> = (this: Fields & DataflowObservableObject, f: Facet<Fields>) => T; |
|
|
|
|
|
|
|
|
|
export type MaybeValue = Value | undefined; |
|
|
|
|
export type MaybeValue = Value<any> | undefined; |
|
|
|
|
export type EndpointSpec = { assertion: MaybeValue, analysis: Skeleton.Analysis | null }; |
|
|
|
|
|
|
|
|
|
export type ObserverCallback<Fields> = |
|
|
|
|
(this: Fields, facet: Facet<Fields>, bindings: Array<Value>) => void; |
|
|
|
|
(this: Fields, facet: Facet<Fields>, bindings: Array<Value<any>>) => void; |
|
|
|
|
|
|
|
|
|
export type ObserverCallbacks<Fields> = { |
|
|
|
|
add?: ObserverCallback<Fields>; |
|
|
|
@ -150,7 +150,7 @@ export abstract class Dataspace {
@@ -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<any>]> = []; |
|
|
|
|
delta.forEach((count, a) => { |
|
|
|
|
if (count > 0) { |
|
|
|
|
// debug('applyPatch +', a && a.toString());
|
|
|
|
@ -167,7 +167,7 @@ export abstract class Dataspace {
@@ -167,7 +167,7 @@ export abstract class Dataspace {
|
|
|
|
|
// if (!delta.isEmpty()) debug('applyPatch END');
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
deliverMessage(m: Value, _sendingActor: Actor | null) { |
|
|
|
|
deliverMessage(m: Value<any>, _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 {
@@ -175,7 +175,7 @@ export abstract class Dataspace {
|
|
|
|
|
// });
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
adjustIndex(a: Value, count: number) { |
|
|
|
|
adjustIndex(a: Value<any>, count: number) { |
|
|
|
|
return this.index.adjustAssertion(a, count); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -303,18 +303,16 @@ export class Actor {
@@ -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<any>) { this.pendingPatch().adjust(a, +1); } |
|
|
|
|
retract(a: Value<any>) { this.pendingPatch().adjust(a, -1); } |
|
|
|
|
|
|
|
|
|
adhocRetract(a: Value) { |
|
|
|
|
a = fromJS(a); |
|
|
|
|
adhocRetract(a: Value<any>) { |
|
|
|
|
if (this.adhocAssertions.change(a, -1, true) === ChangeDescription.PRESENT_TO_ABSENT) { |
|
|
|
|
this.retract(a); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
adhocAssert(a: Value) { |
|
|
|
|
a = fromJS(a); |
|
|
|
|
adhocAssert(a: Value<any>) { |
|
|
|
|
if (this.adhocAssertions.change(a, +1) === ChangeDescription.ABSENT_TO_PRESENT) { |
|
|
|
|
this.assert(a); |
|
|
|
|
} |
|
|
|
@ -343,17 +341,17 @@ class Patch extends Action {
@@ -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<any>, count: number) { |
|
|
|
|
this.changes.change(a, count); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
class Message extends Action { |
|
|
|
|
readonly body: Value; |
|
|
|
|
readonly body: Value<any>; |
|
|
|
|
|
|
|
|
|
constructor(body: any) { |
|
|
|
|
constructor(body: Value<any>) { |
|
|
|
|
super(); |
|
|
|
|
this.body = fromJS(body); |
|
|
|
|
this.body = body; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
perform(ds: Dataspace, ac: Actor | null): void { |
|
|
|
@ -577,7 +575,7 @@ export class Facet<Fields> {
@@ -577,7 +575,7 @@ export class Facet<Fields> {
|
|
|
|
|
|
|
|
|
|
addObserverEndpoint(specThunk: (facet: Facet<Fields>) => MaybeValue, callbacks: ObserverCallbacks<Fields>): Endpoint<Fields> { |
|
|
|
|
const scriptify = (f?: ObserverCallback<Fields>) => |
|
|
|
|
f && ((facet: Facet<Fields>, vs: Array<Value>) => |
|
|
|
|
f && ((facet: Facet<Fields>, vs: Array<Value<any>>) => |
|
|
|
|
facet.scheduleScript(() => f.call(facet.fields, facet, vs))); |
|
|
|
|
return this._addRawObserverEndpoint(specThunk, { |
|
|
|
|
add: scriptify(callbacks.add), |
|
|
|
@ -667,11 +665,11 @@ export class Facet<Fields> {
@@ -667,11 +665,11 @@ export class Facet<Fields> {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// This alias exists because of the naive expansion done by the parser.
|
|
|
|
|
_send(body: any) { |
|
|
|
|
_send(body: Value<any>) { |
|
|
|
|
this.send(body); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
send(body: any) { |
|
|
|
|
send(body: Value<any>) { |
|
|
|
|
this.ensureNonFacetSetup('`send`'); |
|
|
|
|
this.enqueueScriptAction(new Message(body)); |
|
|
|
|
} |
|
|
|
@ -772,7 +770,6 @@ export class Endpoint<Fields> {
@@ -772,7 +770,6 @@ export class Endpoint<Fields> {
|
|
|
|
|
|
|
|
|
|
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); |
|
|
|
|