MiscJSDoc.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. /*
  2. * This file and its definitions are needed just so that ESDoc sees these
  3. * JSDoc documentation comments. Originally they were meant for some TypeScript
  4. * interfaces, but TypeScript strips away JSDoc comments near interfaces. Hence,
  5. * we need these bogus classes, which are not stripped away. This file on the
  6. * other hand, is not included in the release bundle.
  7. */
  8. import { TeardownLogic } from './Subscription';
  9. import { Observable } from './Observable';
  10. import './observable/dom/MiscJSDoc';
  11. import { Observer } from './Observer';
  12. /**
  13. * We need this JSDoc comment for affecting ESDoc.
  14. * @extends {Ignored}
  15. * @hide true
  16. */
  17. export class ObservableDoc {
  18. /**
  19. * Creates a new Observable, that will execute the specified function when an
  20. * {@link Observer} subscribes to it.
  21. *
  22. * <span class="informal">Create custom Observable, that does whatever you like.</span>
  23. *
  24. * <img src="./img/create.png" width="100%">
  25. *
  26. * `create` converts an `onSubscription` function to an actual Observable.
  27. * Whenever someone subscribes to that Observable, the function will be called
  28. * with an {@link Observer} instance as a first and only parameter. `onSubscription` should
  29. * then invoke the Observers `next`, `error` and `complete` methods.
  30. *
  31. * Calling `next` with a value will emit that value to the observer. Calling `complete`
  32. * means that Observable finished emitting and will not do anything else.
  33. * Calling `error` means that something went wrong - value passed to `error` method should
  34. * provide details on what exactly happened.
  35. *
  36. * A well-formed Observable can emit as many values as it needs via `next` method,
  37. * but `complete` and `error` methods can be called only once and nothing else can be called
  38. * thereafter. If you try to invoke `next`, `complete` or `error` methods after created
  39. * Observable already completed or ended with an error, these calls will be ignored to
  40. * preserve so called *Observable Contract*. Note that you are not required to call
  41. * `complete` at any point - it is perfectly fine to create an Observable that never ends,
  42. * depending on your needs.
  43. *
  44. * `onSubscription` can optionally return either a function or an object with
  45. * `unsubscribe` method. In both cases function or method will be called when
  46. * subscription to Observable is being cancelled and should be used to clean up all
  47. * resources. So, for example, if you are using `setTimeout` in your custom
  48. * Observable, when someone unsubscribes, you can clear planned timeout, so that
  49. * it does not fire needlessly and browser (or other environment) does not waste
  50. * computing power on timing event that no one will listen to anyways.
  51. *
  52. * Most of the times you should not need to use `create`, because existing
  53. * operators allow you to create an Observable for most of the use cases.
  54. * That being said, `create` is low-level mechanism allowing you to create
  55. * any Observable, if you have very specific needs.
  56. *
  57. * **TypeScript signature issue**
  58. *
  59. * Because Observable extends class which already has defined static `create` function,
  60. * but with different type signature, it was impossible to assign proper signature to
  61. * `Observable.create`. Because of that, it has very general type `Function` and thus
  62. * function passed to `create` will not be type checked, unless you explicitly state
  63. * what signature it should have.
  64. *
  65. * When using TypeScript we recommend to declare type signature of function passed to
  66. * `create` as `(observer: Observer) => TeardownLogic`, where {@link Observer}
  67. * and {@link TeardownLogic} are interfaces provided by the library.
  68. *
  69. * @example <caption>Emit three numbers, then complete.</caption>
  70. * var observable = Rx.Observable.create(function (observer) {
  71. * observer.next(1);
  72. * observer.next(2);
  73. * observer.next(3);
  74. * observer.complete();
  75. * });
  76. * observable.subscribe(
  77. * value => console.log(value),
  78. * err => {},
  79. * () => console.log('this is the end')
  80. * );
  81. *
  82. * // Logs
  83. * // 1
  84. * // 2
  85. * // 3
  86. * // "this is the end"
  87. *
  88. *
  89. * @example <caption>Emit an error</caption>
  90. * const observable = Rx.Observable.create((observer) => {
  91. * observer.error('something went really wrong...');
  92. * });
  93. *
  94. * observable.subscribe(
  95. * value => console.log(value), // will never be called
  96. * err => console.log(err),
  97. * () => console.log('complete') // will never be called
  98. * );
  99. *
  100. * // Logs
  101. * // "something went really wrong..."
  102. *
  103. *
  104. * @example <caption>Return unsubscribe function</caption>
  105. *
  106. * const observable = Rx.Observable.create(observer => {
  107. * const id = setTimeout(() => observer.next('...'), 5000); // emit value after 5s
  108. *
  109. * return () => { clearTimeout(id); console.log('cleared!'); };
  110. * });
  111. *
  112. * const subscription = observable.subscribe(value => console.log(value));
  113. *
  114. * setTimeout(() => subscription.unsubscribe(), 3000); // cancel subscription after 3s
  115. *
  116. * // Logs:
  117. * // "cleared!" after 3s
  118. *
  119. * // Never logs "..."
  120. *
  121. *
  122. * @see {@link empty}
  123. * @see {@link never}
  124. * @see {@link of}
  125. * @see {@link throw}
  126. *
  127. * @param {function(observer: Observer): TeardownLogic} onSubscription A
  128. * function that accepts an Observer, and invokes its `next`,
  129. * `error`, and `complete` methods as appropriate, and optionally returns some
  130. * logic for cleaning up resources.
  131. * @return {Observable} An Observable that, whenever subscribed, will execute the
  132. * specified function.
  133. * @static true
  134. * @name create
  135. * @owner Observable
  136. */
  137. static create<T>(onSubscription: <R>(observer: Observer<R>) => TeardownLogic): Observable<T> {
  138. return new Observable<T>(onSubscription);
  139. };
  140. }
  141. /**
  142. * An interface for a consumer of push-based notifications delivered by an
  143. * {@link Observable}.
  144. *
  145. * ```ts
  146. * interface Observer<T> {
  147. * closed?: boolean;
  148. * next: (value: T) => void;
  149. * error: (err: any) => void;
  150. * complete: () => void;
  151. * }
  152. * ```
  153. *
  154. * An object conforming to the Observer interface is usually
  155. * given to the `observable.subscribe(observer)` method, and the Observable will
  156. * call the Observer's `next(value)` method to provide notifications. A
  157. * well-behaved Observable will call an Observer's `complete()` method exactly
  158. * once or the Observer's `error(err)` method exactly once, as the last
  159. * notification delivered.
  160. *
  161. * @interface
  162. * @name Observer
  163. * @noimport true
  164. */
  165. export class ObserverDoc<T> {
  166. /**
  167. * An optional flag to indicate whether this Observer, when used as a
  168. * subscriber, has already been unsubscribed from its Observable.
  169. * @type {boolean}
  170. */
  171. closed: boolean = false;
  172. /**
  173. * The callback to receive notifications of type `next` from the Observable,
  174. * with a value. The Observable may call this method 0 or more times.
  175. * @param {T} value The `next` value.
  176. * @return {void}
  177. */
  178. next(value: T): void {
  179. return void 0;
  180. }
  181. /**
  182. * The callback to receive notifications of type `error` from the Observable,
  183. * with an attached {@link Error}. Notifies the Observer that the Observable
  184. * has experienced an error condition.
  185. * @param {any} err The `error` exception.
  186. * @return {void}
  187. */
  188. error(err: any): void {
  189. return void 0;
  190. }
  191. /**
  192. * The callback to receive a valueless notification of type `complete` from
  193. * the Observable. Notifies the Observer that the Observable has finished
  194. * sending push-based notifications.
  195. * @return {void}
  196. */
  197. complete(): void {
  198. return void 0;
  199. }
  200. }
  201. /**
  202. * `SubscribableOrPromise` interface describes values that behave like either
  203. * Observables or Promises. Every operator that accepts arguments annotated
  204. * with this interface, can be also used with parameters that are not necessarily
  205. * RxJS Observables.
  206. *
  207. * Following types of values might be passed to operators expecting this interface:
  208. *
  209. * ## Observable
  210. *
  211. * RxJS {@link Observable} instance.
  212. *
  213. * ## Observable-like (Subscribable)
  214. *
  215. * This might be any object that has `Symbol.observable` method. This method,
  216. * when called, should return object with `subscribe` method on it, which should
  217. * behave the same as RxJS `Observable.subscribe`.
  218. *
  219. * `Symbol.observable` is part of https://github.com/tc39/proposal-observable proposal.
  220. * Since currently it is not supported natively, and every symbol is equal only to itself,
  221. * you should use https://github.com/blesh/symbol-observable polyfill, when implementing
  222. * custom Observable-likes.
  223. *
  224. * **TypeScript Subscribable interface issue**
  225. *
  226. * Although TypeScript interface claims that Subscribable is an object that has `subscribe`
  227. * method declared directly on it, passing custom objects that have `subscribe`
  228. * method but not `Symbol.observable` method will fail at runtime. Conversely, passing
  229. * objects with `Symbol.observable` but without `subscribe` will fail at compile time
  230. * (if you use TypeScript).
  231. *
  232. * TypeScript has problem supporting interfaces with methods defined as symbol
  233. * properties. To get around that, you should implement `subscribe` directly on
  234. * passed object, and make `Symbol.observable` method simply return `this`. That way
  235. * everything will work as expected, and compiler will not complain. If you really
  236. * do not want to put `subscribe` directly on your object, you will have to type cast
  237. * it to `any`, before passing it to an operator.
  238. *
  239. * When this issue is resolved, Subscribable interface will only permit Observable-like
  240. * objects with `Symbol.observable` defined, no matter if they themselves implement
  241. * `subscribe` method or not.
  242. *
  243. * ## ES6 Promise
  244. *
  245. * Promise can be interpreted as Observable that emits value and completes
  246. * when it is resolved or errors when it is rejected.
  247. *
  248. * ## Promise-like (Thenable)
  249. *
  250. * Promises passed to operators do not have to be native ES6 Promises.
  251. * They can be implementations from popular Promise libraries, polyfills
  252. * or even custom ones. They just need to have `then` method that works
  253. * as the same as ES6 Promise `then`.
  254. *
  255. * @example <caption>Use merge and then map with non-RxJS observable</caption>
  256. * const nonRxJSObservable = {
  257. * subscribe(observer) {
  258. * observer.next(1000);
  259. * observer.complete();
  260. * },
  261. * [Symbol.observable]() {
  262. * return this;
  263. * }
  264. * };
  265. *
  266. * Rx.Observable.merge(nonRxJSObservable)
  267. * .map(value => "This value is " + value)
  268. * .subscribe(result => console.log(result)); // Logs "This value is 1000"
  269. *
  270. *
  271. * @example <caption>Use combineLatest with ES6 Promise</caption>
  272. * Rx.Observable.combineLatest(Promise.resolve(5), Promise.resolve(10), Promise.resolve(15))
  273. * .subscribe(
  274. * value => console.log(value),
  275. * err => {},
  276. * () => console.log('the end!')
  277. * );
  278. * // Logs
  279. * // [5, 10, 15]
  280. * // "the end!"
  281. *
  282. *
  283. * @interface
  284. * @name SubscribableOrPromise
  285. * @noimport true
  286. */
  287. export class SubscribableOrPromiseDoc<T> {
  288. }
  289. /**
  290. * `ObservableInput` interface describes all values that are either an
  291. * {@link SubscribableOrPromise} or some kind of collection of values that
  292. * can be transformed to Observable emitting that values. Every operator that
  293. * accepts arguments annotated with this interface, can be also used with
  294. * parameters that are not necessarily RxJS Observables.
  295. *
  296. * `ObservableInput` extends {@link SubscribableOrPromise} with following types:
  297. *
  298. * ## Array
  299. *
  300. * Arrays can be interpreted as observables that emit all values in array one by one,
  301. * from left to right, and then complete immediately.
  302. *
  303. * ## Array-like
  304. *
  305. * Arrays passed to operators do not have to be built-in JavaScript Arrays. They
  306. * can be also, for example, `arguments` property available inside every function,
  307. * [DOM NodeList](https://developer.mozilla.org/pl/docs/Web/API/NodeList),
  308. * or, actually, any object that has `length` property (which is a number)
  309. * and stores values under non-negative (zero and up) integers.
  310. *
  311. * ## ES6 Iterable
  312. *
  313. * Operators will accept both built-in and custom ES6 Iterables, by treating them as
  314. * observables that emit all its values in order of iteration and then complete
  315. * when iteration ends. Note that contrary to arrays, Iterables do not have to
  316. * necessarily be finite, so creating Observables that never complete is possible as well.
  317. *
  318. * Note that you can make iterator an instance of Iterable by having it return itself
  319. * in `Symbol.iterator` method. It means that every operator accepting Iterables accepts,
  320. * though indirectly, iterators themselves as well. All native ES6 iterators are instances
  321. * of Iterable by default, so you do not have to implement their `Symbol.iterator` method
  322. * yourself.
  323. *
  324. * **TypeScript Iterable interface issue**
  325. *
  326. * TypeScript `ObservableInput` interface actually lacks type signature for Iterables,
  327. * because of issues it caused in some projects (see [this issue](https://github.com/ReactiveX/rxjs/issues/2306)).
  328. * If you want to use Iterable as argument for operator, cast it to `any` first.
  329. * Remember of course that, because of casting, you have to yourself ensure that passed
  330. * argument really implements said interface.
  331. *
  332. *
  333. * @example <caption>Use merge with arrays</caption>
  334. * Rx.Observable.merge([1, 2], [4], [5, 6])
  335. * .subscribe(
  336. * value => console.log(value),
  337. * err => {},
  338. * () => console.log('ta dam!')
  339. * );
  340. *
  341. * // Logs
  342. * // 1
  343. * // 2
  344. * // 3
  345. * // 4
  346. * // 5
  347. * // 6
  348. * // "ta dam!"
  349. *
  350. *
  351. * @example <caption>Use merge with array-like</caption>
  352. * Rx.Observable.merge({0: 1, 1: 2, length: 2}, {0: 3, length: 1})
  353. * .subscribe(
  354. * value => console.log(value),
  355. * err => {},
  356. * () => console.log('nice, huh?')
  357. * );
  358. *
  359. * // Logs
  360. * // 1
  361. * // 2
  362. * // 3
  363. * // "nice, huh?"
  364. *
  365. * @example <caption>Use merge with an Iterable (Map)</caption>
  366. * const firstMap = new Map([[1, 'a'], [2, 'b']]);
  367. * const secondMap = new Map([[3, 'c'], [4, 'd']]);
  368. *
  369. * Rx.Observable.merge(
  370. * firstMap, // pass Iterable
  371. * secondMap.values() // pass iterator, which is itself an Iterable
  372. * ).subscribe(
  373. * value => console.log(value),
  374. * err => {},
  375. * () => console.log('yup!')
  376. * );
  377. *
  378. * // Logs
  379. * // [1, "a"]
  380. * // [2, "b"]
  381. * // "c"
  382. * // "d"
  383. * // "yup!"
  384. *
  385. * @example <caption>Use from with generator (returning infinite iterator)</caption>
  386. * // infinite stream of incrementing numbers
  387. * const infinite = function* () {
  388. * let i = 0;
  389. *
  390. * while (true) {
  391. * yield i++;
  392. * }
  393. * };
  394. *
  395. * Rx.Observable.from(infinite())
  396. * .take(3) // only take 3, cause this is infinite
  397. * .subscribe(
  398. * value => console.log(value),
  399. * err => {},
  400. * () => console.log('ta dam!')
  401. * );
  402. *
  403. * // Logs
  404. * // 0
  405. * // 1
  406. * // 2
  407. * // "ta dam!"
  408. *
  409. * @interface
  410. * @name ObservableInput
  411. * @noimport true
  412. */
  413. export class ObservableInputDoc<T> {
  414. }
  415. /**
  416. *
  417. * This interface describes what should be returned by function passed to Observable
  418. * constructor or static {@link create} function. Value of that interface will be used
  419. * to cancel subscription for given Observable.
  420. *
  421. * `TeardownLogic` can be:
  422. *
  423. * ## Function
  424. *
  425. * Function that takes no parameters. When consumer of created Observable calls `unsubscribe`,
  426. * that function will be called
  427. *
  428. * ## AnonymousSubscription
  429. *
  430. * `AnonymousSubscription` is simply an object with `unsubscribe` method on it. That method
  431. * will work the same as function
  432. *
  433. * ## void
  434. *
  435. * If created Observable does not have any resources to clean up, function does not have to
  436. * return anything.
  437. *
  438. * @interface
  439. * @name TeardownLogic
  440. * @noimport true
  441. */
  442. export class TeardownLogicDoc {
  443. }