UNPKG

@ketch-sdk/ketch-data-layer

Version:
81 lines (80 loc) 2.98 kB
import { Identity, Trait, TraitName } from '@ketch-sdk/ketch-types'; import { ListenerOptions } from '../listener'; /** * Watcher provides a mechanism for watching for traits. */ export default class Watcher { private readonly _w; private readonly _listenerOptions; private _intervalId?; private _fetchers; private _attributes; private _emitter; /** * Create a new Watcher. * * @param w The window interface * @param options The listener options */ constructor(w: Window, options?: ListenerOptions); /** * Add a trait with the given name and definition. * * @param name The name of the trait. * @param attribute The definition of the trait. */ add(name: string, attribute: Identity | Trait | (() => Promise<string[]>)): void; /** * Starts watching for traits. */ start(type?: TraitName, returnEarly?: boolean): Promise<void>; /** * Stops watching for traits. */ stop(): void; /** * Fetches and notifies about traits. */ notify(type?: TraitName, returnEarly?: boolean): Promise<void>; /** * Alias for `emitter.on(eventName, listener)`. */ addListener(eventName: string | symbol, listener: (...args: any[]) => void): this; /** * Adds the `listener` function to the end of the listeners array for the * event named `eventName`. No checks are made to see if the `listener` has * already been added. Multiple calls passing the same combination of `eventName`and `listener` will result in * the `listener` being added, and called, multiple * times. * * @param eventName The name of the event. * @param listener The callback function */ on(eventName: string | symbol, listener: (...args: any[]) => void): this; /** * Adds a **one-time**`listener` function for the event named `eventName`. The * next time `eventName` is triggered, this listener is removed and then invoked. * * @param eventName The name of the event. * @param listener The callback function */ once(eventName: string | symbol, listener: (...args: any[]) => void): this; /** * Removes the specified `listener` from the listener array for the event named`eventName`. */ removeListener(eventName: string | symbol, listener: (...args: any[]) => void): this; /** * Alias for `emitter.removeListener()`. */ off(eventName: string | symbol, listener: (...args: any[]) => void): this; /** * Removes all listeners, or those of the specified `eventName`. * * It is bad practice to remove listeners added elsewhere in the code, * particularly when the `EventEmitter` instance was created by some other * component or module (e.g. sockets or file streams). * * Returns a reference to the `EventEmitter`, so that calls can be chained. */ removeAllListeners(event?: string | symbol): this; }