Simplify by making Entity methods optional

This commit is contained in:
Tony Garnock-Jones 2021-02-22 20:14:35 +01:00
parent e1d5e4cd53
commit fa98a399bc
2 changed files with 9 additions and 19 deletions

View File

@ -11,9 +11,9 @@ export const retract = Symbol('retract');
export const message = Symbol('message');
export interface Entity {
[assert](turn: Turn, assertion: Assertion, handle: Handle): void;
[retract](turn: Turn, handle: Handle): void;
[message](turn: Turn, message: Assertion): void;
[assert]?(turn: Turn, assertion: Assertion, handle: Handle): void;
[retract]?(turn: Turn, handle: Handle): void;
[message]?(turn: Turn, message: Assertion): void;
}
export class Ref<T> {
@ -104,10 +104,6 @@ export class Turn {
return this._ensureActor(what).ref(t);
}
entity(t: Partial<Entity>): Ref<Entity> {
return this.ref({ ... NULL_ENTITY, ... t }, "entity");
}
spawn(bootProc: (t: Turn) => void, initialAssertions?: IdentitySet<Handle>): void {
if ((initialAssertions !== void 0) && (initialAssertions.size > 0)) {
this._ensureActor("spawn with initialAssertions");
@ -131,7 +127,7 @@ export class Turn {
const h = nextHandle++;
this.enqueue(location.actor, t => {
this.actor!.outbound.set(h, [location, assertion]);
location.target[assert](t, assertion, h);
location.target[assert]?.(t, assertion, h);
});
return h;
}
@ -149,7 +145,7 @@ export class Turn {
_retract(location: Ref<Entity>, handle: Handle): void {
this.enqueue(location.actor, t => {
this.actor!.outbound.delete(handle);
location.target[retract](t, handle);
location.target[retract]?.(t, handle);
});
}
@ -160,7 +156,7 @@ export class Turn {
}
message(location: Ref<Entity>, assertion: Assertion): void {
this.enqueue(location.actor, t => location.target[message](t, assertion));
this.enqueue(location.actor, t => location.target[message]?.(t, assertion));
}
enqueue(actor: Actor, a: LocalAction): void {
@ -185,12 +181,6 @@ export class Turn {
}
}
export const NULL_ENTITY: Entity = {
[assert](_t: Turn, _assertion: Assertion, _handle: Handle): void {},
[retract](_t: Turn, _handle: Handle): void {},
[message](_t: Turn, _assertion: Assertion): void {},
};
function extractFromMap<K, V>(map: Map<K, V>, keys?: IdentitySet<K>): Map<K, V> {
const result: Map<K, V> = new Map();
if (keys !== void 0) {

View File

@ -111,7 +111,7 @@ Turn.for(null, async (t: Turn) => {
valueHandle = t.replace(ds, valueHandle, BoxState(value));
}
setValue(t, 0);
t.assert(ds, Observe(SetBox.constructorInfo.label, t.entity({
t.assert(ds, Observe(SetBox.constructorInfo.label, t.ref({
[message](t: Turn, [newValue]: [number]): void {
if (newValue % 25000 === 0) {
const endTime = Date.now();
@ -131,7 +131,7 @@ Turn.for(null, async (t: Turn) => {
t.spawn(t => {
console.log('Spawning Client');
let count = 0;
t.assert(ds, Observe(BoxState.constructorInfo.label, t.entity({
t.assert(ds, Observe(BoxState.constructorInfo.label, t.ref({
[assert](t: Turn, [currentValue]: [number]): void {
// console.log(`Client: got ${currentValue}`);
if (currentValue === 300000) {
@ -142,7 +142,7 @@ Turn.for(null, async (t: Turn) => {
}
}
})));
t.assert(ds, Observe(BoxState.constructorInfo.label, t.entity({
t.assert(ds, Observe(BoxState.constructorInfo.label, t.ref({
[assert](_t: Turn, _assertion: Assertion): void { count++; },
[retract](t: Turn, _handle: Handle) {
if (--count === 0) {