Throttle / debounce / delay paramater for Reaction #20
Answered
by
vanifatovvlad
slimshader
asked this question in
Ideas
-
Hi, I am implementing UI in which new set of data is pulled from API every time user changes a TextFiled value. What is the best way to throttle Reactions so that API calls could be only made after user finished typing? With Rx I would use Throttle() operator and I see that MobX Reaction has "delay" parameter that seems to do that too (altho the name implies something else) |
Beta Was this translation helpful? Give feedback.
Answered by
vanifatovvlad
Mar 31, 2023
Replies: 1 comment
-
This method can be implemented on top of the reaction, like: public static Reaction ReactionThrottled<T>(Lifetime lifetime, Func<T> data, Action<T> effect, int ms) {
var current = int.MinValue;
var throttling = false;
return Atom.Reaction(lifetime, data, value => Schedule(++current, value).Forget());
async UniTask Schedule(int target, T value) {
if (throttling) {
await UniTask.Delay(ms);
if (target == current) {
throttling = false;
if (!lifetime.IsDisposed) {
effect?.Invoke(value);
}
}
}
else {
throttling = true;
effect?.Invoke(value);
await UniTask.Delay(ms);
if (target == current) {
throttling = false;
}
}
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
slimshader
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This method can be implemented on top of the reaction, like: