moved lossless from collection to node, as we can reuse it for different kinds of data structures

This commit is contained in:
Ladd Hoffman 2024-12-28 18:20:32 -06:00
parent ad3c00638c
commit 870c1a62b6
2 changed files with 11 additions and 8 deletions

View File

@ -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<string, Entity>();
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());
}
}

View File

@ -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;