PairsObservable.js 2.8 KB

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