-
Notifications
You must be signed in to change notification settings - Fork 3k
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): Adds subjectSelector argument to groupBy #2023
feat(groupBy): Adds subjectSelector argument to groupBy #2023
Conversation
Adds subjectSelector argument to groupBy to allow consumers to customize the behavior of the hot GroupedObservable.
This appears to be a non-breaking change. I think I can see the utility of this, but would you might adding some use cases in a comment on this PR so everyone can better understand this addition? Also, we'll need to update the documentation. Ideally with an additional example showing using this argument for something useful, I guess. cc/ @staltz @mattpodwysocki what are your thoughts on this addition? |
private elementSelector?: ((value: T) => R) | void, | ||
private durationSelector?: (grouped: GroupedObservable<K, R>) => Observable<any>) { | ||
private durationSelector?: (grouped: GroupedObservable<K, R>) => Observable<any>, | ||
private subjectSelector?: () => Subject<R>) { |
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'd rename this to subjectFactory
since it's not taking any input into this function
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.
Agreed
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.
Agreed
@Blesh it's an interesting choice here, although @trxcllnt can you give us a concrete use case with working code as to why this is a good change? |
Would it make sense to also add this for the |
@paulpdaniels yes, it's a slippery slope here once we allow you to specify the guts as it were of a given Observable of Observables. |
The only thing I don't like about this is that it makes groupBy a method with 5 arguments. But otherwise I'm favorable and can see its utility, albeit rare. |
@mattpodwysocki I admit, the use-cases for this are relatively rare, which is why I like the subjectFactory in the last arg position. The problem that spurred me to add this option was around multitouch gesture recognition: Observable
.fromEvent(domElement, "touchstart")
.groupBy(getIdentifier)
.take(2)
.map((starts) => starts
.mergeMap(start => Observable
.fromEvent(window, "touchmove")
.filter(isIdentifier(start.identifier))
.startWith(start)
)
)
.combineAll() By the time the second touchstart event fires, the first inner group has already fired, so the first Observable in the combineAll never emits. |
Thinking more on this... I feel like adding this leaks out the underlying abstraction. People don't need to know we use a Subject under the hood. I say this because it's possible that at some later point we decide to implement a groupBy that doesn't use a Subject. Perhaps for performance reasons. If we add this argument we can't really do that because we'll be stuck with using Subjects... or at least we can't do that without some crazy conditionals. |
@Blesh I see what you're saying, but since GroupedObservables are hot and multicast to multiple subscribers, how else could they be implemented? It seems any performance improvements that could be realized from a specialized "GroupedObservableSubject" probably can and should be applied to Subjects generally. One description of The only other solution I've found to this problem is adding Do you have an alternate solution to the problem of missing events from GroupedObservables, or is the autoConnect direction more what you're thinking? |
We decided to ship this at the meeting today. |
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Adds a
subjectSelector
argument togroupBy
, allowing consumers to customize the messaging semantics of the inner GroupedObservables.Sometimes it isn't possible to immediately subscribe to groupBy's inner GroupedObservables as they're emitted, and you need to replay some number of events between when the GroupedObservable was created and when you subscribe.
For example, one encounters this problem writing multitouch gestures. Each Gesture has a set of constraints to consider the gesture started, but then needs access to the historic events once those constraints have been satisfied.