source: trip-planner-front/node_modules/rxjs/src/internal/observable/pairs.ts@ 8d391a1

Last change on this file since 8d391a1 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

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