mobx-utils icon indicating copy to clipboard operation
mobx-utils copied to clipboard

It might be better for fromResource to have dispose function returned from the setup function

Open upsuper opened this issue 3 years ago • 0 comments

For example, the code in the README would be:

function createObservableUser(dbUserRecord) {
  return fromResource(
    (sink) => {
      // sink the current state
      sink(dbUserRecord.fields)
      // subscribe to the record, invoke the sink callback whenever new data arrives
      const subscription = dbUserRecord.onUpdated(() => {
        sink(dbUserRecord.fields)
      })
      return () => {
        // the user observable is not in use at the moment, unsubscribe (for now)
        dbUserRecord.unsubscribe(subscription)
      }
    },
  )
}

This way we can avoid relying on storing the state outside, making it easier to use.

upsuper avatar Sep 23 '22 05:09 upsuper