@itwin/presentation-backend
Version:
Backend of iTwin.js Presentation library
123 lines • 4.34 kB
TypeScript
/** @packageDocumentation
* @module Core
*/
/**
* Configuration properties for [[TemporaryStorage]].
* @internal
*/
export interface TemporaryStorageProps<T> {
/** A method that's called for every value before it's removed from storage */
cleanupHandler?: (id: string, value: T, reason: "timeout" | "dispose" | "request") => void;
onDisposedSingle?: (id: string) => void;
onDisposedAll?: () => void;
/**
* An interval at which the storage attempts to clean up its values.
* When `0` or `undefined` is specified, values are not cleaned up
* automatically and cleanup has to be initiated manually by calling
* [[TemporaryStorage.disposeOutdatedValues]].
*/
cleanupInterval?: number;
/**
* Shortest period of time which the value should be kept in storage
* unused before it's cleaned up.
*
* `undefined` means the values may be kept unused in the storage indefinitely.
* `0` means the values are removed from the storage on every cleanup (either manual
* call to [[TemporaryStorage.disposeOutdatedValues]] or scheduled (controlled
* by [[cleanupInterval]])).
*/
unusedValueLifetime?: number;
/**
* The maximum period of time which the value should be kept in storage
* before it's cleaned up. The time is measured from the moment the value is added
* to the storage.
*
* `undefined` means the values may be kept indefinitely. `0` means they're removed
* up on every cleanup (either manual call to [[TemporaryStorage.disposeOutdatedValues]]
* or scheduled (controlled by [[cleanupInterval]])).
*/
maxValueLifetime?: number;
}
/** Value with know last used time */
interface TemporaryValue<T> {
created: Date;
lastUsed: Date;
value: T;
}
/**
* Storage for values that get removed from it after being unused (not-requested
* for a specified amount of time).
*
* @internal
*/
export declare class TemporaryStorage<T> implements Disposable {
private _timer?;
protected _values: Map<string, TemporaryValue<T>>;
readonly props: TemporaryStorageProps<T>;
/**
* Constructor. Creates the storage using supplied params.
*/
constructor(props: TemporaryStorageProps<T>);
/**
* Destructor. Must be called to clean up the stored values
* and other resources
*/
[Symbol.dispose](): void;
/**
* Cleans up values that are currently outdated (based
* on their max and unused value lifetimes specified through [[Props]]).
*/
disposeOutdatedValues: () => void;
private deleteExistingEntry;
/**
* Get a value from the storage.
*
* **Note:** requesting a value with this method updates it's last used time.
*/
getValue(id: string): T | undefined;
notifyValueUsed(id: string): void;
/**
* Adds a value into the storage.
* @throws An error when trying to add a value with ID that's already stored in the storage.
*/
addValue(id: string, value: T): void;
/** Deletes a value with given id. */
deleteValue(id: string): void;
/**
* Get all values currently in this storage.
*
* **Note:** requesting values with this method **doesn't**
* update their last used times.
*/
get values(): T[];
}
/**
* Configuration properties for [[FactoryBasedTemporaryStorage]].
* @internal
*/
export interface FactoryBasedTemporaryStorageProps<T> extends TemporaryStorageProps<T> {
/** A factory method that creates a stored value given it's identifier */
factory: (id: string, onValueUsed: () => void) => T;
}
/**
* Storage for values that get removed from it after being unused (not-requested
* for a specified amount of time).
*
* @internal
*/
export declare class FactoryBasedTemporaryStorage<T> extends TemporaryStorage<T> {
readonly props: FactoryBasedTemporaryStorageProps<T>;
/**
* Constructor. Creates the storage using supplied params.
*/
constructor(props: FactoryBasedTemporaryStorageProps<T>);
/**
* Get a value from the storage. If the value with the specified id
* doesn't exist, it gets created.
*
* **Note:** requesting a value with this method updates it's last used time.
*/
getValue(id: string): T;
}
export {};
//# sourceMappingURL=TemporaryStorage.d.ts.map