PairsObservable.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /** PURE_IMPORTS_START .._Observable 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. function dispatch(state) {
  11. var obj = state.obj, keys = state.keys, length = state.length, index = state.index, subscriber = state.subscriber;
  12. if (index === length) {
  13. subscriber.complete();
  14. return;
  15. }
  16. var key = keys[index];
  17. subscriber.next([key, obj[key]]);
  18. state.index = index + 1;
  19. this.schedule(state);
  20. }
  21. /**
  22. * We need this JSDoc comment for affecting ESDoc.
  23. * @extends {Ignored}
  24. * @hide true
  25. */
  26. export var PairsObservable = /*@__PURE__*/ (/*@__PURE__*/ function (_super) {
  27. __extends(PairsObservable, _super);
  28. function PairsObservable(obj, scheduler) {
  29. _super.call(this);
  30. this.obj = obj;
  31. this.scheduler = scheduler;
  32. this.keys = Object.keys(obj);
  33. }
  34. /**
  35. * Convert an object into an observable sequence of [key, value] pairs
  36. * using an optional IScheduler to enumerate the object.
  37. *
  38. * @example <caption>Converts a javascript object to an Observable</caption>
  39. * var obj = {
  40. * foo: 42,
  41. * bar: 56,
  42. * baz: 78
  43. * };
  44. *
  45. * var source = Rx.Observable.pairs(obj);
  46. *
  47. * var subscription = source.subscribe(
  48. * function (x) {
  49. * console.log('Next: %s', x);
  50. * },
  51. * function (err) {
  52. * console.log('Error: %s', err);
  53. * },
  54. * function () {
  55. * console.log('Completed');
  56. * });
  57. *
  58. * @param {Object} obj The object to inspect and turn into an
  59. * Observable sequence.
  60. * @param {Scheduler} [scheduler] An optional IScheduler to run the
  61. * enumeration of the input sequence on.
  62. * @returns {(Observable<Array<string | T>>)} An observable sequence of
  63. * [key, value] pairs from the object.
  64. */
  65. PairsObservable.create = function (obj, scheduler) {
  66. return new PairsObservable(obj, scheduler);
  67. };
  68. /** @deprecated internal use only */ PairsObservable.prototype._subscribe = function (subscriber) {
  69. var _a = this, keys = _a.keys, scheduler = _a.scheduler;
  70. var length = keys.length;
  71. if (scheduler) {
  72. return scheduler.schedule(dispatch, 0, {
  73. obj: this.obj, keys: keys, length: length, index: 0, subscriber: subscriber
  74. });
  75. }
  76. else {
  77. for (var idx = 0; idx < length; idx++) {
  78. var key = keys[idx];
  79. subscriber.next([key, this.obj[key]]);
  80. }
  81. subscriber.complete();
  82. }
  83. };
  84. return PairsObservable;
  85. }(Observable));
  86. //# sourceMappingURL=PairsObservable.js.map