ArrayLikeObservable.js 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /** PURE_IMPORTS_START .._Observable,._ScalarObservable,._EmptyObservable PURE_IMPORTS_END */
  2. var __extends = (this && this.__extends) || function (d, b) {
  3. for (var p in b)
  4. if (b.hasOwnProperty(p))
  5. d[p] = b[p];
  6. function __() { this.constructor = d; }
  7. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  8. };
  9. import { Observable } from '../Observable';
  10. import { ScalarObservable } from './ScalarObservable';
  11. import { EmptyObservable } from './EmptyObservable';
  12. /**
  13. * We need this JSDoc comment for affecting ESDoc.
  14. * @extends {Ignored}
  15. * @hide true
  16. */
  17. export var ArrayLikeObservable = /*@__PURE__*/ (/*@__PURE__*/ function (_super) {
  18. __extends(ArrayLikeObservable, _super);
  19. function ArrayLikeObservable(arrayLike, scheduler) {
  20. _super.call(this);
  21. this.arrayLike = arrayLike;
  22. this.scheduler = scheduler;
  23. if (!scheduler && arrayLike.length === 1) {
  24. this._isScalar = true;
  25. this.value = arrayLike[0];
  26. }
  27. }
  28. ArrayLikeObservable.create = function (arrayLike, scheduler) {
  29. var length = arrayLike.length;
  30. if (length === 0) {
  31. return new EmptyObservable();
  32. }
  33. else if (length === 1) {
  34. return new ScalarObservable(arrayLike[0], scheduler);
  35. }
  36. else {
  37. return new ArrayLikeObservable(arrayLike, scheduler);
  38. }
  39. };
  40. ArrayLikeObservable.dispatch = function (state) {
  41. var arrayLike = state.arrayLike, index = state.index, length = state.length, subscriber = state.subscriber;
  42. if (subscriber.closed) {
  43. return;
  44. }
  45. if (index >= length) {
  46. subscriber.complete();
  47. return;
  48. }
  49. subscriber.next(arrayLike[index]);
  50. state.index = index + 1;
  51. this.schedule(state);
  52. };
  53. /** @deprecated internal use only */ ArrayLikeObservable.prototype._subscribe = function (subscriber) {
  54. var index = 0;
  55. var _a = this, arrayLike = _a.arrayLike, scheduler = _a.scheduler;
  56. var length = arrayLike.length;
  57. if (scheduler) {
  58. return scheduler.schedule(ArrayLikeObservable.dispatch, 0, {
  59. arrayLike: arrayLike, index: index, length: length, subscriber: subscriber
  60. });
  61. }
  62. else {
  63. for (var i = 0; i < length && !subscriber.closed; i++) {
  64. subscriber.next(arrayLike[i]);
  65. }
  66. subscriber.complete();
  67. }
  68. };
  69. return ArrayLikeObservable;
  70. }(Observable));
  71. //# sourceMappingURL=ArrayLikeObservable.js.map