You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Usually, I use onBecomeObserved and onBecomeUnobserved in pairs. Typically, this is the start and stop of some action. It seems that it would be more concise to combine this into one action. I solved the problem for myself in the following way (naming is terrible, i know):
In your documentation, you provide the following example:
exportclassCity{locationtemperatureintervalconstructor(location){makeAutoObservable(this,{resume: false,suspend: false})this.location=location// Only start data fetching if temperature is actually used!onBecomeObserved(this,"temperature",this.resume)onBecomeUnobserved(this,"temperature",this.suspend)}resume=()=>{log(`Resuming ${this.location}`)this.interval=setInterval(()=>this.fetchTemperature(),5000)}suspend=()=>{log(`Suspending ${this.location}`)this.temperature=undefinedclearInterval(this.interval)}fetchTemperature=flow(function*(){// Data fetching logic...})}
With the new helper, the example could look a bit more concise:
exportclassCity{locationtemperatureconstructor(location){makeAutoObservable(this,{resume: false,suspend: false})this.location=location// Only start data fetching if temperature is actually used!onObserveWithCleanup(this,"temperature",this.startFetch)}startFetch=()=>{log(`Resuming ${this.location}`)constinterval=setInterval(()=>this.fetchTemperature(),5000)return()=>{log(`Suspending ${this.location}`)this.temperature=undefinedclearInterval(interval)};}fetchTemperature=flow(function*(){// Data fetching logic...})}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Usually, I use onBecomeObserved and onBecomeUnobserved in pairs. Typically, this is the start and stop of some action. It seems that it would be more concise to combine this into one action. I solved the problem for myself in the following way (naming is terrible, i know):
In your documentation, you provide the following example:
With the new helper, the example could look a bit more concise:
Beta Was this translation helpful? Give feedback.
All reactions