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

Unexpected error when using useSubscription for a Subject #122

Closed
darkyzhou opened this issue Aug 15, 2023 · 1 comment
Closed

Unexpected error when using useSubscription for a Subject #122

darkyzhou opened this issue Aug 15, 2023 · 1 comment

Comments

@darkyzhou
Copy link

darkyzhou commented Aug 15, 2023

Minimal codes to reproduce:

export default function App() {
  const { current: someValue$ } = useRef(new Subject<number>());

  useSubscription(someObservable$, someValue$);

  // ...
}

Error:
截屏2023-08-15 11 28 53

Stackblitz URL: https://stackblitz.com/edit/stackblitz-starters-uzyyi8?devToolsHeight=33&file=src%2FApp.tsx

Suspicious cause in use-subscription-internal.ts:

const nextObserver =
  (argsRef.current[1] as PartialObserver<TInput>)?.next ||
  (argsRef.current[1] as ((value: TInput) => void) | null | undefined)
if (nextObserver) {
  // BUG: When argsRef.current[1].next presents, this way of calling the function
  // causes `this` to be missing which Subject's `next` relies heavily on.
  return nextObserver(value)
}

A possible fix, would love to open a PR for this:

const observer = argsRef.current[1];
if (observer && 'next' in observer && typeof observer.next === 'function') {
  return observer.next(value);
}

if (observer) {
  return observer(value);
}
@crimx crimx closed this as completed in b76d11e Aug 15, 2023
@crimx
Copy link
Owner

crimx commented Aug 15, 2023

observable-hooks@4.2.3 published.

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

2 participants