feat(collections): add new collection APIs, iterator support, and tree serialization utilities

This commit is contained in:
2026-03-22 08:44:49 +00:00
parent 20182a00f8
commit f4db131ede
23 changed files with 2251 additions and 2657 deletions

View File

@@ -26,6 +26,11 @@ export class InterestMap<DTInterestId, DTInterestFullfillment> {
*/
private interestObjectMap = new ObjectMap<Interest<DTInterestId, DTInterestFullfillment>>();
/**
* O(1) lookup of interests by their comparison string
*/
private interestsByComparisonString = new Map<string, Interest<DTInterestId, DTInterestFullfillment>>();
/**
* a function to compare interests
*/
@@ -49,29 +54,23 @@ export class InterestMap<DTInterestId, DTInterestFullfillment> {
): Promise<Interest<DTInterestId, DTInterestFullfillment>> {
const comparisonString = this.comparisonFunc(interestId);
let returnInterest: Interest<DTInterestId, DTInterestFullfillment>;
const newInterest = new Interest<DTInterestId, DTInterestFullfillment>(
this,
interestId,
this.comparisonFunc,
{
markLostAfterDefault: this.options.markLostAfterDefault,
defaultFullfillment: defaultFullfillmentArg,
}
);
let interestExists = false;
await this.interestObjectMap.forEach((interestArg) => {
if (!interestExists && interestArg.comparisonString === newInterest.comparisonString) {
console.log('info', `interest already exists for ${newInterest.comparisonString}`);
interestExists = true;
returnInterest = interestArg;
returnInterest.renew();
}
});
if (!returnInterest) {
returnInterest = newInterest;
this.interestObjectMap.add(returnInterest);
const existingInterest = this.interestsByComparisonString.get(comparisonString);
if (existingInterest) {
returnInterest = existingInterest;
returnInterest.renew();
} else {
newInterest.destroy(); // clean up abandoned Interest's timers
returnInterest = new Interest<DTInterestId, DTInterestFullfillment>(
this,
interestId,
this.comparisonFunc,
{
markLostAfterDefault: this.options.markLostAfterDefault,
defaultFullfillment: defaultFullfillmentArg,
}
);
this.interestObjectMap.add(returnInterest);
this.interestsByComparisonString.set(comparisonString, returnInterest);
}
this.interestObservable.push(returnInterest);
return returnInterest;
@@ -83,9 +82,10 @@ export class InterestMap<DTInterestId, DTInterestFullfillment> {
* removes an interest from the interest map
*/
public removeInterest(interestArg: Interest<DTInterestId, DTInterestFullfillment>) {
const interestToRemove = this.interestObjectMap.findOneAndRemoveSync((interestArg2) => {
this.interestObjectMap.findOneAndRemoveSync((interestArg2) => {
return interestArg.comparisonString === interestArg2.comparisonString;
});
this.interestsByComparisonString.delete(interestArg.comparisonString);
}
/**
@@ -101,14 +101,7 @@ export class InterestMap<DTInterestId, DTInterestFullfillment> {
* @param comparisonStringArg
*/
public checkInterestByString(comparisonStringArg: string): boolean {
const foundInterest = this.interestObjectMap.findSync((interest) => {
return interest.comparisonString === comparisonStringArg;
});
if (foundInterest) {
return true;
} else {
return false;
}
return this.interestsByComparisonString.has(comparisonStringArg);
}
/**
@@ -128,10 +121,7 @@ export class InterestMap<DTInterestId, DTInterestFullfillment> {
*/
public findInterest(interestId: DTInterestId): Interest<DTInterestId, DTInterestFullfillment> {
const comparableString = this.comparisonFunc(interestId);
const interest = this.interestObjectMap.findSync((interestArg) => {
return interestArg.comparisonString === comparableString;
});
return interest; // if an interest is found, the interest is returned, otherwise interest is null
return this.interestsByComparisonString.get(comparableString) ?? null;
}
/**
@@ -143,6 +133,7 @@ export class InterestMap<DTInterestId, DTInterestFullfillment> {
interest.destroy();
}
this.interestObjectMap.wipe();
this.interestsByComparisonString.clear();
this.interestObservable.signalComplete();
}
}