2018-11-23 19:33:44 +00:00
|
|
|
import * as plugins from './lik.plugins';
|
2020-05-04 00:09:20 +00:00
|
|
|
import { ObjectMap } from './lik.objectmap';
|
2018-11-23 19:33:44 +00:00
|
|
|
|
|
|
|
import { Observable } from 'rxjs';
|
|
|
|
|
|
|
|
import { Interest } from './lik.interestmap.interest';
|
|
|
|
|
|
|
|
export type IInterestComparisonFunc<T> = (objectArg: T) => string;
|
|
|
|
|
|
|
|
export class InterestMap<DTInterestId, DTInterestFullfillment> {
|
|
|
|
/**
|
|
|
|
* stores interests that are currently fullfilled by the cache
|
|
|
|
*/
|
2020-05-04 00:09:20 +00:00
|
|
|
private interestObjectMap = new ObjectMap<Interest<DTInterestId, DTInterestFullfillment>>();
|
2018-11-23 19:33:44 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* a function to compare interests
|
|
|
|
*/
|
|
|
|
private comparisonFunc: IInterestComparisonFunc<DTInterestId>;
|
|
|
|
|
|
|
|
constructor(comparisonFuncArg: IInterestComparisonFunc<DTInterestId>) {
|
|
|
|
this.comparisonFunc = comparisonFuncArg;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* adds an interest to the InterestMap
|
|
|
|
* @param objectArg
|
|
|
|
*/
|
|
|
|
public async addInterest(
|
|
|
|
objectArg: DTInterestId
|
|
|
|
): Promise<Interest<DTInterestId, DTInterestFullfillment>> {
|
|
|
|
const comparisonString = this.comparisonFunc(objectArg);
|
|
|
|
let returnInterest: Interest<DTInterestId, DTInterestFullfillment>;
|
|
|
|
const newInterest = new Interest<DTInterestId, DTInterestFullfillment>(
|
|
|
|
this,
|
|
|
|
objectArg,
|
|
|
|
this.comparisonFunc
|
|
|
|
);
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
this.interestObservable.push(returnInterest);
|
|
|
|
return returnInterest;
|
|
|
|
}
|
|
|
|
|
|
|
|
// tslint:disable-next-line:member-ordering
|
|
|
|
public interestObservable = new plugins.smartrx.ObservableIntake<Interest<DTInterestId, any>>();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* removes an interest from the interest map
|
|
|
|
* @param objectArg removes an interest from the InterestMap
|
|
|
|
*/
|
|
|
|
public removeInterest(interestArg: Interest<DTInterestId, DTInterestFullfillment>) {
|
|
|
|
const interestToRemove = this.interestObjectMap.findOneAndRemove(interestArg2 => {
|
|
|
|
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.find(interest => {
|
|
|
|
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
|
|
|
|
* @param objectArg
|
|
|
|
*/
|
|
|
|
public findInterest(objectArg: DTInterestId): Interest<DTInterestId, DTInterestFullfillment> {
|
|
|
|
const comparableString = this.comparisonFunc(objectArg);
|
2020-02-15 21:25:49 +00:00
|
|
|
const interest = this.interestObjectMap.find(interestArg => {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|