Suggestion: overload `useSubscription` to add support for observer objects
OliverJAsh opened this issue · 7 comments
The subscribe
function of an Observable
can receive an observer object:
const observer = { next: myNext, error: myError, complete: myComplete }
ob$.subscribe(observer)
It would be great if useSubscription
allowed the same, so we can do this:
useSubscription(ob$, observer)
… instead of this:
useSubscription(ob$, observer.next, observer.error, observer.complete)
Hi, we purposely did not support the observer object for performance reason. It would create less garbage and easier to handle with the current pattern.
Do you have a real world use case that would benefit from this pattern?
we purposely did not support the observer object for performance reason. It would create less garbage and easier to handle with the current pattern.
This seems like micro performance to me. I feel like it would be better to be consistent the way subscribe
works in RxJS, and then let users decide which overload they would like to use.
Side note: the next version of RxJS will deprecate the subscribe
overload which accepts multiple functions. https://docs.google.com/presentation/d/1pqbojTcQGUiQ6b-aA7bGl2rmboChsZQnR-LZ1cDpkT4/edit#slide=id.g82d9b6b050_0_129.
Do you have a real world use case that would benefit from this pattern?
Yes, we have a hook that creates an observer:
export type DataFetchingActionsObserver = Rx.PartialObserver<Array<OldOrNewAction>>;
export const createDataFetchingActionsObserver = ({
dispatch,
history,
}: {
dispatch: Dispatch;
history: RouterHistory;
}): DataFetchingActionsObserver => ({
next: batchDispatch(dispatch),
error: handleDataFetchingError(dispatch, history),
});
export const useDataFetchingActionsObserver = () => {
const dispatch = useDispatch();
const history = useHistory();
return createDataFetchingActionsObserver({ dispatch, history });
};
I would like to use this observer with the useSubscription
hook.
You are right. We should add this and let user decide.
I'll raise a PR.
Thanks for the fantastic lib btw. Migrating the Unsplash app over from rxjs-hooks, this is much more flexible.
Thanks for trying observable-hooks and the PR! Let me know if there is any blocker.
Hi @OliverJAsh are you still working on this one?
Hey I haven't had the chance sorry, I still really want it though!