lik/ts/lik.interestmap.ts

136 lines
4.2 KiB
TypeScript
Raw Normal View History

2022-05-27 15:53:02 +00:00
/* ===========
The InterestMap is an mechanism that collects interests into something
An interest is expressed by an object, string or number.
A comparison func can be specified to make interests comparable
For every unique interestId an interest is created.
Subssequent interests will be mapped to the same interest
which is then is only fullfilled once.
=========== */
import * as plugins from './lik.plugins.js';
import { ObjectMap } from './lik.objectmap.js';
import { Interest } from './lik.interestmap.interest.js';
2018-11-23 19:33:44 +00:00
export type IInterestComparisonFunc<T> = (objectArg: T) => string;
2020-07-14 01:11:48 +00:00
export interface IInterestMapOptions {
markLostAfterDefault?: number;
}
2018-11-23 19:33:44 +00:00
export class InterestMap<DTInterestId, DTInterestFullfillment> {
2020-07-14 01:11:48 +00:00
public options: IInterestMapOptions;
2018-11-23 19:33:44 +00:00
/**
* stores interests that are currently fullfilled by the cache
*/
private interestObjectMap = new ObjectMap<Interest<DTInterestId, DTInterestFullfillment>>();
2018-11-23 19:33:44 +00:00
/**
* a function to compare interests
*/
private comparisonFunc: IInterestComparisonFunc<DTInterestId>;
2020-11-24 18:53:28 +00:00
constructor(
comparisonFuncArg: IInterestComparisonFunc<DTInterestId>,
optionsArg: IInterestMapOptions = {}
) {
2018-11-23 19:33:44 +00:00
this.comparisonFunc = comparisonFuncArg;
2020-07-14 01:11:48 +00:00
this.options = optionsArg;
2018-11-23 19:33:44 +00:00
}
/**
* adds an interest to the InterestMap
2022-05-27 15:53:02 +00:00
* @param interestId
2018-11-23 19:33:44 +00:00
*/
public async addInterest(
2022-05-27 15:53:02 +00:00
interestId: DTInterestId,
2020-07-14 10:55:48 +00:00
defaultFullfillmentArg?: DTInterestFullfillment
2018-11-23 19:33:44 +00:00
): Promise<Interest<DTInterestId, DTInterestFullfillment>> {
2022-05-27 15:53:02 +00:00
const comparisonString = this.comparisonFunc(interestId);
2018-11-23 19:33:44 +00:00
let returnInterest: Interest<DTInterestId, DTInterestFullfillment>;
const newInterest = new Interest<DTInterestId, DTInterestFullfillment>(
this,
2022-05-27 15:53:02 +00:00
interestId,
2020-07-14 01:11:48 +00:00
this.comparisonFunc,
{
2020-07-14 10:55:48 +00:00
markLostAfterDefault: this.options.markLostAfterDefault,
2020-11-24 18:53:28 +00:00
defaultFullfillment: defaultFullfillmentArg,
2020-07-14 01:11:48 +00:00
}
2018-11-23 19:33:44 +00:00
);
let interestExists = false;
2020-07-12 00:44:50 +00:00
await this.interestObjectMap.forEach((interestArg) => {
2018-11-23 19:33:44 +00:00
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);
}
this.interestObservable.push(returnInterest);
return returnInterest;
}
public interestObservable = new plugins.smartrx.ObservableIntake<Interest<DTInterestId, any>>();
/**
* removes an interest from the interest map
*/
public removeInterest(interestArg: Interest<DTInterestId, DTInterestFullfillment>) {
const interestToRemove = this.interestObjectMap.findOneAndRemoveSync((interestArg2) => {
2018-11-23 19:33:44 +00:00
return interestArg.comparisonString === interestArg2.comparisonString;
});
}
/**
* check interest
*/
public checkInterest(objectArg: DTInterestId): boolean {
const comparisonString = this.comparisonFunc(objectArg);
return this.checkInterestByString(comparisonString);
}
/**
* checks an interest
* @param comparisonStringArg
*/
public checkInterestByString(comparisonStringArg: string): boolean {
const foundInterest = this.interestObjectMap.findSync((interest) => {
2018-11-23 19:33:44 +00:00
return interest.comparisonString === comparisonStringArg;
});
if (foundInterest) {
return true;
} else {
return false;
}
}
/**
* inform lost interest
2018-12-11 00:36:14 +00:00
* @param interestId
2018-11-23 19:33:44 +00:00
*/
2018-12-11 00:36:14 +00:00
public informLostInterest(interestId: DTInterestId) {
2018-11-23 19:33:44 +00:00
const wantedInterest = this.findInterest(interestId);
if (wantedInterest) {
wantedInterest.markLost();
}
}
/**
* finds an interest
2022-05-27 15:53:02 +00:00
* @param interestId
2018-11-23 19:33:44 +00:00
*/
2022-05-27 15:53:02 +00:00
public findInterest(interestId: DTInterestId): Interest<DTInterestId, DTInterestFullfillment> {
const comparableString = this.comparisonFunc(interestId);
const interest = this.interestObjectMap.findSync((interestArg) => {
2020-02-15 21:25:49 +00:00
return interestArg.comparisonString === comparableString;
2018-11-23 19:33:44 +00:00
});
return interest; // if an interest is found, the interest is returned, otherwise interest is null
}
}