Skip to content
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

Closed
anthonyrota opened this issue Jan 7, 2024 · 1 comment
Closed

every operator sending multiple values when re-entrant #7425

anthonyrota opened this issue Jan 7, 2024 · 1 comment
Assignees

Comments

@anthonyrota
Copy link

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 the complete is sent.

Related: #1967 #2100

  next: (value) => {
    if (!predicate.call(thisArg, value, index++, source)) {
      destination.next(false); # If inside this function call, the source sends a new value that the predicate rejects, two or more false's can be sent.
      destination.complete();
    }
  },

Also, it can send next(false) then next(true) then complete if re-entrant complete within the next 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

import { Subject } from 'rxjs';
import { every } from 'rxjs/operators';
{
  console.log('scen 1')
  const subj = new Subject();
  let i = 0;
  subj.pipe(every(() => false)).subscribe({
    next: (value) => {
      console.log('next', value, i);
      if (++i < 5) {
        subj.next(undefined);
      }
    },
    complete: () => {
      console.log('end');
    },
  });
  subj.next(undefined)
}
{
  console.log('scen 2')
  const subj = new Subject();
  subj.pipe(every(() => false)).subscribe({
    next: (value) => {
      console.log('next', value);
      subj.complete();
    },
    complete: () => {
      console.log('end');
    },
  });
  subj.next(undefined)
}

Reproduction URL

https://stackblitz.com/edit/rxjs-22nsn5?file=index.ts

Version

8.0.0-alpha.12

Environment

No response

Additional context

No response

@josepot
Copy link
Contributor

josepot commented Jan 7, 2024

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;
    });

Example

props to @voliva for helping me out 😉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants