Throttle / debounce / delay paramater for Reaction #20
Unanswered
slimshader
asked this question in
Ideas
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
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.
All reactions