UNPKG

2.65 kBTypeScriptView Raw
1import { AsyncOptionalCreatable, Duration } from '@salesforce/kit';
2import { AnyJson } from '@salesforce/ts-types';
3import { Logger } from '../logger';
4import { StatusResult } from './types';
5/**
6 * This is a polling client that can be used to poll the status of long running tasks. It can be used as a replacement
7 * for Streaming when streaming topics are not available or when streaming handshakes are failing. Why wouldn't you
8 * want to use this? It can impact Salesforce API usage.
9 *
10 * ```
11 * const options: PollingClient.Options = {
12 * async poll(): Promise<StatusResult> {
13 * return Promise.resolve({ completed: true, payload: 'Hello World' });
14 * },
15 * frequency: Duration.milliseconds(10),
16 * timeout: Duration.minutes(1)
17 * };
18 * const client = await PollingClient.create(options);
19 * const pollResult = await client.subscribe();
20 * console.log(`pollResult: ${pollResult}`);
21 * ```
22 */
23export declare class PollingClient extends AsyncOptionalCreatable<PollingClient.Options> {
24 protected logger: Logger;
25 private options;
26 /**
27 * Constructor
28 *
29 * @param options Polling client options
30 * @ignore
31 */
32 constructor(options?: PollingClient.Options);
33 /**
34 * Asynchronous initializer.
35 */
36 init(): Promise<void>;
37 /**
38 * Returns a promise to call the specified polling function using the interval and timeout specified
39 * in the polling options.
40 */
41 subscribe<T = AnyJson>(): Promise<T>;
42}
43export declare namespace PollingClient {
44 /**
45 * Options for the polling client.
46 */
47 interface Options {
48 /**
49 * Polling function.
50 */
51 poll: () => Promise<StatusResult>;
52 /**
53 * How frequent should the polling function be called.
54 */
55 frequency: Duration;
56 /**
57 * Hard timeout for polling.
58 */
59 timeout: Duration;
60 /**
61 * Change the name of the timeout error.
62 *
63 * ```
64 * if (err.name === 'MyChangedName) ...
65 * ```
66 */
67 timeoutErrorName?: string;
68 }
69 /**
70 * Default options set for polling. The default options specify a timeout of 3 minutes and polling frequency of 15
71 * seconds;
72 */
73 class DefaultPollingOptions implements PollingClient.Options {
74 frequency: Duration;
75 poll: () => Promise<StatusResult>;
76 timeout: Duration;
77 /**
78 * constructor
79 *
80 * @param poll The function used for polling status.
81 * {@link StatusResult}
82 */
83 constructor(poll: () => Promise<StatusResult>);
84 }
85}