Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

pairs.ts 3.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import { Observable } from '../Observable';
  2. import { SchedulerAction, SchedulerLike } from '../types';
  3. import { Subscriber } from '../Subscriber';
  4. import { Subscription } from '../Subscription';
  5. /**
  6. * Convert an object into an Observable of `[key, value]` pairs.
  7. *
  8. * <span class="informal">Turn entries of an object into a stream.</span>
  9. *
  10. * <img src="./img/pairs.png" width="100%">
  11. *
  12. * `pairs` takes an arbitrary object and returns an Observable that emits arrays. Each
  13. * emitted array has exactly two elements - the first is a key from the object
  14. * and the second is a value corresponding to that key. Keys are extracted from
  15. * an object via `Object.keys` function, which means that they will be only
  16. * enumerable keys that are present on an object directly - not ones inherited
  17. * via prototype chain.
  18. *
  19. * By default these arrays are emitted synchronously. To change that you can
  20. * pass a {@link SchedulerLike} as a second argument to `pairs`.
  21. *
  22. * @example <caption>Converts a javascript object to an Observable</caption>
  23. * ```ts
  24. * import { pairs } from 'rxjs';
  25. *
  26. * const obj = {
  27. * foo: 42,
  28. * bar: 56,
  29. * baz: 78
  30. * };
  31. *
  32. * pairs(obj)
  33. * .subscribe(
  34. * value => console.log(value),
  35. * err => {},
  36. * () => console.log('the end!')
  37. * );
  38. *
  39. * // Logs:
  40. * // ["foo", 42],
  41. * // ["bar", 56],
  42. * // ["baz", 78],
  43. * // "the end!"
  44. * ```
  45. *
  46. * @param {Object} obj The object to inspect and turn into an
  47. * Observable sequence.
  48. * @param {Scheduler} [scheduler] An optional IScheduler to schedule
  49. * when resulting Observable will emit values.
  50. * @returns {(Observable<Array<string|T>>)} An observable sequence of
  51. * [key, value] pairs from the object.
  52. */
  53. export function pairs<T>(obj: Object, scheduler?: SchedulerLike): Observable<[string, T]> {
  54. if (!scheduler) {
  55. return new Observable<[string, T]>(subscriber => {
  56. const keys = Object.keys(obj);
  57. for (let i = 0; i < keys.length && !subscriber.closed; i++) {
  58. const key = keys[i];
  59. if (obj.hasOwnProperty(key)) {
  60. subscriber.next([key, obj[key]]);
  61. }
  62. }
  63. subscriber.complete();
  64. });
  65. } else {
  66. return new Observable<[string, T]>(subscriber => {
  67. const keys = Object.keys(obj);
  68. const subscription = new Subscription();
  69. subscription.add(
  70. scheduler.schedule<{ keys: string[], index: number, subscriber: Subscriber<[string, T]>, subscription: Subscription, obj: Object }>
  71. (dispatch, 0, { keys, index: 0, subscriber, subscription, obj }));
  72. return subscription;
  73. });
  74. }
  75. }
  76. /** @internal */
  77. export function dispatch<T>(this: SchedulerAction<any>,
  78. state: { keys: string[], index: number, subscriber: Subscriber<[string, T]>, subscription: Subscription, obj: Object }) {
  79. const { keys, index, subscriber, subscription, obj } = state;
  80. if (!subscriber.closed) {
  81. if (index < keys.length) {
  82. const key = keys[index];
  83. subscriber.next([key, obj[key]]);
  84. subscription.add(this.schedule({ keys, index: index + 1, subscriber, subscription, obj }));
  85. } else {
  86. subscriber.complete();
  87. }
  88. }
  89. }