UNPKG

@cygnus-reach/reach-protocol

Version:

Improve the Reachâ„  remote support experience with straightforward device interactions via the Reach Protocol.

120 lines (119 loc) • 4.3 kB
/// <reference types="node" /> import { ReachMessage, ReachMessageTypes } from "../proto/reach"; import { CompletablePromise } from "./CompletablePromise"; export declare class Request<T> { protected timeout: number; protected onMessage: (message: ReachMessage) => T; associatedTypes: ReachMessageTypes[]; ids: RequestID; stateless?: boolean | undefined; protected timer: NodeJS.Timeout | undefined; protected timeoutHandler: () => void; protected deferred: CompletablePromise<T>; protected _state: T | undefined; get state(): T | undefined; /** * Creates a Request with handling for a no-response timeout, associated message types, and optional internal state * @param timeout * The interval between messages before a request is considered timed out, in ms. This timer must be started with resetTimeout(). * @param onMessage * A function handling relevant incoming messages, returning an updated state if stateful * @param associatedTypes * The types of messages associated with this request and handled by onMessage. Error reports should generally not be included in this. * @param ids * The unique set of IDs accociated with a request * @param stateless * Whether the request is stateless. If set, wait() will return successfully even if no state has been set. */ constructor(timeout: number, onMessage: (message: ReachMessage) => T, associatedTypes: ReachMessageTypes[], ids: RequestID, stateless?: boolean | undefined); wait(): Promise<T>; receivedMessage(response: ReachMessage): void; resetTimeout(): void; complete(): void; fail(error: Error): void; protected onTimeout(): void; } /** * A model for a single message sent by a client within a transaction */ export interface RequestMessage { /** * The payload of the message */ payload: any; /** * The number of objects remaining in a chain of request messages within a transaction */ remainingObjects?: number; } /** * User-facing configuration used when creating and sending a Request */ export interface RequestConfig { /** * The time (in ms) without receiving a new response after which a request will be treated as failed */ timeout: number; /** * The ID of the client making a request to the server */ clientId: number; /** * The ID of the endpoint within a server being communicated with */ endpointId: number; /** * The ID of the transaction being conducted. This should generally be generated internally. * This should be unique independent of client ID and endpoint ID where possible, but only must be unique within its client-endpoint configuration */ transactionId: number; } /** * Low-level configuration used when creating and sending a Request */ export interface RequestMechanics { /** * A list of message types that may come in as valid responses to a request. */ expectedResponseTypes: ReachMessageTypes[]; /** * Whether the request's result is a pure success/fail */ stateless: boolean; } /** * A set of IDs that uniquely identify a request's transaction */ export declare class RequestID { /** * The ID of the client making a request to the server */ clientId: number; /** * The ID of the endpoint within a server being communicated with */ endpointId: number; /** * The ID of the transaction being conducted. * This should be unique independent of client ID and endpoint ID where possible, but only must be unique within its client-endpoint configuration */ transactionId: number; constructor( /** * The ID of the client making a request to the server */ clientId: number, /** * The ID of the endpoint within a server being communicated with */ endpointId: number, /** * The ID of the transaction being conducted. * This should be unique independent of client ID and endpoint ID where possible, but only must be unique within its client-endpoint configuration */ transactionId: number); /** * A formatted string of the three IDs, suitable for use as a key in a hashmap. */ get key(): string; }