New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(groupBy): Support named arguments, support ObservableInputs for duration selector #5679
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
f7db0dd
feat(groupBy): Support named arguments, support ObservableInputs for …
benlesh 9c26c40
chore: rebased. There are still type issues
benlesh 2ac05da
refactor: change to fn, options pattern
benlesh c376360
chore: remove duplicate typing
benlesh File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,51 @@ | ||
import { Observable } from '../Observable'; | ||
import { innerFrom } from '../observable/from'; | ||
import { Subject } from '../Subject'; | ||
import { Observer, OperatorFunction } from '../types'; | ||
import { ObservableInput, Observer, OperatorFunction, SubjectLike } from '../types'; | ||
import { operate } from '../util/lift'; | ||
import { OperatorSubscriber } from './OperatorSubscriber'; | ||
|
||
interface BasicGroupByOptions<K, T> { | ||
element?: undefined; | ||
duration?: (grouped: GroupedObservable<K, T>) => ObservableInput<any>; | ||
connector?: () => SubjectLike<T>; | ||
} | ||
|
||
interface GroupByOptionsWithElement<K, E, T> { | ||
element: (value: T) => E; | ||
duration?: (grouped: GroupedObservable<K, E>) => ObservableInput<any>; | ||
connector?: () => SubjectLike<E>; | ||
} | ||
|
||
export function groupBy<T, K>(key: (value: T) => K, options: BasicGroupByOptions<K, T>): OperatorFunction<T, GroupedObservable<K, T>>; | ||
|
||
export function groupBy<T, K, E>( | ||
key: (value: T) => K, | ||
options: GroupByOptionsWithElement<K, E, T> | ||
): OperatorFunction<T, GroupedObservable<K, E>>; | ||
|
||
export function groupBy<T, K extends T>( | ||
keySelector: (value: T) => value is K | ||
key: (value: T) => value is K | ||
): OperatorFunction<T, GroupedObservable<true, K> | GroupedObservable<false, Exclude<T, K>>>; | ||
export function groupBy<T, K>(keySelector: (value: T) => K): OperatorFunction<T, GroupedObservable<K, T>>; | ||
|
||
export function groupBy<T, K>(key: (value: T) => K): OperatorFunction<T, GroupedObservable<K, T>>; | ||
|
||
/** | ||
* @deprecated use the options parameter instead. | ||
*/ | ||
export function groupBy<T, K>( | ||
keySelector: (value: T) => K, | ||
elementSelector: void, | ||
durationSelector: (grouped: GroupedObservable<K, T>) => Observable<any> | ||
key: (value: T) => K, | ||
element: void, | ||
duration: (grouped: GroupedObservable<K, T>) => Observable<any> | ||
): OperatorFunction<T, GroupedObservable<K, T>>; | ||
|
||
/** | ||
* @deprecated use the options parameter instead. | ||
*/ | ||
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> | ||
key: (value: T) => K, | ||
element?: (value: T) => R, | ||
duration?: (grouped: GroupedObservable<K, R>) => Observable<any> | ||
): OperatorFunction<T, GroupedObservable<K, R>>; | ||
|
||
/** | ||
|
@@ -32,7 +55,7 @@ export function groupBy<T, K, R>( | |
* | ||
* ![](groupBy.png) | ||
* | ||
* When the Observable emits an item, a key is computed for this item with the keySelector function. | ||
* When the Observable emits an item, a key is computed for this item with the key function. | ||
* | ||
* If a {@link GroupedObservable} for this key exists, this {@link GroupedObservable} emits. Otherwise, a new | ||
* {@link GroupedObservable} for this key is created and emits. | ||
|
@@ -41,7 +64,7 @@ export function groupBy<T, K, R>( | |
* 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. | ||
* returned by the element function. | ||
* | ||
* ## Examples | ||
* | ||
|
@@ -101,28 +124,45 @@ export function groupBy<T, K, R>( | |
* // { id: 3, values: [ 'TSLint' ] } | ||
* ``` | ||
* | ||
* @param {function(value: T): K} keySelector A function that extracts the key | ||
* @param key A function that extracts the key | ||
* for each item. | ||
* @param {function(value: T): R} [elementSelector] A function that extracts the | ||
* @param element A function that extracts the | ||
* return element for each item. | ||
* @param {function(grouped: GroupedObservable<K,R>): Observable<any>} [durationSelector] | ||
* @param duration | ||
* A function that returns an Observable to determine how long each group should | ||
* exist. | ||
* @param {function(): Subject<R>} [subjectSelector] Factory function to create an | ||
* @param connector Factory function to create an | ||
* intermediate Subject through which grouped elements are emitted. | ||
* @return A function that returns 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. | ||
* | ||
* @deprecated Use the options parameter instead. | ||
*/ | ||
export function groupBy<T, K, R>( | ||
key: (value: T) => K, | ||
element?: (value: T) => R, | ||
duration?: (grouped: GroupedObservable<K, R>) => Observable<any>, | ||
connector?: () => Subject<R> | ||
): OperatorFunction<T, GroupedObservable<K, R>>; | ||
|
||
// Impl | ||
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> | ||
elementOrOptions?: ((value: any) => any) | void | BasicGroupByOptions<K, T> | GroupByOptionsWithElement<K, R, T>, | ||
duration?: (grouped: GroupedObservable<any, any>) => ObservableInput<any>, | ||
connector?: () => SubjectLike<any> | ||
): OperatorFunction<T, GroupedObservable<K, R>> { | ||
return operate((source, subscriber) => { | ||
let element: ((value: any) => any) | void; | ||
if (!elementOrOptions || typeof elementOrOptions === 'function') { | ||
element = elementOrOptions; | ||
} else { | ||
({ duration, element, connector } = elementOrOptions); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Destructuring assignment FTW. 🎉❤ |
||
} | ||
|
||
// A lookup for the groups that we have so far. | ||
const groups = new Map<K, Subject<any>>(); | ||
const groups = new Map<K, SubjectLike<any>>(); | ||
|
||
// Used for notifying all groups and the subscriber in the same way. | ||
const notify = (cb: (group: Observer<any>) => void) => { | ||
|
@@ -153,15 +193,15 @@ export function groupBy<T, K, R>( | |
let group = groups.get(key); | ||
if (!group) { | ||
// Create our group subject | ||
groups.set(key, (group = subjectSelector ? subjectSelector() : new Subject<any>())); | ||
groups.set(key, (group = connector ? connector() : new Subject<any>())); | ||
|
||
// Emit the grouped observable. Note that we can't do a simple `asObservable()` here, | ||
// because the grouped observable has special semantics around reference counting | ||
// to ensure we don't sever our connection to the source prematurely. | ||
const grouped = createGroupedObservable(key, group); | ||
subscriber.next(grouped); | ||
|
||
if (durationSelector) { | ||
if (duration) { | ||
const durationSubscriber = new OperatorSubscriber( | ||
// Providing the group here ensures that it is disposed of -- via `unsubscribe` -- | ||
// wnen the duration subscription is torn down. That is important, because then | ||
|
@@ -185,12 +225,12 @@ export function groupBy<T, K, R>( | |
); | ||
|
||
// Start our duration notifier. | ||
groupBySourceSubscriber.add(durationSelector(grouped).subscribe(durationSubscriber)); | ||
groupBySourceSubscriber.add(innerFrom(duration(grouped)).subscribe(durationSubscriber)); | ||
} | ||
} | ||
|
||
// Send the value to our group. | ||
group.next(elementSelector ? elementSelector(value) : value); | ||
group.next(element ? element(value) : value); | ||
} catch (err) { | ||
handleError(err); | ||
} | ||
|
@@ -214,7 +254,7 @@ export function groupBy<T, K, R>( | |
* @param key The key of the group | ||
* @param groupSubject The subject that fuels the group | ||
*/ | ||
function createGroupedObservable(key: K, groupSubject: Subject<any>) { | ||
function createGroupedObservable(key: K, groupSubject: SubjectLike<any>) { | ||
const result: any = new Observable<T>((groupSubscriber) => { | ||
groupBySourceSubscriber.activeGroups++; | ||
const innerSub = groupSubject.subscribe(groupSubscriber); | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can see that the element selector isn't really necessary based on the test's description, but with the removal of the element selectors there are now no tests that specify them. Maybe we should add one?