Skip to content

Commit

Permalink
feat(callback): add callback utils (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
Xenira committed Mar 6, 2024
1 parent 85f243b commit 3e6de5c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 22 deletions.
45 changes: 45 additions & 0 deletions plugin/src/util/Callbacks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using System.Linq;
using UnityEngine;

namespace PiUtils.Util;

public class Callbacks
{
public static Action<T> Debounce<T>(Action<T> 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<T> Unique<T>(Action<T> callback)
{
Option<T> lastValue = Option<T>.None();

return (T arg) =>
{
if (lastValue.Contains(arg))
{
return;
}
lastValue = Option<T>.Some(arg);
callback(arg);
};
}
}
22 changes: 0 additions & 22 deletions plugin/src/util/Debouncer.cs

This file was deleted.

0 comments on commit 3e6de5c

Please sign in to comment.