You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

77 lines
2.9 KiB

  1. /** @license
  2. * Copyright 2016 - present The Material Motion Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy
  6. * of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations
  14. * under the License.
  15. */
  16. "use strict";
  17. const symbol_observable_1 = require("symbol-observable");
  18. const wrapWithObserver_1 = require("./wrapWithObserver");
  19. /**
  20. * Observable is a standard interface that's useful for modeling multiple,
  21. * asynchronous events.
  22. *
  23. * IndefiniteObservable is a minimalist implementation of a subset of the TC39
  24. * Observable proposal. It is indefinite because it will never call `complete`
  25. * or `error` on the provided observer.
  26. */
  27. class IndefiniteObservable {
  28. /**
  29. * The provided function should receive an observer and connect that
  30. * observer's `next` method to an event source (for instance,
  31. * `element.addEventListener('click', observer.next)`).
  32. *
  33. * It must return a function that will disconnect the observer from the event
  34. * source.
  35. */
  36. constructor(connect) {
  37. this._connect = connect;
  38. }
  39. /**
  40. * `subscribe` uses the function supplied to the constructor to connect an
  41. * observer to an event source. Each observer is connected independently:
  42. * each call to `subscribe` calls `connect` with the new observer.
  43. *
  44. * To disconnect the observer from the event source, call `unsubscribe` on the
  45. * returned subscription.
  46. *
  47. * Note: `subscribe` accepts either a function or an object with a
  48. * next method.
  49. */
  50. subscribe(observerOrNext) {
  51. // For simplicity's sake, `subscribe` accepts `next` either as either an
  52. // anonymous function or wrapped in an object (the observer). Since
  53. // `connect` always expects to receive an observer, wrap any loose
  54. // functions in an object.
  55. const observer = wrapWithObserver_1.default(observerOrNext);
  56. let disconnect = this._connect(observer);
  57. return {
  58. unsubscribe() {
  59. if (disconnect) {
  60. disconnect();
  61. disconnect = undefined;
  62. }
  63. }
  64. };
  65. }
  66. /**
  67. * Tells other libraries that know about observables that we are one.
  68. *
  69. * https://github.com/tc39/proposal-observable#observable
  70. */
  71. [symbol_observable_1.default]() {
  72. return this;
  73. }
  74. }
  75. Object.defineProperty(exports, "__esModule", { value: true });
  76. exports.default = IndefiniteObservable;
  77. //# sourceMappingURL=IndefiniteObservable.js.map