UNPKG

rjweb-server

Version:

Easy and Robust Way to create a Web Server with Many Easy-to-use Features in NodeJS

90 lines (89 loc) 2.51 kB
import { Content } from "../functions/parseContent"; import { RealAny } from "../types/internal"; type Listener = (value: Content) => RealAny; export type RefListener = { index: Listener; }; /** * A Shared Reference for miscellaneous use cases * @example * ``` * const ref = new Reference(...) * ``` * @since 7.2.0 */ export default class Reference<Value extends Content = any> { private state; private processDataFn; private listeners; /** * Create a new reference to use with the server * @example * ``` * const controller = new Server({ }) * * const ref = new Reference('Hello') * * controller.path('/', (path) => path * .ws('/echo', (ws) => ws * .onConnect((ctr) => { * ctr.printRef(ref) // Will not emit the current state, only when the ref changes * }) * .onMessage((ctr) => { * ref.set(ctr.rawMessage, { * emit: true // When disabled will not emit the current state to subscribers * }) // Will automatically end up echoing it because of the subscription & that to all listeners * }) * ) * ) * ``` * @since 7.2.0 */ constructor( /** The Initial Value of the Reference */ initialValue?: Value); /** * Process data before sending it to listeners * @example * ``` * const ref = new Reference('Hello') * * ref.processData((data) => { * return data + ', yes' * }) * * ref.set('Nice') * * // Will emit 'Nice, yes' to listeners instead of 'Nice' * ``` */ processData(callback: (value: Value) => Content): this; /** * Set the State of a Reference * @example * ``` * const ref = new Reference('Hello') * * ref.set('Moin') * ref.set((prev) => `${prev}!`) * * ref.get() // 'Moin!' * ``` * @since 7.2.0 */ set(value: Value | ((value: Value) => Value), options?: { /** * Whether to emit the State change to listeners * @default true * @since 7.2.0 */ emit?: boolean; }): this; /** * Get the Current State of the Reference * @since 7.2.0 */ get(): Value; /** * Listen for Changes (when actually emitted) * @since 7.2.0 */ protected onChange(listener: Listener): RefListener; /** * Remove a Listener by index * @since 7.2.0 */ protected removeOnChange(listener: RefListener): this; } export {};