UNPKG

@vaadin/hilla-frontend

Version:

Hilla core frontend utils

79 lines (78 loc) 2.34 kB
import type { Subscription } from "./Connect.js"; export declare enum State { ACTIVE = "active", INACTIVE = "inactive", RECONNECTING = "reconnecting", } type ActiveEvent = CustomEvent<{ active: boolean }>; interface EventMap { "state-changed": ActiveEvent; } type ListenerType<T extends keyof EventMap> = ((this: FluxConnection, ev: EventMap[T]) => any) | { handleEvent(ev: EventMap[T]): void } | null; /** * Possible options for dealing with lost subscriptions after a websocket is reopened. */ export declare enum ActionOnLostSubscription { /** * The subscription should be resubscribed using the same server method and parameters. */ RESUBSCRIBE = "resubscribe", /** * The subscription should be removed. */ REMOVE = "remove", } /** * Possible states of a flux subscription. */ export declare enum FluxSubscriptionState { /** * The subscription is not connected and is trying to connect. */ CONNECTING = "connecting", /** * The subscription is connected and receiving updates. */ CONNECTED = "connected", /** * The subscription is closed and is not trying to reconnect. */ CLOSED = "closed", } /** * Event wrapper for flux subscription connection state change callback */ export type FluxSubscriptionStateChangeEvent = CustomEvent<{ state: FluxSubscriptionState }>; /** * A representation of the underlying persistent network connection used for subscribing to Flux type endpoint methods. */ export declare class FluxConnection extends EventTarget { #private; state: State; wasClosed: boolean; constructor(connectPrefix: string, atmosphereOptions?: Partial<Atmosphere.Request>); /** * Promise that resolves when the instance is initialized. */ get ready(): Promise<void>; /** * Subscribes to the flux returned by the given endpoint name + method name using the given parameters. * * @param endpointName - the endpoint to connect to * @param methodName - the method in the endpoint to connect to * @param parameters - the parameters to use * @returns a subscription */ subscribe(endpointName: string, methodName: string, parameters?: unknown[]): Subscription<any>; } export interface FluxConnection { addEventListener<T extends keyof EventMap>(type: T, listener: ListenerType<T>): void; removeEventListener<T extends keyof EventMap>(type: T, listener: ListenerType<T>): void; } export {};