From 7efb9024ec00e8f3c88db18025e640f72ba7379a Mon Sep 17 00:00:00 2001 From: Ladd Date: Sun, 22 Dec 2024 14:17:44 -0600 Subject: [PATCH] store self-generated deltas --- src/collection-layer.ts | 32 +++++++++----------------------- src/types.ts | 4 +--- 2 files changed, 10 insertions(+), 26 deletions(-) diff --git a/src/collection-layer.ts b/src/collection-layer.ts index a9bb8ea..bb2c0c6 100644 --- a/src/collection-layer.ts +++ b/src/collection-layer.ts @@ -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 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 { Delta } from "./types"; import { randomUUID } from "node:crypto"; +import {myRequestAddr} from "./peers"; // type Property = { // 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(); -// } - - export class Collection { entities = new Map(); eventStream = new EventEmitter(); @@ -48,9 +31,6 @@ export class Collection { // TODO: Make sure this is the kind of delta we're looking for this.applyDelta(delta); }); - this.eventStream.on('create', (entity: Entity) => { - console.log(`new entity!`, entity); - }); } // Applies the javascript rules for updating object values, @@ -144,20 +124,24 @@ export class Collection { cb(entity); }); } + onUpdate(cb: (entity: Entity) => void) { this.eventStream.on('update', (entity: Entity) => { cb(entity); }); } + put(entityId: string | undefined, properties: object): Entity { const deltas: Delta[] = []; const entity = this.updateEntity(entityId, properties, true, deltas); deltas.forEach(async (delta: Delta) => { + delta.receivedFrom = myRequestAddr; deltasAccepted.push(delta); await publishDelta(delta); }); return entity; } + del(entityId: string) { const deltas: Delta[] = []; this.updateEntity(entityId, undefined, true, deltas); @@ -166,9 +150,11 @@ export class Collection { await publishDelta(delta); }); } + get(id: string): Entity | undefined { return this.entities.get(id); } + getIds(): string[] { return Array.from(this.entities.keys()); } diff --git a/src/types.ts b/src/types.ts index ba6649f..f2e6792 100644 --- a/src/types.ts +++ b/src/types.ts @@ -50,12 +50,10 @@ export class PeerAddress { return new PeerAddress(addr, parseInt(port)); } toAddrString() { - console.log('toAddrStr...', {addr: this.addr, port: this.port}); return `${this.addr}:${this.port}`; } toJSON() { - console.log('toAddrStr...', {addr: this.addr, port: this.port}); - return `${this.addr}:${this.port}`; + return this.toAddrString(); } };