Tighter types

This commit is contained in:
Tony Garnock-Jones 2021-02-22 20:08:11 +01:00
parent afca804797
commit e1d5e4cd53
2 changed files with 14 additions and 5 deletions

View File

@ -104,6 +104,10 @@ 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");
@ -181,6 +185,12 @@ 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.ref({
t.assert(ds, Observe(SetBox.constructorInfo.label, t.entity({
[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.ref({
t.assert(ds, Observe(BoxState.constructorInfo.label, t.entity({
[assert](t: Turn, [currentValue]: [number]): void {
// console.log(`Client: got ${currentValue}`);
if (currentValue === 300000) {
@ -140,10 +140,9 @@ Turn.for(null, async (t: Turn) => {
} else {
t.message(ds, SetBox(currentValue + 1));
}
},
[retract]() {}
}
})));
t.assert(ds, Observe(BoxState.constructorInfo.label, t.ref({
t.assert(ds, Observe(BoxState.constructorInfo.label, t.entity({
[assert](_t: Turn, _assertion: Assertion): void { count++; },
[retract](t: Turn, _handle: Handle) {
if (--count === 0) {