import { Subscriber } from '../Subscriber'; import { Subscription } from '../Subscription'; import { Observable } from '../Observable'; import { Operator } from '../Operator'; import { Subject } from '../Subject'; import { OperatorFunction } from '../types'; /* tslint:disable:max-line-length */ export function groupBy<T, K>(keySelector: (value: T) => K): OperatorFunction<T, GroupedObservable<K, T>>; export function groupBy<T, K>(keySelector: (value: T) => K, elementSelector: void, durationSelector: (grouped: GroupedObservable<K, T>) => Observable<any>): OperatorFunction<T, GroupedObservable<K, T>>; export function groupBy<T, K, R>(keySelector: (value: T) => K, elementSelector?: (value: T) => R, durationSelector?: (grouped: GroupedObservable<K, R>) => Observable<any>): OperatorFunction<T, GroupedObservable<K, R>>; export function groupBy<T, K, R>(keySelector: (value: T) => K, elementSelector?: (value: T) => R, durationSelector?: (grouped: GroupedObservable<K, R>) => Observable<any>, subjectSelector?: () => Subject<R>): OperatorFunction<T, GroupedObservable<K, R>>; /* tslint:enable:max-line-length */ /** * Groups the items emitted by an Observable according to a specified criterion, * and emits these grouped items as `GroupedObservables`, one * {@link GroupedObservable} per group. * *  * * When the Observable emits an item, a key is computed for this item with the keySelector function. * * If a {@link GroupedObservable} for this key exists, this {@link GroupedObservable} emits. Elsewhere, a new * {@link GroupedObservable} for this key is created and emits. * * A {@link GroupedObservable} represents values belonging to the same group represented by a common key. The common * key is available as the key field of a {@link GroupedObservable} instance. * * The elements emitted by {@link GroupedObservable}s are by default the items emitted by the Observable, or elements * returned by the elementSelector function. * * ## Examples * * ### Group objects by id and return as array * * ```ts * import { of } from 'rxjs'; * import { mergeMap, groupBy, reduce } from 'rxjs/operators'; * * of( * {id: 1, name: 'JavaScript'}, * {id: 2, name: 'Parcel'}, * {id: 2, name: 'webpack'}, * {id: 1, name: 'TypeScript'}, * {id: 3, name: 'TSLint'} * ).pipe( * groupBy(p => p.id), * mergeMap((group$) => group$.pipe(reduce((acc, cur) => [...acc, cur], []))), * ) * .subscribe(p => console.log(p)); * * // displays: * // [ { id: 1, name: 'JavaScript'}, * // { id: 1, name: 'TypeScript'} ] * // * // [ { id: 2, name: 'Parcel'}, * // { id: 2, name: 'webpack'} ] * // * // [ { id: 3, name: 'TSLint'} ] * ``` * * ### Pivot data on the id field * * ```ts * import { of } from 'rxjs'; * import { groupBy, map, mergeMap, reduce } from 'rxjs/operators'; * * of( * { id: 1, name: 'JavaScript' }, * { id: 2, name: 'Parcel' }, * { id: 2, name: 'webpack' }, * { id: 1, name: 'TypeScript' }, * { id: 3, name: 'TSLint' } * ) * .pipe( * groupBy(p => p.id, p => p.name), * mergeMap(group$ => * group$.pipe(reduce((acc, cur) => [...acc, cur], [`${group$.key}`])) * ), * map(arr => ({ id: parseInt(arr[0], 10), values: arr.slice(1) })) * ) * .subscribe(p => console.log(p)); * * // displays: * // { id: 1, values: [ 'JavaScript', 'TypeScript' ] } * // { id: 2, values: [ 'Parcel', 'webpack' ] } * // { id: 3, values: [ 'TSLint' ] } * ``` * * @param {function(value: T): K} keySelector A function that extracts the key * for each item. * @param {function(value: T): R} [elementSelector] A function that extracts the * return element for each item. * @param {function(grouped: GroupedObservable<K,R>): Observable<any>} [durationSelector] * A function that returns an Observable to determine how long each group should * exist. * @return {Observable<GroupedObservable<K,R>>} An Observable that emits * GroupedObservables, each of which corresponds to a unique key value and each * of which emits those items from the source Observable that share that key * value. * @method groupBy * @owner Observable */ export function groupBy<T, K, R>(keySelector: (value: T) => K, elementSelector?: ((value: T) => R) | void, durationSelector?: (grouped: GroupedObservable<K, R>) => Observable<any>, subjectSelector?: () => Subject<R>): OperatorFunction<T, GroupedObservable<K, R>> { return (source: Observable<T>) => source.lift(new GroupByOperator(keySelector, elementSelector, durationSelector, subjectSelector)); } export interface RefCountSubscription { count: number; unsubscribe: () => void; closed: boolean; attemptedToUnsubscribe: boolean; } class GroupByOperator<T, K, R> implements Operator<T, GroupedObservable<K, R>> { constructor(private keySelector: (value: T) => K, private elementSelector?: ((value: T) => R) | void, private durationSelector?: (grouped: GroupedObservable<K, R>) => Observable<any>, private subjectSelector?: () => Subject<R>) { } call(subscriber: Subscriber<GroupedObservable<K, R>>, source: any): any { return source.subscribe(new GroupBySubscriber( subscriber, this.keySelector, this.elementSelector, this.durationSelector, this.subjectSelector )); } } /** * We need this JSDoc comment for affecting ESDoc. * @ignore * @extends {Ignored} */ class GroupBySubscriber<T, K, R> extends Subscriber<T> implements RefCountSubscription { private groups: Map<K, Subject<T | R>> = null; public attemptedToUnsubscribe: boolean = false; public count: number = 0; constructor(destination: Subscriber<GroupedObservable<K, R>>, private keySelector: (value: T) => K, private elementSelector?: ((value: T) => R) | void, private durationSelector?: (grouped: GroupedObservable<K, R>) => Observable<any>, private subjectSelector?: () => Subject<R>) { super(destination); } protected _next(value: T): void { let key: K; try { key = this.keySelector(value); } catch (err) { this.error(err); return; } this._group(value, key); } private _group(value: T, key: K) { let groups = this.groups; if (!groups) { groups = this.groups = new Map<K, Subject<T | R>>(); } let group = groups.get(key); let element: R; if (this.elementSelector) { try { element = this.elementSelector(value); } catch (err) { this.error(err); } } else { element = <any>value; } if (!group) { group = (this.subjectSelector ? this.subjectSelector() : new Subject<R>()) as Subject<T | R>; groups.set(key, group); const groupedObservable = new GroupedObservable(key, group, this); this.destination.next(groupedObservable); if (this.durationSelector) { let duration: any; try { duration = this.durationSelector(new GroupedObservable<K, R>(key, <Subject<R>>group)); } catch (err) { this.error(err); return; } this.add(duration.subscribe(new GroupDurationSubscriber(key, group, this))); } } if (!group.closed) { group.next(element); } } protected _error(err: any): void { const groups = this.groups; if (groups) { groups.forEach((group, key) => { group.error(err); }); groups.clear(); } this.destination.error(err); } protected _complete(): void { const groups = this.groups; if (groups) { groups.forEach((group, key) => { group.complete(); }); groups.clear(); } this.destination.complete(); } removeGroup(key: K): void { this.groups.delete(key); } unsubscribe() { if (!this.closed) { this.attemptedToUnsubscribe = true; if (this.count === 0) { super.unsubscribe(); } } } } /** * We need this JSDoc comment for affecting ESDoc. * @ignore * @extends {Ignored} */ class GroupDurationSubscriber<K, T> extends Subscriber<T> { constructor(private key: K, private group: Subject<T>, private parent: GroupBySubscriber<any, K, T | any>) { super(group); } protected _next(value: T): void { this.complete(); } /** @deprecated This is an internal implementation detail, do not use. */ _unsubscribe() { const { parent, key } = this; this.key = this.parent = null; if (parent) { parent.removeGroup(key); } } } /** * An Observable representing values belonging to the same group represented by * a common key. The values emitted by a GroupedObservable come from the source * Observable. The common key is available as the field `key` on a * GroupedObservable instance. * * @class GroupedObservable<K, T> */ export class GroupedObservable<K, T> extends Observable<T> { /** @deprecated Do not construct this type. Internal use only */ constructor(public key: K, private groupSubject: Subject<T>, private refCountSubscription?: RefCountSubscription) { super(); } /** @deprecated This is an internal implementation detail, do not use. */ _subscribe(subscriber: Subscriber<T>) { const subscription = new Subscription(); const { refCountSubscription, groupSubject } = this; if (refCountSubscription && !refCountSubscription.closed) { subscription.add(new InnerRefCountSubscription(refCountSubscription)); } subscription.add(groupSubject.subscribe(subscriber)); return subscription; } } /** * We need this JSDoc comment for affecting ESDoc. * @ignore * @extends {Ignored} */ class InnerRefCountSubscription extends Subscription { constructor(private parent: RefCountSubscription) { super(); parent.count++; } unsubscribe() { const parent = this.parent; if (!parent.closed && !this.closed) { super.unsubscribe(); parent.count -= 1; if (parent.count === 0 && parent.attemptedToUnsubscribe) { parent.unsubscribe(); } } } }
Name | Type | Size | Permission | Actions |
---|---|---|---|---|
audit.ts | File | 4.14 KB | 0644 |
|
auditTime.ts | File | 2.38 KB | 0644 |
|
buffer.ts | File | 2.54 KB | 0644 |
|
bufferCount.ts | File | 4.46 KB | 0644 |
|
bufferTime.ts | File | 8.51 KB | 0644 |
|
bufferToggle.ts | File | 5.6 KB | 0644 |
|
bufferWhen.ts | File | 3.89 KB | 0644 |
|
catchError.ts | File | 4.72 KB | 0644 |
|
combineAll.ts | File | 2.44 KB | 0644 |
|
combineLatest.ts | File | 4.32 KB | 0644 |
|
concat.ts | File | 2.18 KB | 0644 |
|
concatAll.ts | File | 2.26 KB | 0644 |
|
concatMap.ts | File | 3.4 KB | 0644 |
|
concatMapTo.ts | File | 3.05 KB | 0644 |
|
count.ts | File | 3.78 KB | 0644 |
|
debounce.ts | File | 4.88 KB | 0644 |
|
debounceTime.ts | File | 4.54 KB | 0644 |
|
defaultIfEmpty.ts | File | 2.69 KB | 0644 |
|
delay.ts | File | 5.09 KB | 0644 |
|
delayWhen.ts | File | 7.7 KB | 0644 |
|
dematerialize.ts | File | 2.58 KB | 0644 |
|
distinct.ts | File | 4.22 KB | 0644 |
|
distinctUntilChanged.ts | File | 3.76 KB | 0644 |
|
distinctUntilKeyChanged.ts | File | 2.79 KB | 0644 |
|
elementAt.ts | File | 2.56 KB | 0644 |
|
endWith.ts | File | 4.06 KB | 0644 |
|
every.ts | File | 2.54 KB | 0644 |
|
exhaust.ts | File | 3.2 KB | 0644 |
|
exhaustMap.ts | File | 5.64 KB | 0644 |
|
expand.ts | File | 6.18 KB | 0644 |
|
filter.ts | File | 3.69 KB | 0644 |
|
finalize.ts | File | 1.3 KB | 0644 |
|
find.ts | File | 3.81 KB | 0644 |
|
findIndex.ts | File | 1.86 KB | 0644 |
|
first.ts | File | 3.36 KB | 0644 |
|
groupBy.ts | File | 9.95 KB | 0644 |
|
ignoreElements.ts | File | 1.49 KB | 0644 |
|
index.ts | File | 4.02 KB | 0644 |
|
isEmpty.ts | File | 2.67 KB | 0644 |
|
last.ts | File | 2.36 KB | 0644 |
|
map.ts | File | 3.04 KB | 0644 |
|
mapTo.ts | File | 1.9 KB | 0644 |
|
materialize.ts | File | 3.25 KB | 0644 |
|
max.ts | File | 1.54 KB | 0644 |
|
merge.ts | File | 3.59 KB | 0644 |
|
mergeAll.ts | File | 2.43 KB | 0644 |
|
mergeMap.ts | File | 6.28 KB | 0644 |
|
mergeMapTo.ts | File | 2.56 KB | 0644 |
|
mergeScan.ts | File | 4.65 KB | 0644 |
|
min.ts | File | 1.54 KB | 0644 |
|
multicast.ts | File | 3.46 KB | 0644 |
|
observeOn.ts | File | 5.17 KB | 0644 |
|
onErrorResumeNext.ts | File | 8.18 KB | 0644 |
|
pairwise.ts | File | 2.43 KB | 0644 |
|
partition.ts | File | 2.68 KB | 0644 |
|
pluck.ts | File | 3.07 KB | 0644 |
|
publish.ts | File | 2.51 KB | 0644 |
|
publishBehavior.ts | File | 589 B | 0644 |
|
publishLast.ts | File | 2.06 KB | 0644 |
|
publishReplay.ts | File | 1.47 KB | 0644 |
|
race.ts | File | 1.85 KB | 0644 |
|
reduce.ts | File | 3.71 KB | 0644 |
|
refCount.ts | File | 5 KB | 0644 |
|
repeat.ts | File | 3.03 KB | 0644 |
|
repeatWhen.ts | File | 4.15 KB | 0644 |
|
retry.ts | File | 2.74 KB | 0644 |
|
retryWhen.ts | File | 3.45 KB | 0644 |
|
sample.ts | File | 2.78 KB | 0644 |
|
sampleTime.ts | File | 3.18 KB | 0644 |
|
scan.ts | File | 4.13 KB | 0644 |
|
sequenceEqual.ts | File | 5.16 KB | 0644 |
|
share.ts | File | 1.02 KB | 0644 |
|
shareReplay.ts | File | 4.19 KB | 0644 |
|
single.ts | File | 3.79 KB | 0644 |
|
skip.ts | File | 1.27 KB | 0644 |
|
skipLast.ts | File | 2.81 KB | 0644 |
|
skipUntil.ts | File | 3.81 KB | 0644 |
|
skipWhile.ts | File | 1.95 KB | 0644 |
|
startWith.ts | File | 4.35 KB | 0644 |
|
subscribeOn.ts | File | 2.56 KB | 0644 |
|
switchAll.ts | File | 1.99 KB | 0644 |
|
switchMap.ts | File | 6.04 KB | 0644 |
|
switchMapTo.ts | File | 2.56 KB | 0644 |
|
take.ts | File | 2.74 KB | 0644 |
|
takeLast.ts | File | 3.44 KB | 0644 |
|
takeUntil.ts | File | 2.79 KB | 0644 |
|
takeWhile.ts | File | 3.87 KB | 0644 |
|
tap.ts | File | 5.33 KB | 0644 |
|
throttle.ts | File | 4.83 KB | 0644 |
|
throttleTime.ts | File | 5.83 KB | 0644 |
|
throwIfEmpty.ts | File | 2.19 KB | 0644 |
|
timeInterval.ts | File | 2.49 KB | 0644 |
|
timeout.ts | File | 4.07 KB | 0644 |
|
timeoutWith.ts | File | 6.13 KB | 0644 |
|
timestamp.ts | File | 1.67 KB | 0644 |
|
toArray.ts | File | 1.12 KB | 0644 |
|
window.ts | File | 3.72 KB | 0644 |
|
windowCount.ts | File | 4.82 KB | 0644 |
|
windowTime.ts | File | 9.9 KB | 0644 |
|
windowToggle.ts | File | 6.2 KB | 0644 |
|
windowWhen.ts | File | 4.57 KB | 0644 |
|
withLatestFrom.ts | File | 7.72 KB | 0644 |
|
zip.ts | File | 3.35 KB | 0644 |
|
zipAll.ts | File | 653 B | 0644 |
|