diff --git a/plugin/src/util/Callbacks.cs b/plugin/src/util/Callbacks.cs new file mode 100644 index 0000000..71c764f --- /dev/null +++ b/plugin/src/util/Callbacks.cs @@ -0,0 +1,45 @@ +using System; +using System.Linq; +using UnityEngine; + +namespace PiUtils.Util; + +public class Callbacks +{ + public static Action Debounce(Action callback, float seconds) + { + Coroutine timeout = null; + T lastValue; + + return (T arg) => + { + lastValue = arg; + if (timeout != null) + { + return; + } + + timeout = AsyncGameObject.Timeout(() => + { + timeout = null; + callback(lastValue); + }, seconds); + }; + } + + public static Action Unique(Action callback) + { + Option lastValue = Option.None(); + + return (T arg) => + { + if (lastValue.Contains(arg)) + { + return; + } + + lastValue = Option.Some(arg); + callback(arg); + }; + } +} diff --git a/plugin/src/util/Debouncer.cs b/plugin/src/util/Debouncer.cs deleted file mode 100644 index 9feb94e..0000000 --- a/plugin/src/util/Debouncer.cs +++ /dev/null @@ -1,22 +0,0 @@ -using System; -using UnityEngine; - -namespace PiUtils.Util; - -public class Debouncer -{ - public static Action Debounce(Action callback, float seconds) - { - Coroutine timeout = null; - - return (T arg) => - { - if (timeout != null) - { - AsyncGameObject.Cancel(timeout); - } - - timeout = AsyncGameObject.Timeout(() => callback(arg), seconds); - }; - } -}