FromEventPatternObservable.d.ts 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import { Observable } from '../Observable';
  2. import { Subscriber } from '../Subscriber';
  3. /**
  4. * We need this JSDoc comment for affecting ESDoc.
  5. * @extends {Ignored}
  6. * @hide true
  7. */
  8. export declare class FromEventPatternObservable<T> extends Observable<T> {
  9. private addHandler;
  10. private removeHandler;
  11. private selector;
  12. /**
  13. * Creates an Observable from an API based on addHandler/removeHandler
  14. * functions.
  15. *
  16. * <span class="informal">Converts any addHandler/removeHandler API to an
  17. * Observable.</span>
  18. *
  19. * <img src="./img/fromEventPattern.png" width="100%">
  20. *
  21. * Creates an Observable by using the `addHandler` and `removeHandler`
  22. * functions to add and remove the handlers, with an optional selector
  23. * function to project the event arguments to a result. The `addHandler` is
  24. * called when the output Observable is subscribed, and `removeHandler` is
  25. * called when the Subscription is unsubscribed.
  26. *
  27. * @example <caption>Emits clicks happening on the DOM document</caption>
  28. * function addClickHandler(handler) {
  29. * document.addEventListener('click', handler);
  30. * }
  31. *
  32. * function removeClickHandler(handler) {
  33. * document.removeEventListener('click', handler);
  34. * }
  35. *
  36. * var clicks = Rx.Observable.fromEventPattern(
  37. * addClickHandler,
  38. * removeClickHandler
  39. * );
  40. * clicks.subscribe(x => console.log(x));
  41. *
  42. * @see {@link from}
  43. * @see {@link fromEvent}
  44. *
  45. * @param {function(handler: Function): any} addHandler A function that takes
  46. * a `handler` function as argument and attaches it somehow to the actual
  47. * source of events.
  48. * @param {function(handler: Function, signal?: any): void} [removeHandler] An optional function that
  49. * takes a `handler` function as argument and removes it in case it was
  50. * previously attached using `addHandler`. if addHandler returns signal to teardown when remove,
  51. * removeHandler function will forward it.
  52. * @param {function(...args: any): T} [selector] An optional function to
  53. * post-process results. It takes the arguments from the event handler and
  54. * should return a single value.
  55. * @return {Observable<T>}
  56. * @static true
  57. * @name fromEventPattern
  58. * @owner Observable
  59. */
  60. static create<T>(addHandler: (handler: Function) => any, removeHandler?: (handler: Function, signal?: any) => void, selector?: (...args: Array<any>) => T): FromEventPatternObservable<T>;
  61. constructor(addHandler: (handler: Function) => any, removeHandler?: (handler: Function, signal?: any) => void, selector?: (...args: Array<any>) => T);
  62. /** @deprecated internal use only */ _subscribe(subscriber: Subscriber<T>): void;
  63. private _callSelector(subscriber, args);
  64. private _callAddHandler(handler, errorSubscriber);
  65. }