/** * A reference to an object that automatically disappears after some time. * * The object has three states: * - in the initial, _empty_ state, the reference points to no object and * the reference count is zero * - in the _using_ state, the reference points to some object, and the reference * count is positive; the object will not be released while in this state * - in the _expiring_ state, the reference points to some object, but the * reference count is zero; after a timeout, if still in the expiring state, * the object will be released and the reference will revert to the empty state */ export default class TimedReference { private readonly _maxAge; private _cached; private _refCount; private _age; private _timeout; private _releasefn; /** * Construct a new timed reference * * @param maxAge the maximum time to be in the expiring state * @param releasefn a function called to release the underlying object; defaults * to a no-op */ constructor(maxAge: number, releasefn?: (x: T) => Promise); /** * Acquire the object referenced by this reference. * * If `ref` is true, this function increases the reference count, so the * object will not be automatically released until {@link release} is called. * If `ref` is false, this function does not affect the reference count. If * the reference is currently expiring, the timeout is reset. * * @param ref * @param ifabsent how to compute the reference if absent; if unspecified, the * object might be null if absent * @returns */ acquire(ref: boolean, ifabsent: () => T | Promise): Promise; acquire(ref: boolean): Promise; private _tryClear; /** * Release the reference. * * If the reference count becomes zero, the reference enters the expiring state * and will disappear after a timeout. */ release(): void; /** * Release the reference now. * * This function decreases the reference count if it is positive. If the reference * count is or becomes zero, it releases the object immediately. */ releaseNow(): void; }