observable.d.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. import type { Nullable } from "../types";
  2. /**
  3. * A class serves as a medium between the observable and its observers
  4. */
  5. export declare class EventState {
  6. /**
  7. * Create a new EventState
  8. * @param mask defines the mask associated with this state
  9. * @param skipNextObservers defines a flag which will instruct the observable to skip following observers when set to true
  10. * @param target defines the original target of the state
  11. * @param currentTarget defines the current target of the state
  12. */
  13. constructor(mask: number, skipNextObservers?: boolean, target?: any, currentTarget?: any);
  14. /**
  15. * Initialize the current event state
  16. * @param mask defines the mask associated with this state
  17. * @param skipNextObservers defines a flag which will instruct the observable to skip following observers when set to true
  18. * @param target defines the original target of the state
  19. * @param currentTarget defines the current target of the state
  20. * @returns the current event state
  21. */
  22. initialize(mask: number, skipNextObservers?: boolean, target?: any, currentTarget?: any): EventState;
  23. /**
  24. * An Observer can set this property to true to prevent subsequent observers of being notified
  25. */
  26. skipNextObservers: boolean;
  27. /**
  28. * Get the mask value that were used to trigger the event corresponding to this EventState object
  29. */
  30. mask: number;
  31. /**
  32. * The object that originally notified the event
  33. */
  34. target?: any;
  35. /**
  36. * The current object in the bubbling phase
  37. */
  38. currentTarget?: any;
  39. /**
  40. * This will be populated with the return value of the last function that was executed.
  41. * If it is the first function in the callback chain it will be the event data.
  42. */
  43. lastReturnValue?: any;
  44. /**
  45. * User defined information that will be sent to observers
  46. */
  47. userInfo?: any;
  48. }
  49. /**
  50. * Represent an Observer registered to a given Observable object.
  51. */
  52. export declare class Observer<T> {
  53. /**
  54. * Defines the callback to call when the observer is notified
  55. */
  56. callback: (eventData: T, eventState: EventState) => void;
  57. /**
  58. * Defines the mask of the observer (used to filter notifications)
  59. */
  60. mask: number;
  61. /**
  62. * Defines the current scope used to restore the JS context
  63. */
  64. scope: any;
  65. /** @internal */
  66. _willBeUnregistered: boolean;
  67. /**
  68. * Gets or sets a property defining that the observer as to be unregistered after the next notification
  69. */
  70. unregisterOnNextCall: boolean;
  71. /**
  72. * this function can be used to remove the observer from the observable.
  73. * It will be set by the observable that the observer belongs to.
  74. * @internal
  75. */
  76. _remove: Nullable<() => void>;
  77. /**
  78. * Creates a new observer
  79. * @param callback defines the callback to call when the observer is notified
  80. * @param mask defines the mask of the observer (used to filter notifications)
  81. * @param scope defines the current scope used to restore the JS context
  82. */
  83. constructor(
  84. /**
  85. * Defines the callback to call when the observer is notified
  86. */
  87. callback: (eventData: T, eventState: EventState) => void,
  88. /**
  89. * Defines the mask of the observer (used to filter notifications)
  90. */
  91. mask: number,
  92. /**
  93. * Defines the current scope used to restore the JS context
  94. */
  95. scope?: any);
  96. /**
  97. * Remove the observer from its observable
  98. * This can be used instead of using the observable's remove function.
  99. */
  100. remove(): void;
  101. }
  102. /**
  103. * The Observable class is a simple implementation of the Observable pattern.
  104. *
  105. * There's one slight particularity though: a given Observable can notify its observer using a particular mask value, only the Observers registered with this mask value will be notified.
  106. * This enable a more fine grained execution without having to rely on multiple different Observable objects.
  107. * For instance you may have a given Observable that have four different types of notifications: Move (mask = 0x01), Stop (mask = 0x02), Turn Right (mask = 0X04), Turn Left (mask = 0X08).
  108. * A given observer can register itself with only Move and Stop (mask = 0x03), then it will only be notified when one of these two occurs and will never be for Turn Left/Right.
  109. */
  110. export declare class Observable<T> {
  111. /**
  112. * If set to true the observable will notify when an observer was added if the observable was already triggered.
  113. * This is helpful to single-state observables like the scene onReady or the dispose observable.
  114. */
  115. notifyIfTriggered: boolean;
  116. private _observers;
  117. private _numObserversMarkedAsDeleted;
  118. private _hasNotified;
  119. private _lastNotifiedValue?;
  120. /**
  121. * @internal
  122. */
  123. _eventState: EventState;
  124. private _onObserverAdded;
  125. /**
  126. * Create an observable from a Promise.
  127. * @param promise a promise to observe for fulfillment.
  128. * @param onErrorObservable an observable to notify if a promise was rejected.
  129. * @returns the new Observable
  130. */
  131. static FromPromise<T, E = Error>(promise: Promise<T>, onErrorObservable?: Observable<E>): Observable<T>;
  132. /**
  133. * Gets the list of observers
  134. * Note that observers that were recently deleted may still be present in the list because they are only really deleted on the next javascript tick!
  135. */
  136. get observers(): Array<Observer<T>>;
  137. /**
  138. * Creates a new observable
  139. * @param onObserverAdded defines a callback to call when a new observer is added
  140. * @param notifyIfTriggered If set to true the observable will notify when an observer was added if the observable was already triggered.
  141. */
  142. constructor(onObserverAdded?: (observer: Observer<T>) => void,
  143. /**
  144. * If set to true the observable will notify when an observer was added if the observable was already triggered.
  145. * This is helpful to single-state observables like the scene onReady or the dispose observable.
  146. */
  147. notifyIfTriggered?: boolean);
  148. /**
  149. * Create a new Observer with the specified callback
  150. * @param callback the callback that will be executed for that Observer
  151. * @param mask the mask used to filter observers
  152. * @param insertFirst if true the callback will be inserted at the first position, hence executed before the others ones. If false (default behavior) the callback will be inserted at the last position, executed after all the others already present.
  153. * @param scope optional scope for the callback to be called from
  154. * @param unregisterOnFirstCall defines if the observer as to be unregistered after the next notification
  155. * @returns the new observer created for the callback
  156. */
  157. add(callback?: null | undefined, mask?: number, insertFirst?: boolean, scope?: any, unregisterOnFirstCall?: boolean): null;
  158. add(callback: (eventData: T, eventState: EventState) => void, mask?: number, insertFirst?: boolean, scope?: any, unregisterOnFirstCall?: boolean): Observer<T>;
  159. add(callback?: ((eventData: T, eventState: EventState) => void) | null | undefined, mask?: number, insertFirst?: boolean, scope?: any, unregisterOnFirstCall?: boolean): Nullable<Observer<T>>;
  160. /**
  161. * Create a new Observer with the specified callback and unregisters after the next notification
  162. * @param callback the callback that will be executed for that Observer
  163. * @returns the new observer created for the callback
  164. */
  165. addOnce(callback?: null | undefined): null;
  166. addOnce(callback: (eventData: T, eventState: EventState) => void): Observer<T>;
  167. addOnce(callback?: ((eventData: T, eventState: EventState) => void) | null | undefined): Nullable<Observer<T>>;
  168. /**
  169. * Remove an Observer from the Observable object
  170. * @param observer the instance of the Observer to remove
  171. * @returns false if it doesn't belong to this Observable
  172. */
  173. remove(observer: Nullable<Observer<T>>): boolean;
  174. /**
  175. * Remove a callback from the Observable object
  176. * @param callback the callback to remove
  177. * @param scope optional scope. If used only the callbacks with this scope will be removed
  178. * @returns false if it doesn't belong to this Observable
  179. */
  180. removeCallback(callback: (eventData: T, eventState: EventState) => void, scope?: any): boolean;
  181. /**
  182. * @internal
  183. */
  184. _deferUnregister(observer: Observer<T>): void;
  185. private _remove;
  186. /**
  187. * Moves the observable to the top of the observer list making it get called first when notified
  188. * @param observer the observer to move
  189. */
  190. makeObserverTopPriority(observer: Observer<T>): void;
  191. /**
  192. * Moves the observable to the bottom of the observer list making it get called last when notified
  193. * @param observer the observer to move
  194. */
  195. makeObserverBottomPriority(observer: Observer<T>): void;
  196. /**
  197. * Notify all Observers by calling their respective callback with the given data
  198. * Will return true if all observers were executed, false if an observer set skipNextObservers to true, then prevent the subsequent ones to execute
  199. * @param eventData defines the data to send to all observers
  200. * @param mask defines the mask of the current notification (observers with incompatible mask (ie mask & observer.mask === 0) will not be notified)
  201. * @param target defines the original target of the state
  202. * @param currentTarget defines the current target of the state
  203. * @param userInfo defines any user info to send to observers
  204. * @returns false if the complete observer chain was not processed (because one observer set the skipNextObservers to true)
  205. */
  206. notifyObservers(eventData: T, mask?: number, target?: any, currentTarget?: any, userInfo?: any): boolean;
  207. /**
  208. * Notify a specific observer
  209. * @param observer defines the observer to notify
  210. * @param eventData defines the data to be sent to each callback
  211. * @param mask is used to filter observers defaults to -1
  212. */
  213. notifyObserver(observer: Observer<T>, eventData: T, mask?: number): void;
  214. /**
  215. * Gets a boolean indicating if the observable has at least one observer
  216. * @returns true is the Observable has at least one Observer registered
  217. */
  218. hasObservers(): boolean;
  219. /**
  220. * Clear the list of observers
  221. */
  222. clear(): void;
  223. /**
  224. * Clean the last notified state - both the internal last value and the has-notified flag
  225. */
  226. cleanLastNotifiedState(): void;
  227. /**
  228. * Clone the current observable
  229. * @returns a new observable
  230. */
  231. clone(): Observable<T>;
  232. /**
  233. * Does this observable handles observer registered with a given mask
  234. * @param mask defines the mask to be tested
  235. * @returns whether or not one observer registered with the given mask is handled
  236. **/
  237. hasSpecificMask(mask?: number): boolean;
  238. }