UNPKG

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