poll.d.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. import { IObservableDisposable } from '@lumino/disposable';
  2. import { ISignal } from '@lumino/signaling';
  3. import { IPoll } from './index';
  4. /**
  5. * A class that wraps an asynchronous function to poll at a regular interval
  6. * with exponential increases to the interval length if the poll fails.
  7. *
  8. * @typeparam T - The resolved type of the factory's promises.
  9. * Defaults to `any`.
  10. *
  11. * @typeparam U - The rejected type of the factory's promises.
  12. * Defaults to `any`.
  13. *
  14. * @typeparam V - An optional type to extend the phases supported by a poll.
  15. * Defaults to `standby`, which already exists in the `Phase` type.
  16. */
  17. export declare class Poll<T = any, U = any, V extends string = 'standby'> implements IObservableDisposable, IPoll<T, U, V> {
  18. /**
  19. * Instantiate a new poll with exponential backoff in case of failure.
  20. *
  21. * @param options - The poll instantiation options.
  22. */
  23. constructor(options: Poll.IOptions<T, U, V>);
  24. /**
  25. * The name of the poll.
  26. */
  27. readonly name: string;
  28. /**
  29. * A signal emitted when the poll is disposed.
  30. */
  31. readonly disposed: ISignal<this, void>;
  32. /**
  33. * The polling frequency parameters.
  34. */
  35. frequency: IPoll.Frequency;
  36. /**
  37. * Whether the poll is disposed.
  38. */
  39. readonly isDisposed: boolean;
  40. /**
  41. * Indicates when the poll switches to standby.
  42. */
  43. standby: Poll.Standby | (() => boolean | Poll.Standby);
  44. /**
  45. * The poll state, which is the content of the current poll tick.
  46. */
  47. readonly state: IPoll.State<T, U, V>;
  48. /**
  49. * A promise that resolves when the poll next ticks.
  50. */
  51. readonly tick: Promise<this>;
  52. /**
  53. * A signal emitted when the poll ticks and fires off a new request.
  54. */
  55. readonly ticked: ISignal<this, IPoll.State<T, U, V>>;
  56. /**
  57. * Dispose the poll.
  58. */
  59. dispose(): void;
  60. /**
  61. * Refreshes the poll. Schedules `refreshed` tick if necessary.
  62. *
  63. * @returns A promise that resolves after tick is scheduled and never rejects.
  64. *
  65. * #### Notes
  66. * The returned promise resolves after the tick is scheduled, but before
  67. * the polling action is run. To wait until after the poll action executes,
  68. * await the `poll.tick` promise: `await poll.refresh(); await poll.tick;`
  69. */
  70. refresh(): Promise<void>;
  71. /**
  72. * Schedule the next poll tick.
  73. *
  74. * @param next - The next poll state data to schedule. Defaults to standby.
  75. *
  76. * @param next.cancel - Cancels state transition if function returns `true`.
  77. *
  78. * @returns A promise that resolves when the next poll state is active.
  79. *
  80. * #### Notes
  81. * This method is not meant to be invoked by user code typically. It is public
  82. * to allow poll instances to be composed into classes that schedule ticks.
  83. */
  84. schedule(next?: Partial<IPoll.State<T, U, V> & {
  85. cancel: (last: IPoll.State<T, U, V>) => boolean;
  86. }>): Promise<void>;
  87. /**
  88. * Starts the poll. Schedules `started` tick if necessary.
  89. *
  90. * @returns A promise that resolves after tick is scheduled and never rejects.
  91. */
  92. start(): Promise<void>;
  93. /**
  94. * Stops the poll. Schedules `stopped` tick if necessary.
  95. *
  96. * @returns A promise that resolves after tick is scheduled and never rejects.
  97. */
  98. stop(): Promise<void>;
  99. /**
  100. * Execute a new poll factory promise or stand by if necessary.
  101. */
  102. private _execute;
  103. private _disposed;
  104. private _factory;
  105. private _frequency;
  106. private _standby;
  107. private _state;
  108. private _tick;
  109. private _ticked;
  110. private _timeout;
  111. }
  112. /**
  113. * A namespace for `Poll` types, interfaces, and statics.
  114. */
  115. export declare namespace Poll {
  116. /**
  117. * A promise factory that returns an individual poll request.
  118. *
  119. * @typeparam T - The resolved type of the factory's promises.
  120. *
  121. * @typeparam U - The rejected type of the factory's promises.
  122. *
  123. * @typeparam V - The type to extend the phases supported by a poll.
  124. */
  125. type Factory<T, U, V extends string> = (state: IPoll.State<T, U, V>) => Promise<T>;
  126. /**
  127. * Indicates when the poll switches to standby.
  128. */
  129. type Standby = 'never' | 'when-hidden';
  130. /**
  131. * Instantiation options for polls.
  132. *
  133. * @typeparam T - The resolved type of the factory's promises.
  134. *
  135. * @typeparam U - The rejected type of the factory's promises.
  136. *
  137. * @typeparam V - The type to extend the phases supported by a poll.
  138. */
  139. interface IOptions<T, U, V extends string> {
  140. /**
  141. * Whether to begin polling automatically; defaults to `true`.
  142. */
  143. auto?: boolean;
  144. /**
  145. * A factory function that is passed a poll tick and returns a poll promise.
  146. */
  147. factory: Factory<T, U, V>;
  148. /**
  149. * The polling frequency parameters.
  150. */
  151. frequency?: Partial<IPoll.Frequency>;
  152. /**
  153. * The name of the poll.
  154. * Defaults to `'unknown'`.
  155. */
  156. name?: string;
  157. /**
  158. * Indicates when the poll switches to standby or a function that returns
  159. * a boolean or a `Poll.Standby` value to indicate whether to stand by.
  160. * Defaults to `'when-hidden'`.
  161. *
  162. * #### Notes
  163. * If a function is passed in, for any given context, it should be
  164. * idempotent and safe to call multiple times. It will be called before each
  165. * tick execution, but may be called by clients as well.
  166. */
  167. standby?: Standby | (() => boolean | Standby);
  168. }
  169. /**
  170. * An interval value that indicates the poll should tick immediately.
  171. */
  172. const IMMEDIATE = 0;
  173. /**
  174. * Delays are 32-bit integers in many browsers so intervals need to be capped.
  175. *
  176. * #### Notes
  177. * https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout#Maximum_delay_value
  178. */
  179. const MAX_INTERVAL = 2147483647;
  180. /**
  181. * An interval value that indicates the poll should never tick.
  182. */
  183. const NEVER: number;
  184. }
  185. //# sourceMappingURL=poll.d.ts.map