samchon/tstl

Implement `TimedSingleton` and `VaradicTimedSingleton` classes

samchon opened this issue · 0 comments

Implement new type of (variadic) singleton classes who re-generates internal value repeatedly following an interval value.

It would be very useful for client side programs who need to refresh activation token for a promised time repeatedly.

/**
 * Timed singleton generator.
 * 
 * The `TimedSingleton` is a type of {@link Singleton} class who re-constructs the singleton 
 * value repeatedly whenever specific time has been elapsed after the last lazy construction.
 * 
 * @template T Type of the value to be lazy-constructed
 * @author Jeongho Nam - https://github.com/samchon
 */
export class TimedSingleton<T, Args extends any[] = []>
{
    /**
     * Initializer Constructor.
     * 
     * @param interval Specific interval time, to determine whether re-generation of the singleton value is required or not, as milliseconds
     * @param closure Lazy constructor function returning the target value
     */
    public constructor
        (
            interval: number;
            closure: (...args: Args) => T
        );

    public get(...args: Args): T;
}

/**
 * Variadic timed singleton generator.
 * 
 * The `VariadicTimedSingleton` is a type of {@link VariadicSingleton} class who re-constructs
 * the singleton value repeatedly whenever specific time has been elapsed after the last lazy
 * construction.
 * 
 * @template T Type of the value to be lazy-constructed
 * @template Args Type of parameters of the lazy constructor function
 * @author Jeongho Nam - https://github.com/samchon
 */
export class VariadicTimedSingleton<T, Args extends any[]>
{
    /**
     * Initializer Constructor.
     * 
     * @param interval Specific interval time, to determine whether re-generation of the singleton value is required or not, as milliseconds
     * @param closure Lazy constructor function returning the target value
     * @param hasher Hash function for the *lazy constructor* function arguments
     * @param pred Predicator function for the *lazy constructor* function arguments
     */
    public constructor
        (
            interval: number,
            closure: (...args: Args) => T,
            hasher: (args: Args) => number = args => hash(...args),
            pred: (x: Args, y: Args) => boolean = equal
        );

    public get(...args: Args): T;
}