lik/ts/classes.interestmap.interest.ts

94 lines
2.6 KiB
TypeScript
Raw Normal View History

2024-02-25 12:01:06 +00:00
import * as plugins from './classes.plugins.js';
2018-11-23 19:33:44 +00:00
2024-02-25 12:01:06 +00:00
import { InterestMap, type IInterestComparisonFunc } from './classes.interestmap.js';
2018-11-23 19:33:44 +00:00
2020-07-14 10:55:48 +00:00
export interface IInterestOptions<DTInterestFullfillment> {
2020-07-14 01:11:48 +00:00
markLostAfterDefault: number;
2020-07-14 10:55:48 +00:00
defaultFullfillment?: DTInterestFullfillment;
2020-07-14 01:11:48 +00:00
}
2018-11-23 19:33:44 +00:00
export class Interest<DTInterestId, DTInterestFullfillment> {
2020-07-14 10:55:48 +00:00
public options: IInterestOptions<DTInterestFullfillment>;
2020-07-14 01:11:48 +00:00
2018-11-23 19:33:44 +00:00
private interestMapRef: InterestMap<DTInterestId, DTInterestFullfillment>;
public originalInterest: DTInterestId;
public comparisonFunc: IInterestComparisonFunc<DTInterestId>;
public destructionTimer = new plugins.smarttime.Timer(10000);
public isFullfilled = false;
/**
* a generic store to store objects in that are needed for fullfillment;
*/
public fullfillmentStore: any[] = [];
/**
* quick access to a string that makes the interest comparable for checking for similar interests
*/
public get comparisonString() {
return this.comparisonFunc(this.originalInterest);
}
2023-01-18 16:45:19 +00:00
private interestDeferred: plugins.smartpromise.Deferred<DTInterestFullfillment> =
new plugins.smartpromise.Deferred();
2018-11-23 19:33:44 +00:00
public interestFullfilled = this.interestDeferred.promise;
/**
* fullfill the interest
*/
public fullfillInterest(objectArg: DTInterestFullfillment) {
this.isFullfilled = true;
this.fullfillmentStore = [];
this.interestDeferred.resolve(objectArg);
}
/**
*
*/
constructor(
interestMapArg: InterestMap<DTInterestId, DTInterestFullfillment>,
interestArg: DTInterestId,
2020-07-14 01:11:48 +00:00
comparisonFuncArg: IInterestComparisonFunc<DTInterestId>,
2020-07-14 10:55:48 +00:00
optionsArg?: IInterestOptions<DTInterestFullfillment>
2018-11-23 19:33:44 +00:00
) {
2020-07-14 01:11:48 +00:00
this.interestMapRef = interestMapArg;
2018-11-23 19:33:44 +00:00
this.originalInterest = interestArg;
this.comparisonFunc = comparisonFuncArg;
2020-07-14 01:11:48 +00:00
this.options = optionsArg;
2018-11-23 19:33:44 +00:00
this.destructionTimer.completed.then(() => {
this.destroy();
});
2020-07-14 01:11:48 +00:00
if (this.options?.markLostAfterDefault) {
plugins.smartdelay.delayFor(this.options.markLostAfterDefault).then(this.markLost);
}
2018-11-23 19:33:44 +00:00
}
// ===============================
// LIFECYCLE MANAGEMENT
// ===============================
/**
* self destructs the interest
*/
public destroy() {
this.interestMapRef.removeInterest(this);
2020-07-14 10:55:48 +00:00
if (!this.isFullfilled && this.options.defaultFullfillment) {
this.fullfillInterest(this.options.defaultFullfillment);
}
2018-11-23 19:33:44 +00:00
}
/**
* notifies the interest that the interest in it has been lost
*/
public markLost() {
this.destructionTimer.start();
}
/**
* notifies the interest that the interest in it has been restored
*/
public renew() {
this.destructionTimer.reset();
}
2018-12-11 00:36:14 +00:00
}