mobx-utils
mobx-utils copied to clipboard
It might be better for fromResource to have dispose function returned from the setup function
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.