fix(classes): cleanup resources, add cancellable timeouts, and fix bugs in several core utility classes
This commit is contained in:
@@ -15,12 +15,18 @@ export class Interest<DTInterestId, DTInterestFullfillment> {
|
||||
public comparisonFunc: IInterestComparisonFunc<DTInterestId>;
|
||||
public destructionTimer = new plugins.smarttime.Timer(10000);
|
||||
public isFullfilled = false;
|
||||
private isDestroyed = false;
|
||||
|
||||
/**
|
||||
* 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;
|
||||
|
||||
/**
|
||||
* quick access to a string that makes the interest comparable for checking for similar interests
|
||||
*/
|
||||
@@ -39,12 +45,9 @@ export class Interest<DTInterestId, DTInterestFullfillment> {
|
||||
this.isFullfilled = true;
|
||||
this.fullfillmentStore = [];
|
||||
this.interestDeferred.resolve(objectArg);
|
||||
this.destroy(); // Remove from InterestMap immediately after fulfillment
|
||||
this.destroy();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
constructor(
|
||||
interestMapArg: InterestMap<DTInterestId, DTInterestFullfillment>,
|
||||
interestArg: DTInterestId,
|
||||
@@ -57,10 +60,17 @@ export class Interest<DTInterestId, DTInterestFullfillment> {
|
||||
this.options = optionsArg;
|
||||
|
||||
this.destructionTimer.completed.then(() => {
|
||||
this.destroy();
|
||||
if (!this.isDestroyed) {
|
||||
this.destroy();
|
||||
}
|
||||
});
|
||||
if (this.options?.markLostAfterDefault) {
|
||||
plugins.smartdelay.delayFor(this.options.markLostAfterDefault).then(this.markLost);
|
||||
this.markLostTimeout = new plugins.smartdelay.Timeout(this.options.markLostAfterDefault);
|
||||
this.markLostTimeout.promise.then(() => {
|
||||
if (!this.isDestroyed) {
|
||||
this.markLost();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -72,9 +82,28 @@ export class Interest<DTInterestId, DTInterestFullfillment> {
|
||||
* 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
|
||||
this.interestMapRef.removeInterest(this);
|
||||
if (!this.isFullfilled && this.options.defaultFullfillment) {
|
||||
this.fullfillInterest(this.options.defaultFullfillment);
|
||||
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user