UNPKG

3.02 kBTypeScriptView Raw
1import { EventEmitter } from 'node:events';
2import { AnyFunction, AnyJson, JsonMap } from '@salesforce/ts-types';
3export type Message = JsonMap;
4export type Callback<T = unknown> = (...args: any[]) => T;
5export type StatusResult = {
6 /**
7 * If the result of the streaming or polling client is expected to return a result
8 */
9 payload?: AnyJson;
10 /**
11 * Indicates to the streaming or polling client that the subscriber has what its needs. If `true` the client will end
12 * the messaging exchanges with the endpoint.
13 */
14 completed: boolean;
15};
16/**
17 * The subscription object returned from the cometd subscribe object.
18 */
19export type CometSubscription = {
20 callback(callback: () => void): void;
21 errback(callback: (error: Error) => void): void;
22};
23/**
24 * Types for defining extensions.
25 */
26export type StreamingExtension = {
27 /**
28 * Extension for outgoing message.
29 *
30 * @param message The message.
31 * @param callback The callback to invoke after the message is processed.
32 */
33 outgoing?: (message: JsonMap, callback: AnyFunction) => void;
34 /**
35 * Extension for the incoming message.
36 *
37 * @param message The message.
38 * @param callback The callback to invoke after the message is processed.
39 */
40 incoming?: (message: JsonMap, callback: AnyFunction) => void;
41};
42/**
43 * Function type for processing messages
44 */
45export declare type StreamProcessor = (message: JsonMap) => StatusResult;
46/**
47 * Comet client interface. The is to allow for mocking the inner streaming Cometd implementation.
48 * The Faye implementation is used by default but it could be used to adapt another Cometd impl.
49 */
50export declare abstract class CometClient extends EventEmitter {
51 /**
52 * Disable polling features.
53 *
54 * @param label Polling feature label.
55 */
56 abstract disable(label: string): void;
57 /**
58 * Add a custom extension to the underlying client.
59 *
60 * @param extension The json function for the extension.
61 */
62 abstract addExtension(extension: StreamingExtension): void;
63 /**
64 * Sets an http header name/value.
65 *
66 * @param name The header name.
67 * @param value The header value.
68 */
69 abstract setHeader(name: string, value: string): void;
70 /**
71 * handshake with the streaming channel
72 *
73 * @param callback Callback for the handshake when it successfully completes. The handshake should throw
74 * errors when errors are encountered.
75 */
76 abstract handshake(callback: () => void): void;
77 /**
78 * Subscribes to Comet topics. Subscribe should perform a handshake if one hasn't been performed yet.
79 *
80 * @param channel The topic to subscribe to.
81 * @param callback The callback to execute once a message has been received.
82 */
83 abstract subscribe(channel: string, callback: (message: JsonMap) => void): CometSubscription;
84 /**
85 * Method to call to disconnect the client from the server.
86 */
87 abstract disconnect(): void;
88}