import { Observable } from '../Observable'; import { SchedulerAction, SchedulerLike } from '../types'; import { Subscriber } from '../Subscriber'; import { Subscription } from '../Subscription'; /** * Convert an object into an Observable of `[key, value]` pairs. * * <span class="informal">Turn entries of an object into a stream.</span> * * <img src="./img/pairs.png" width="100%"> * * `pairs` takes an arbitrary object and returns an Observable that emits arrays. Each * emitted array has exactly two elements - the first is a key from the object * and the second is a value corresponding to that key. Keys are extracted from * an object via `Object.keys` function, which means that they will be only * enumerable keys that are present on an object directly - not ones inherited * via prototype chain. * * By default these arrays are emitted synchronously. To change that you can * pass a {@link SchedulerLike} as a second argument to `pairs`. * * @example <caption>Converts a javascript object to an Observable</caption> * ```ts * import { pairs } from 'rxjs'; * * const obj = { * foo: 42, * bar: 56, * baz: 78 * }; * * pairs(obj) * .subscribe( * value => console.log(value), * err => {}, * () => console.log('the end!') * ); * * // Logs: * // ["foo", 42], * // ["bar", 56], * // ["baz", 78], * // "the end!" * ``` * * @param {Object} obj The object to inspect and turn into an * Observable sequence. * @param {Scheduler} [scheduler] An optional IScheduler to schedule * when resulting Observable will emit values. * @returns {(Observable<Array<string|T>>)} An observable sequence of * [key, value] pairs from the object. */ export function pairs<T>(obj: Object, scheduler?: SchedulerLike): Observable<[string, T]> { if (!scheduler) { return new Observable<[string, T]>(subscriber => { const keys = Object.keys(obj); for (let i = 0; i < keys.length && !subscriber.closed; i++) { const key = keys[i]; if (obj.hasOwnProperty(key)) { subscriber.next([key, obj[key]]); } } subscriber.complete(); }); } else { return new Observable<[string, T]>(subscriber => { const keys = Object.keys(obj); const subscription = new Subscription(); subscription.add( scheduler.schedule<{ keys: string[], index: number, subscriber: Subscriber<[string, T]>, subscription: Subscription, obj: Object }> (dispatch, 0, { keys, index: 0, subscriber, subscription, obj })); return subscription; }); } } /** @internal */ export function dispatch<T>(this: SchedulerAction<any>, state: { keys: string[], index: number, subscriber: Subscriber<[string, T]>, subscription: Subscription, obj: Object }) { const { keys, index, subscriber, subscription, obj } = state; if (!subscriber.closed) { if (index < keys.length) { const key = keys[index]; subscriber.next([key, obj[key]]); subscription.add(this.schedule({ keys, index: index + 1, subscriber, subscription, obj })); } else { subscriber.complete(); } } }
Name | Type | Size | Permission | Actions |
---|---|---|---|---|
dom | Folder | 0755 |
|
|
ConnectableObservable.ts | File | 5.42 KB | 0644 |
|
SubscribeOnObservable.ts | File | 1.59 KB | 0644 |
|
bindCallback.ts | File | 15.9 KB | 0644 |
|
bindNodeCallback.ts | File | 15.41 KB | 0644 |
|
combineLatest.ts | File | 21.95 KB | 0644 |
|
concat.ts | File | 9.43 KB | 0644 |
|
defer.ts | File | 2.53 KB | 0644 |
|
empty.ts | File | 2.21 KB | 0644 |
|
forkJoin.ts | File | 9.49 KB | 0644 |
|
from.ts | File | 3.06 KB | 0644 |
|
fromArray.ts | File | 423 B | 0644 |
|
fromEvent.ts | File | 10.38 KB | 0644 |
|
fromEventPattern.ts | File | 7.14 KB | 0644 |
|
fromIterable.ts | File | 511 B | 0644 |
|
fromObservable.ts | File | 485 B | 0644 |
|
fromPromise.ts | File | 439 B | 0644 |
|
generate.ts | File | 12.55 KB | 0644 |
|
iif.ts | File | 3.22 KB | 0644 |
|
interval.ts | File | 2.53 KB | 0644 |
|
merge.ts | File | 9.94 KB | 0644 |
|
never.ts | File | 1.16 KB | 0644 |
|
of.ts | File | 4.77 KB | 0644 |
|
onErrorResumeNext.ts | File | 3.83 KB | 0644 |
|
pairs.ts | File | 3.06 KB | 0644 |
|
partition.ts | File | 2.61 KB | 0644 |
|
race.ts | File | 5.03 KB | 0644 |
|
range.ts | File | 2.28 KB | 0644 |
|
throwError.ts | File | 2.2 KB | 0644 |
|
timer.ts | File | 3.32 KB | 0644 |
|
using.ts | File | 3.04 KB | 0644 |
|
zip.ts | File | 12.06 KB | 0644 |
|