import { Condition } from "../condition/index.js"; import { Level } from "level"; import type { ClientStateManager } from "../state.js"; export type Notifier = { [K in keyof Events]: (event: Events[K], state: S) => unknown; }; type Indexed = { item: T; index: number; }; /** * Generic interface that represents a structure that ranks elements. * Most common is a Priority Queue (heap like) that pops elements in order. * An array is also a Ranker, without ordering. */ export interface Ranker { push(item: T): void; pop(): T | undefined; } export type ModulatorEvents = { ready: Indexed; }; export interface Modulator { /** * Initializes the modulator and loads any previously existing state from the state manager. * @param {Condition} condition The condition under which the client runs. * @returns {Promise} True if the modulator was initialized successfully. */ init(condition: Condition): Promise; /** * Starts the handling of a fragment by adding it to the todo list. * @param {ReadonlyArray} fragments The fragments to be handled. */ push(fragments: ReadonlyArray): Promise; /** * Checks if the modulator is ready to trigger the ready event. */ checkReady(): Promise; /** * Called when a fragment has been handled, which removes it from the inflight list. * @param index The index of the fragment that has been handled. */ finished(index: number): Promise; /** * Closes the modulator, which removes it from the factory. */ close(): void; /** * Returns the number of fragments that are still pending. */ pendingCount(): Promise; /** * Returns whether a fragment has been encountered before and is in the immutable list. * @param {string} url The URL of the element to check. * @return {Promise} True if the element is in the immutable list. */ seen(url: string): Promise; /** * Returns all fragments that are mutable. * @returns {Promise>} The mutable list. */ getAllMutable(): Promise>; /** * Returns all data entities that have been extracted but not emitted yet. * @returns {Promise>} The unemitted list. */ getAllUnemitted(): Promise>; /** * Returns all fragments that are currently in flight. * @returns {Promise>} The inflight list. */ getAllInFlight(): Promise>; /** * Returns all fragments that are currently in todo. * @returns {Promise>} The todo list. */ getAllTodo(): Promise>; /** * Records the fact that an element is mutable * @param {string} url The URL of the element to record. * @param {F} fragment The element to record. * @returns {Promise} True if all is good to proceed, false if must not emit new notifications. */ addMutable(url: string, fragment: F): Promise; /** * Records the fact that an element is immutable. * @param {string} url The URL of the element to record. * @returns {Promise} True if all is good to proceed, false if must not emit new notifications. */ addImmutable(url: string): Promise; /** * Records the fact that a data entity has been emitted. * @param {string} url The URL of the emitted data entity. * @returns {Promise} True if all is good to proceed, false if must not emit new notifications. */ addEmitted(url: string): Promise; /** * Records the fact that a data entity has been extracted but not emitted yet. * @param {string} url The URL of the data entity. * @param {M} member The extracted data entity. * @returns {Promise} True if all is good to proceed, false if must not emit new notifications. */ addUnemitted(url: string, member: M): Promise; /** * Returns whether a data entity has been emitted. * @param {string} url The URL of the data entity. * @returns {Promise} True if the data entity has been emitted. */ wasEmitted(url: string): Promise; /** * Removes a data entity from the unemitted list. * @param {string} url The URL of the data entity. * @returns {Promise} True if all is good to proceed, false if must not emit new notifications. */ deleteUnemitted(url: string): Promise; /** * Returns whether the modulator is tracking latest versions * @returns {boolean} True if the modulator is tracking latest versions */ hasLatestVersions(): boolean; /** * Filter out older versions of a member. * @param {string} memberId The ID of the member (isVersionOf). * @param {number} version The version of the member. * @returns {Promise} True if the member is old and should be filtered out. * @throws {Error} If processing was cancelled and must not continue. */ filterLatest(memberId: string, version: number): Promise; } type ModulatorState = { condition: Level; todo: Level; inflight: Level; mutable: Level; emitted: Level; immutable?: Level; unemitted?: Level; latestVersions?: Level; fragmentEncoder?: (item: F) => unknown; fragmentParser?: (item: unknown) => F; memberEncoder?: (item: M) => unknown; memberParser?: (item: unknown) => M; }; /** * Factory that creates Modulators * This is a factory to keep track whether the Modulator should be paused or not. */ export declare class ModulatorFactory { concurrent: number; paused: boolean; saveState: boolean; lastVersionOnly: boolean; clientStateManager: ClientStateManager; children: { [key: string]: Modulator; }; constructor(clientStateManager: ClientStateManager, saveState?: boolean, concurrent?: number, lastVersionOnly?: boolean); create(name: string, ranker: Ranker>, notifier: Notifier, unknown>, fragmentEncoder?: (item: F) => unknown, fragmentParser?: (item: unknown) => F, memberEncoder?: (item: M) => unknown, memberParser?: (item: unknown) => M): Modulator; pause(): void; unpause(): void; close(): void; } export declare class ModulatorInstance implements Modulator { at: number; index: number; private modulatorState; private ranker; private notifier; private factory; private logger; private closed; private versionStateSync; constructor(state: ModulatorState, ranker: Ranker>, notifier: Notifier, unknown>, factory: ModulatorFactory); init(condition: Condition): Promise; push(fragments: ReadonlyArray): Promise; checkReady(): Promise; finished(index: number): Promise; close(): void; pendingCount(): Promise; seen(url: string): Promise; getAllMutable(): Promise>; getAllUnemitted(): Promise>; getAllInFlight(): Promise>; getAllTodo(): Promise>; addMutable(url: string, fragment: F): Promise; addImmutable(url: string): Promise; addEmitted(url: string): Promise; addUnemitted(url: string, member: M): Promise; wasEmitted(url: string): Promise; deleteUnemitted(url: string): Promise; hasLatestVersions(): boolean; /** * This method uses a promise-chain (versionStateSync) to serialize all version checks and updates, * preventing race conditions when multiple fragment extractions occur in parallel. */ filterLatest(memberId: string, version: number): Promise; /** * Clears the todo list. */ private clearAllTodo; /** * Adds a fragment to the todo list. */ private addTodo; /** * Clears the in-flight list. */ private clearAllInFlight; /** * Adds a fragment to the in-flight list. */ private addInFlight; /** * Utility function to execute an operation on the modulator state. */ private withState; } export {};