events handling is disabled now when potentially triggered from the scarpet script preventing from recursive callbacks.

This commit is contained in:
gnembon committed 2021-09-06 02:37:08 -03:00
1 parent 8325783bf4
commit 229d0b037b
5 files changed
+104 -32

No files matched your search

@@ -0,0 +1,68 @@
package carpet.script.utils;
import java.util.function.Supplier;
public class GlocalFlag extends ThreadLocal<Boolean>
{
private final boolean initial;
public GlocalFlag(boolean initial)
{
this.initial = initial;
}
@Override
public Boolean initialValue()
{
return initial;
}
/**
* Allows to thread-safely wrap a call while disabling a global flag and setting it back up right after.
* @param action - callback to invoke when the wrapping is all setup
* @param <T> - returned value of that action, whatever that might be
* @return result of the action
*/
public <T> T getWhileDisabled(Supplier<T> action)
{
return whileValueReturn(!initial, action);
}
private <T> T whileValueReturn(boolean what, Supplier<T> action)
{
T result;
boolean previous;
synchronized (this)
{
previous = get();
set(what);
}
try
{
result = action.get();
}
finally
{
set(previous);
}
return result;
}
public boolean runIfEnabled(Runnable action)
{
synchronized (this)
{
if (get() != initial) return false;
set(!initial);
}
try
{
action.run();
}
finally
{
set(initial);
}
return true;
}
}