Skip to content

Suggestion: use object for input instead of tuple #99

Open
@OliverJAsh

Description

@OliverJAsh
Contributor

When passing in many inputs, it's awkward to remember the indexes inside the input tuple when you're trying to read/destructure a specific input, e.g:

useObservable(
  input$ => {
    const dispatch$ = input$.pipe(
      map(input => input[5]),
      distinctUntilChanged(),
    );

    /* … */
  },
  200,
  [
    props.shouldDisableInfiniteScroll,
    props.isDoneFetching,
    props.fetchDataParams,
    props.fetchDataAndBuildActions,
    props.dispatch,
    props.history,
  ],
);

If the inputs were represented as an object instead, they would be much easier to destructure.

useObservable(
  input$ => {
    const dispatch$ = input$.pipe(
      map(({ dispatch }) => dispatch),
      distinctUntilChanged(),
    );

    /* … */
  },
  200,
  pick(
    props,
    "shouldDisableInfiniteScroll",
    "isDoneFetching",
    "fetchDataParams",
    "fetchDataAndBuildActions",
    "dispatch",
    "history",
  ),
);

Activity

OliverJAsh

OliverJAsh commented on Feb 5, 2020

@OliverJAsh
ContributorAuthor

Another idea: the inputs could be provided as separate observables. This way, users don't have to destructure the observable, nor do they have to worry about adding distinctUntilChanged:

useObservable(
  inputs => {
    const { dispatch$ } = inputs;

    /* … */
  },
  200,
  { dispatch$: props.dispatch }
);
OliverJAsh

OliverJAsh commented on May 29, 2020

@OliverJAsh
ContributorAuthor

I think observable-hooks solves this problem because you can do:

const dispatch$ = useObservable(pluckFirst, [props.dispatch]);

and then use that inside any closure.

Sawtaytoes

Sawtaytoes commented on Jun 5, 2020

@Sawtaytoes

I like the idea of each prop being an observable, but that could get somewhat complex when listening to many of them.

OliverJAsh

OliverJAsh commented on Aug 25, 2020

@OliverJAsh
ContributorAuthor

Re. #99 (comment)

Another idea: the inputs could be provided as separate observables. This way, users don't have to destructure the observable, nor do they have to worry about adding distinctUntilChanged:

This is a start: https://stackblitz.com/edit/react-ts-p7ueqk.

@Brooooooklyn What do you think to this idea?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @OliverJAsh@Brooooooklyn@Sawtaytoes

        Issue actions

          Suggestion: use object for `input` instead of tuple · Issue #99 · LeetCode-OpenSource/rxjs-hooks