timer.d.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import type { Observer } from "../Misc/observable";
  2. import { Observable } from "../Misc/observable";
  3. import type { Nullable } from "../types";
  4. import type { IDisposable } from "../scene";
  5. /**
  6. * Construction options for a timer
  7. */
  8. export interface ITimerOptions<T> {
  9. /**
  10. * Time-to-end
  11. */
  12. timeout: number;
  13. /**
  14. * The context observable is used to calculate time deltas and provides the context of the timer's callbacks. Will usually be OnBeforeRenderObservable.
  15. * Countdown calculation is done ONLY when the observable is notifying its observers, meaning that if
  16. * you choose an observable that doesn't trigger too often, the wait time might extend further than the requested max time
  17. */
  18. contextObservable: Observable<T>;
  19. /**
  20. * Optional parameters when adding an observer to the observable
  21. */
  22. observableParameters?: {
  23. mask?: number;
  24. insertFirst?: boolean;
  25. scope?: any;
  26. };
  27. /**
  28. * An optional break condition that will stop the times prematurely. In this case onEnded will not be triggered!
  29. */
  30. breakCondition?: (data?: ITimerData<T>) => boolean;
  31. /**
  32. * Will be triggered when the time condition has met
  33. */
  34. onEnded?: (data: ITimerData<any>) => void;
  35. /**
  36. * Will be triggered when the break condition has met (prematurely ended)
  37. */
  38. onAborted?: (data: ITimerData<any>) => void;
  39. /**
  40. * Optional function to execute on each tick (or count)
  41. */
  42. onTick?: (data: ITimerData<any>) => void;
  43. }
  44. /**
  45. * An interface defining the data sent by the timer
  46. */
  47. export interface ITimerData<T> {
  48. /**
  49. * When did it start
  50. */
  51. startTime: number;
  52. /**
  53. * Time now
  54. */
  55. currentTime: number;
  56. /**
  57. * Time passed since started
  58. */
  59. deltaTime: number;
  60. /**
  61. * How much is completed, in [0.0...1.0].
  62. * Note that this CAN be higher than 1 due to the fact that we don't actually measure time but delta between observable calls
  63. */
  64. completeRate: number;
  65. /**
  66. * What the registered observable sent in the last count
  67. */
  68. payload: T;
  69. }
  70. /**
  71. * The current state of the timer
  72. */
  73. export declare enum TimerState {
  74. /**
  75. * Timer initialized, not yet started
  76. */
  77. INIT = 0,
  78. /**
  79. * Timer started and counting
  80. */
  81. STARTED = 1,
  82. /**
  83. * Timer ended (whether aborted or time reached)
  84. */
  85. ENDED = 2
  86. }
  87. /**
  88. * A simple version of the timer. Will take options and start the timer immediately after calling it
  89. *
  90. * @param options options with which to initialize this timer
  91. * @returns an observer that can be used to stop the timer
  92. */
  93. export declare function setAndStartTimer<T = any>(options: ITimerOptions<T>): Nullable<Observer<T>>;
  94. /**
  95. * An advanced implementation of a timer class
  96. */
  97. export declare class AdvancedTimer<T = any> implements IDisposable {
  98. /**
  99. * Will notify each time the timer calculates the remaining time
  100. */
  101. onEachCountObservable: Observable<ITimerData<T>>;
  102. /**
  103. * Will trigger when the timer was aborted due to the break condition
  104. */
  105. onTimerAbortedObservable: Observable<ITimerData<T>>;
  106. /**
  107. * Will trigger when the timer ended successfully
  108. */
  109. onTimerEndedObservable: Observable<ITimerData<T>>;
  110. /**
  111. * Will trigger when the timer state has changed
  112. */
  113. onStateChangedObservable: Observable<TimerState>;
  114. private _observer;
  115. private _contextObservable;
  116. private _observableParameters;
  117. private _startTime;
  118. private _timer;
  119. private _state;
  120. private _breakCondition;
  121. private _timeToEnd;
  122. private _breakOnNextTick;
  123. /**
  124. * Will construct a new advanced timer based on the options provided. Timer will not start until start() is called.
  125. * @param options construction options for this advanced timer
  126. */
  127. constructor(options: ITimerOptions<T>);
  128. /**
  129. * set a breaking condition for this timer. Default is to never break during count
  130. * @param predicate the new break condition. Returns true to break, false otherwise
  131. */
  132. set breakCondition(predicate: (data: ITimerData<T>) => boolean);
  133. /**
  134. * Reset ALL associated observables in this advanced timer
  135. */
  136. clearObservables(): void;
  137. /**
  138. * Will start a new iteration of this timer. Only one instance of this timer can run at a time.
  139. *
  140. * @param timeToEnd how much time to measure until timer ended
  141. */
  142. start(timeToEnd?: number): void;
  143. /**
  144. * Will force a stop on the next tick.
  145. */
  146. stop(): void;
  147. /**
  148. * Dispose this timer, clearing all resources
  149. */
  150. dispose(): void;
  151. private _setState;
  152. private _tick;
  153. private _stop;
  154. }