From 870c1a62b6624db6b565d341e6ae3a913ca04e3b Mon Sep 17 00:00:00 2001 From: Ladd Date: Sat, 28 Dec 2024 18:20:32 -0600 Subject: [PATCH] moved lossless from collection to node, as we can reuse it for different kinds of data structures --- src/collection.ts | 17 +++++++++-------- src/node.ts | 2 ++ 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/collection.ts b/src/collection.ts index f23e618..0c364be 100644 --- a/src/collection.ts +++ b/src/collection.ts @@ -8,7 +8,7 @@ import {randomUUID} from "node:crypto"; import EventEmitter from "node:events"; import {Delta, DeltaID} from "./delta"; import {Entity, EntityProperties} from "./entity"; -import {Lossless, LosslessViewMany} from "./lossless"; +import {LosslessViewMany} from "./lossless"; import {firstValueFromLosslessViewOne, Lossy, LossyViewMany, LossyViewOne} from "./lossy"; import {RhizomeNode} from "./node"; import {DomainEntityID} from "./types"; @@ -19,16 +19,15 @@ export class Collection { name: string; entities = new Map(); eventStream = new EventEmitter(); - lossless = new Lossless(); // TODO: Really just need one global Lossless instance - lossy: Lossy; constructor(name: string) { this.name = name; - this.lossy = new Lossy(this.lossless); } ingestDelta(delta: Delta) { - const updated = this.lossless.ingestDelta(delta); + if (!this.rhizomeNode) return; + + const updated = this.rhizomeNode.lossless.ingestDelta(delta); this.eventStream.emit('ingested', delta); this.eventStream.emit('updated', updated); @@ -194,12 +193,14 @@ export class Collection { // Now with lossy view approach, instead of just returning what we already have, // let's compute our view now. // return this.entities.get(id); - const res = this.lossy.resolve((view) => this.defaultResolver(view), [id]); + if (!this.rhizomeNode) return undefined; + const lossy = new Lossy(this.rhizomeNode.lossless); + const res = lossy.resolve((view) => this.defaultResolver(view), [id]); return res[id]; } getIds(): string[] { - // return Array.from(this.entities.keys()); - return Array.from(this.lossless.domainEntities.keys()); + if (!this.rhizomeNode) return []; + return Array.from(this.rhizomeNode.lossless.domainEntities.keys()); } } diff --git a/src/node.ts b/src/node.ts index e715ba7..d0633ee 100644 --- a/src/node.ts +++ b/src/node.ts @@ -2,6 +2,7 @@ import Debug from 'debug'; import {CREATOR, HTTP_API_ADDR, HTTP_API_ENABLE, HTTP_API_PORT, PEER_ID, PUBLISH_BIND_ADDR, PUBLISH_BIND_HOST, PUBLISH_BIND_PORT, REQUEST_BIND_ADDR, REQUEST_BIND_HOST, REQUEST_BIND_PORT, SEED_PEERS} from './config'; import {DeltaStream} from './deltas'; import {HttpServer} from './http'; +import {Lossless} from './lossless'; import {Peers} from './peers'; import {PubSub} from './pub-sub'; import {RequestReply} from './request-reply'; @@ -30,6 +31,7 @@ export class RhizomeNode { requestReply: RequestReply; httpServer: HttpServer; deltaStream: DeltaStream; + lossless = new Lossless(); peers: Peers; myRequestAddr: PeerAddress; myPublishAddr: PeerAddress;