Skip to content
Discussion options

You must be logged in to vote

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?.

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by slimshader
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Ideas
Labels
None yet
2 participants