store self-generated deltas

This commit is contained in:
Ladd Hoffman 2024-12-22 14:17:44 -06:00
parent 8ce984151b
commit 7efb9024ec
2 changed files with 10 additions and 26 deletions

View File

@ -1,4 +1,5 @@
// The goal here is to house a collection of objects that all follow a common schema. // A basic collection of entities
// This may be extended to house a collection of objects that all follow a common schema.
// It should enable operations like removing a property removes the value from the entities in the collection // It should enable operations like removing a property removes the value from the entities in the collection
// It could then be further extended with e.g. table semantics like filter, sort, join // It could then be further extended with e.g. table semantics like filter, sort, join
@ -7,6 +8,7 @@ import { deltasAccepted, publishDelta, subscribeDeltas } from "./deltas";
import { Entity, EntityProperties, EntityPropertiesDeltaBuilder } from "./object-layer"; import { Entity, EntityProperties, EntityPropertiesDeltaBuilder } from "./object-layer";
import { Delta } from "./types"; import { Delta } from "./types";
import { randomUUID } from "node:crypto"; import { randomUUID } from "node:crypto";
import {myRequestAddr} from "./peers";
// type Property = { // type Property = {
// name: string, // name: string,
@ -21,25 +23,6 @@ import { randomUUID } from "node:crypto";
// } // }
// } // }
// class Entity {
// type: EntityType;
// properties?: object;
// constructor(type: EntityType) {
// this.type = type;
// }
// }
// class Collection {
// update(entityId, properties)
// ...
// }
// export class Collections {
// collections = new Map<string, Collection>();
// }
export class Collection { export class Collection {
entities = new Map<string, Entity>(); entities = new Map<string, Entity>();
eventStream = new EventEmitter(); eventStream = new EventEmitter();
@ -48,9 +31,6 @@ export class Collection {
// TODO: Make sure this is the kind of delta we're looking for // TODO: Make sure this is the kind of delta we're looking for
this.applyDelta(delta); this.applyDelta(delta);
}); });
this.eventStream.on('create', (entity: Entity) => {
console.log(`new entity!`, entity);
});
} }
// Applies the javascript rules for updating object values, // Applies the javascript rules for updating object values,
@ -144,20 +124,24 @@ export class Collection {
cb(entity); cb(entity);
}); });
} }
onUpdate(cb: (entity: Entity) => void) { onUpdate(cb: (entity: Entity) => void) {
this.eventStream.on('update', (entity: Entity) => { this.eventStream.on('update', (entity: Entity) => {
cb(entity); cb(entity);
}); });
} }
put(entityId: string | undefined, properties: object): Entity { put(entityId: string | undefined, properties: object): Entity {
const deltas: Delta[] = []; const deltas: Delta[] = [];
const entity = this.updateEntity(entityId, properties, true, deltas); const entity = this.updateEntity(entityId, properties, true, deltas);
deltas.forEach(async (delta: Delta) => { deltas.forEach(async (delta: Delta) => {
delta.receivedFrom = myRequestAddr;
deltasAccepted.push(delta); deltasAccepted.push(delta);
await publishDelta(delta); await publishDelta(delta);
}); });
return entity; return entity;
} }
del(entityId: string) { del(entityId: string) {
const deltas: Delta[] = []; const deltas: Delta[] = [];
this.updateEntity(entityId, undefined, true, deltas); this.updateEntity(entityId, undefined, true, deltas);
@ -166,9 +150,11 @@ export class Collection {
await publishDelta(delta); await publishDelta(delta);
}); });
} }
get(id: string): Entity | undefined { get(id: string): Entity | undefined {
return this.entities.get(id); return this.entities.get(id);
} }
getIds(): string[] { getIds(): string[] {
return Array.from(this.entities.keys()); return Array.from(this.entities.keys());
} }

View File

@ -50,12 +50,10 @@ export class PeerAddress {
return new PeerAddress(addr, parseInt(port)); return new PeerAddress(addr, parseInt(port));
} }
toAddrString() { toAddrString() {
console.log('toAddrStr...', {addr: this.addr, port: this.port});
return `${this.addr}:${this.port}`; return `${this.addr}:${this.port}`;
} }
toJSON() { toJSON() {
console.log('toAddrStr...', {addr: this.addr, port: this.port}); return this.toAddrString();
return `${this.addr}:${this.port}`;
} }
}; };