Files
lik/ts/classes.interestmap.interest.ts

124 lines
3.5 KiB
TypeScript
Raw Normal View History

2024-02-25 13:01:06 +01:00
import * as plugins from './classes.plugins.js';
2018-11-23 20:33:44 +01:00
2024-02-25 13:01:06 +01:00
import { InterestMap, type IInterestComparisonFunc } from './classes.interestmap.js';
2018-11-23 20:33:44 +01: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 20:33:44 +01: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 20:33:44 +01:00
private interestMapRef: InterestMap<DTInterestId, DTInterestFullfillment>;
public originalInterest: DTInterestId;
public comparisonFunc: IInterestComparisonFunc<DTInterestId>;
public destructionTimer = new plugins.smarttime.Timer(10000);
public isFullfilled = false;
private isDestroyed = false;
2018-11-23 20:33:44 +01:00
/**
* a generic store to store objects in that are needed for fullfillment;
*/
public fullfillmentStore: any[] = [];
/**
* a cancellable timeout for the markLostAfterDefault feature
*/
private markLostTimeout: InstanceType<typeof plugins.smartdelay.Timeout> | null = null;
2018-11-23 20:33:44 +01:00
/**
* 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 17:45:19 +01:00
private interestDeferred: plugins.smartpromise.Deferred<DTInterestFullfillment> =
new plugins.smartpromise.Deferred();
2018-11-23 20:33:44 +01:00
public interestFullfilled = this.interestDeferred.promise;
/**
* fullfill the interest
*/
public fullfillInterest(objectArg: DTInterestFullfillment) {
this.isFullfilled = true;
this.fullfillmentStore = [];
this.interestDeferred.resolve(objectArg);
this.destroy();
2018-11-23 20:33:44 +01:00
}
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 20:33:44 +01:00
) {
2020-07-14 01:11:48 +00:00
this.interestMapRef = interestMapArg;
2018-11-23 20:33:44 +01:00
this.originalInterest = interestArg;
this.comparisonFunc = comparisonFuncArg;
2020-07-14 01:11:48 +00:00
this.options = optionsArg;
2018-11-23 20:33:44 +01:00
this.destructionTimer.completed.then(() => {
if (!this.isDestroyed) {
this.destroy();
}
2018-11-23 20:33:44 +01:00
});
2020-07-14 01:11:48 +00:00
if (this.options?.markLostAfterDefault) {
this.markLostTimeout = new plugins.smartdelay.Timeout(this.options.markLostAfterDefault);
this.markLostTimeout.promise.then(() => {
if (!this.isDestroyed) {
this.markLost();
}
});
2020-07-14 01:11:48 +00:00
}
2018-11-23 20:33:44 +01:00
}
// ===============================
// LIFECYCLE MANAGEMENT
// ===============================
/**
* self destructs the interest
*/
public destroy() {
if (this.isDestroyed) {
return;
}
this.isDestroyed = true;
// Cancel timers to release references
this.destructionTimer.reset();
if (this.markLostTimeout) {
this.markLostTimeout.cancel();
this.markLostTimeout = null;
}
// Clear the fulfillment store
this.fullfillmentStore = [];
// Remove from the InterestMap
2018-11-23 20:33:44 +01:00
this.interestMapRef.removeInterest(this);
// Fulfill with default if not yet fulfilled (inlined to avoid mutual recursion)
if (!this.isFullfilled && this.options?.defaultFullfillment) {
this.isFullfilled = true;
this.interestDeferred.resolve(this.options.defaultFullfillment);
2020-07-14 10:55:48 +00:00
}
2018-11-23 20:33:44 +01: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 01:36:14 +01:00
}