Skip to content

To create an operator that ignores the default value emission #328

@Jesus-Adaptive

Description

@Jesus-Adaptive

Hello,

I have found a case where it would be interesting to add a native helper method to the @react-rxjs library in order to allow emissions from a State but to ignore the default one.

The idea is to use it as follows:

// state.ts - common states module file
const jedisByFilmId$ = state((filmId: string) =>
  requestJedis$.pipe(
    filter(character => character.filmId === filmId),
    toArray(),
  ),
  []
)

// JedisName.tsx - jedis names component
const firstMovieJedisNames$ = jedisByFilmId$('first-movie-uuid').pipe(
  ignoreDefaultEmission(),
  map((jedis) => jedis.name)
)

const JedisNames = () => {
  // component suspended in the closest Subscribe boundary
  // rather than requiring to handle default states 🥇
  const names = useStateObservable(firstMovieJedisNames$)

  return (<List>{names}</List>)
}

The idea is to not to emit an empty array and avoid hacks or initial states to handle the situation.


Of course, we can prevent this by splitting the root state in one state + another state that implements the default value for another use case. But in that case we need a separate root state that avoids implementing a default value.

const requestJedisByFilmId$ = state((filmId: string) =>
  requestJedis$.pipe(
    filter(character => character.filmId === filmId),
    toArray(),
  )
)

const jedisByFilmId$ = state((filmId: string) => requestJedisByFilm$(filmId), [])

// another module
const firstMovieJedisNames$ = requestJedisByFilmId$('first-movie-uuid').pipe(
  map((jedis) => jedis.name)
)

I'm happy to create a PR - let me know if it makes sense to include the operator in the library.

Jesús

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions