-
I am working on a react-native application that tries to be synced with a server but I am facing a problem. I have a observable
With this code, re-rendering works fine and the slider value changes every 1 sec on the screen; but the problem is that when the user is changing the value using the slider, the app sends the new data to the server to stay synced but the user can still be playing with the slider... and when the next fetching happens during this period, there is a glitch in the UI because the component reacts to the observerable. This problem gets worse when another client is also changing the value during this period. What I thought can solve the problem was to temporarily stop the screen to re-render on the change of the
Is there a way to achieve this in the library? |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments
-
Can't you pause syncing the observable in onSlidingStart={() => {
data.sync = false
}}
onSlidingComplete={()=>{
data.sync = true
}}
// onSyncResponse
if (data.sync) {
data.value = response.body.value
} Alternatively you can have 2 observables - 1 synced with server state and 1 only client side. Then you will subscribe to one or to another based on whether user is sliding or not. <Slider
// use correct observable
value={data.sliding ? data.localValue : data.value}
minimumValue={0}
maximumValue={100}
onValueChange={(value) => {
// update both server and local
sendValueToServer(value);
data.localValue = value;
}}
// toggle sliding state
onSlidingStart={() => {
data.sliding = true
}}
onSlidingComplete={()=>{
data.sliding = false
}}
/> |
Beta Was this translation helpful? Give feedback.
-
Thank you so much for these great suggestions. @mweststrate I didn't know about the @urugator I think I can do your first solution... the syncing happens in the backend so I have to place the |
Beta Was this translation helpful? Give feedback.
-
I think you just have to immediately update the value in addition to sending it to server. onValueChange={(value) => {
// update both server and local
data.value = value;
data.localValue = value;
sendValueToServer(value);
}} Therefore the synced value is always up to date, unless server says otherwise on next sync - meaning the value was updated by other client and the jump is practically unavoidable. Additionally (and regardless of solution I think) you will need to associate a timestamp with each update and ignore the server value in case it's older then the current one. I would suggest to leave the |
Beta Was this translation helpful? Give feedback.
-
Rendering and side effects shouldn't be coupled. If your data updates should not trigger effects yet, it sounds like you need a view model that holds temporarily state, that only should merged back upon a certain event, or that your side effect shouldn't be fire automatically by using useEffect / autorun, but should rather happen on a specific user event instead (e.g. some onSubmit handler). Anyway moving this to discussions as it is not a thing that will change MobX itself. |
Beta Was this translation helpful? Give feedback.
I think you just have to immediately update the value in addition to sending it to server.
Therefore the synced value is always up to date, unless server says otherwise on next sync - meaning the value was updated by other client and the jump is practically unavoidable.
Additionally (and regardless of solution I think) you will need to associate a timestamp with each update and ignore the server value in case it's older then the current one.
I would suggest to leave the
untracked
as a last option.