123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257 |
- import { Subscriber } from '../Subscriber';
- import { Subscription } from '../Subscription';
- import { Observable } from '../Observable';
- import { Subject } from '../Subject';
- import { Map } from '../util/Map';
- import { FastMap } from '../util/FastMap';
- /* 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.
- *
- * <img src="./img/groupBy.png" width="100%">
- *
- * @example <caption>Group objects by id and return as array</caption>
- * Observable.of<Obj>({id: 1, name: 'aze1'},
- * {id: 2, name: 'sf2'},
- * {id: 2, name: 'dg2'},
- * {id: 1, name: 'erg1'},
- * {id: 1, name: 'df1'},
- * {id: 2, name: 'sfqfb2'},
- * {id: 3, name: 'qfs3'},
- * {id: 2, name: 'qsgqsfg2'}
- * )
- * .groupBy(p => p.id)
- * .flatMap( (group$) => group$.reduce((acc, cur) => [...acc, cur], []))
- * .subscribe(p => console.log(p));
- *
- * // displays:
- * // [ { id: 1, name: 'aze1' },
- * // { id: 1, name: 'erg1' },
- * // { id: 1, name: 'df1' } ]
- * //
- * // [ { id: 2, name: 'sf2' },
- * // { id: 2, name: 'dg2' },
- * // { id: 2, name: 'sfqfb2' },
- * // { id: 2, name: 'qsgqsfg2' } ]
- * //
- * // [ { id: 3, name: 'qfs3' } ]
- *
- * @example <caption>Pivot data on the id field</caption>
- * Observable.of<Obj>({id: 1, name: 'aze1'},
- * {id: 2, name: 'sf2'},
- * {id: 2, name: 'dg2'},
- * {id: 1, name: 'erg1'},
- * {id: 1, name: 'df1'},
- * {id: 2, name: 'sfqfb2'},
- * {id: 3, name: 'qfs1'},
- * {id: 2, name: 'qsgqsfg2'}
- * )
- * .groupBy(p => p.id, p => p.name)
- * .flatMap( (group$) => group$.reduce((acc, cur) => [...acc, cur], ["" + group$.key]))
- * .map(arr => ({'id': parseInt(arr[0]), 'values': arr.slice(1)}))
- * .subscribe(p => console.log(p));
- *
- * // displays:
- * // { id: 1, values: [ 'aze1', 'erg1', 'df1' ] }
- * // { id: 2, values: [ 'sf2', 'dg2', 'sfqfb2', 'qsgqsfg2' ] }
- * // { id: 3, values: [ 'qfs1' ] }
- *
- * @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(keySelector, elementSelector, durationSelector, subjectSelector) {
- return (source) => source.lift(new GroupByOperator(keySelector, elementSelector, durationSelector, subjectSelector));
- }
- class GroupByOperator {
- constructor(keySelector, elementSelector, durationSelector, subjectSelector) {
- this.keySelector = keySelector;
- this.elementSelector = elementSelector;
- this.durationSelector = durationSelector;
- this.subjectSelector = subjectSelector;
- }
- call(subscriber, source) {
- 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 extends Subscriber {
- constructor(destination, keySelector, elementSelector, durationSelector, subjectSelector) {
- super(destination);
- this.keySelector = keySelector;
- this.elementSelector = elementSelector;
- this.durationSelector = durationSelector;
- this.subjectSelector = subjectSelector;
- this.groups = null;
- this.attemptedToUnsubscribe = false;
- this.count = 0;
- }
- _next(value) {
- let key;
- try {
- key = this.keySelector(value);
- }
- catch (err) {
- this.error(err);
- return;
- }
- this._group(value, key);
- }
- _group(value, key) {
- let groups = this.groups;
- if (!groups) {
- groups = this.groups = typeof key === 'string' ? new FastMap() : new Map();
- }
- let group = groups.get(key);
- let element;
- if (this.elementSelector) {
- try {
- element = this.elementSelector(value);
- }
- catch (err) {
- this.error(err);
- }
- }
- else {
- element = value;
- }
- if (!group) {
- group = this.subjectSelector ? this.subjectSelector() : new Subject();
- groups.set(key, group);
- const groupedObservable = new GroupedObservable(key, group, this);
- this.destination.next(groupedObservable);
- if (this.durationSelector) {
- let duration;
- try {
- duration = this.durationSelector(new GroupedObservable(key, group));
- }
- catch (err) {
- this.error(err);
- return;
- }
- this.add(duration.subscribe(new GroupDurationSubscriber(key, group, this)));
- }
- }
- if (!group.closed) {
- group.next(element);
- }
- }
- _error(err) {
- const groups = this.groups;
- if (groups) {
- groups.forEach((group, key) => {
- group.error(err);
- });
- groups.clear();
- }
- this.destination.error(err);
- }
- _complete() {
- const groups = this.groups;
- if (groups) {
- groups.forEach((group, key) => {
- group.complete();
- });
- groups.clear();
- }
- this.destination.complete();
- }
- removeGroup(key) {
- 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 extends Subscriber {
- constructor(key, group, parent) {
- super(group);
- this.key = key;
- this.group = group;
- this.parent = parent;
- }
- _next(value) {
- this.complete();
- }
- /** @deprecated internal use only */ _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 extends Observable {
- constructor(key, groupSubject, refCountSubscription) {
- super();
- this.key = key;
- this.groupSubject = groupSubject;
- this.refCountSubscription = refCountSubscription;
- }
- /** @deprecated internal use only */ _subscribe(subscriber) {
- 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(parent) {
- super();
- this.parent = parent;
- 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();
- }
- }
- }
- }
- //# sourceMappingURL=groupBy.js.map
|