UNPKG

node-apparatus

Version:

A mix of common components needed for awesome node experience

37 lines (36 loc) 1.99 kB
import { Serializable } from "../sequential-invocation-queue/sequential-invocation-queue.js"; import { IAccumulator } from "./i-accumulator.js"; import { IDistributedSortedSet } from "./i-distributed-sorted-set.js"; /** * A window-ing implementation for distributed system that can be used to accumulate elements and drain them in a window of specific size or by absolute(wall clock) time window. * It can be used to stead a frequency of elements from a mixed/different update rate. */ export declare class DistributedTimeWindow<T extends Serializable> { private readonly windowsFlushCallback; private readonly dataContainer; private readonly intervalHandle; /** * Creates a new instance of DistributedTimeWindow. * @param countWindowSize The size of the counting window. Should be more greater than 0. * @param timeoutInSeconds The timeout in seconds for the window. * @param distributedSortedSet The distributed sorted set to manage the identity windows. * @param distributedAccumulatorResolver The distributed accumulator resolver to resolve the accumulator for each identity window. * @param windowsFlushCallback The callback to be called when the windows are flushed. * @throws Error if timeoutInSeconds is less than or equal to 5. * @remarks Internally it uses setInterval to check the timed windows for expiry. */ constructor(countWindowSize: number, timeoutInSeconds: number, distributedSortedSet: IDistributedSortedSet<string>, distributedAccumulatorResolver: (key: string) => IAccumulator<T>, windowsFlushCallback: (windows: T[][]) => void); /** * Push a new element to be window-ed. * @param element The new element to be pushed. */ push(element: T): Promise<void>; /** * Flushes all elements which are accumulated. * @returns Multiple windows. */ flush(): Promise<T[][]>; [Symbol.dispose](): void; [Symbol.asyncDispose](): Promise<void>; private tick; }