-
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
every
operator sending multiple values when re-entrant
#7425
Comments
This is clearly a bug, the observer should synchronously unsubscribed from the source as soon as it observes a value that doesn't meet the criteria. If you were using v7, then you could use the following custom implementation which doesn't suffer from this bug: const customEvery =
<T>(cb: (value: T, idx: number, source: Observable<T>) => boolean) =>
(source: Observable<T>): Observable<boolean> =>
new Observable((observer) => {
let index = 0;
const subscriber = new Subscriber<T>({
next(value: T) {
if (!cb(value, index++, source)) {
subscriber.unsubscribe();
observer.next(false);
observer.complete();
}
},
error(e: unknown) {
observer.error(e);
},
complete() {
observer.next(true);
observer.complete();
},
});
source.subscribe(subscriber);
return subscriber;
}); I can't help to wonder what the official way of building a custom operator like that is for v8 🤔 EDIT:I think that I found it. The new way of doing the equivalent in RxJS 8 would be: const every =
<T>(cb: (value: T, idx: number, source: Observable<T>) => boolean) =>
(source: Observable<T>): Observable<boolean> =>
new Observable((observer) => {
let index = 0;
const subscriber = operate<T, boolean>({
destination: observer,
next(value: T) {
if (!cb(value, index++, source)) {
subscriber.unsubscribe();
observer.next(false);
observer.complete();
}
},
error(e: unknown) {
observer.error(e);
},
complete() {
observer.next(true);
observer.complete();
},
});
source.subscribe(subscriber);
return subscriber;
}); Exampleprops to @voliva for helping me out 😉 |
Describe the bug
The every operator can send multiple false's when, in the Subscriber's next, the Observable it is operating on sends a new value into the operator, because the middle subscriber's
next
handler will be recursively called before thecomplete
is sent.Related: #1967 #2100
Also, it can send
next(false)
thennext(true)
thencomplete
if re-entrantcomplete
within thenext
commented.Expected behavior
The circumstances leading to this shouldn't really happen but if it does, maybe consider guarding against it so only a single false is pushed?
Reproduction code
Reproduction URL
https://stackblitz.com/edit/rxjs-22nsn5?file=index.ts
Version
8.0.0-alpha.12
Environment
No response
Additional context
No response
The text was updated successfully, but these errors were encountered: