UNPKG

node-apparatus

Version:

A mix of common components needed for awesome node experience

46 lines (45 loc) 2.83 kB
import { InjectableConstructor } from "../injectable-constructor/injectable-constructor.js"; 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 distributed window implementation that can be used to accumulate elements and drain them in a window of specific size, by their given identity. */ export declare class DistributedWindowIdentity<T extends Serializable> { private readonly countWindowSize; private readonly identityWindowSize; private readonly distributedSortedSet; private readonly distributedAccumulatorResolver; private readonly injectableConstructor; /** * Creates a new instance of DistributedWindowIdentity. * @param countWindowSize The size of the counting window. Should be more greater than 0. * @param identityWindowSize The size of the identity window. Should be more greater than 0. * @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 injectableConstructor The injectable constructor to create the distributed window instances. * @throws Error if countWindowSize is less than or equal to 0. * @throws Error if identityWindowSize is less than or equal to 0. */ constructor(countWindowSize: number, identityWindowSize: number, distributedSortedSet: IDistributedSortedSet<string>, distributedAccumulatorResolver: (key: string) => IAccumulator<T>, injectableConstructor?: InjectableConstructor); /** * Accumulates a new element and returns the window if the window is full. * @param identity The identity of the payload. * @param element The new element to be accumulated. * @returns The window if the window size is met , otherwise undefined. It can return multiple windows if the previous windows are not drained. * @remarks The sequence of elements can change if the drain implementation returns elements not in the multiple of countWindowSize. */ window(identity: number, element: T): Promise<T[][] | undefined>; /** * Tick the identity and flush the windows if the identity window is full. Use full in scenarios where the identity is time based. * @param identity The identity to be ticked. * @returns The windows if the identity window is full, otherwise empty array. */ tickIdentity(identity: number): Promise<T[][]>; /** * Flushes all elements from the accumulator and returns the windows. * @returns All the pending windows. */ flush(): Promise<T[][]>; private distributedWindowResolver; }