Skip to content

Commit

Permalink
Allow ClientThread to re-run invokeLater-ed methods
Browse files Browse the repository at this point in the history
  • Loading branch information
abextm committed Apr 26, 2018
1 parent 5054caf commit d680c91
Showing 1 changed file with 27 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.google.inject.Inject;
import java.util.Iterator;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.function.BooleanSupplier;
import javax.inject.Singleton;
import lombok.extern.slf4j.Slf4j;
import net.runelite.api.Client;
Expand All @@ -35,16 +36,32 @@
@Slf4j
public class ClientThread
{
private ConcurrentLinkedQueue<Runnable> invokes = new ConcurrentLinkedQueue<>();
private ConcurrentLinkedQueue<BooleanSupplier> invokes = new ConcurrentLinkedQueue<>();

@Inject
private Client client;

public void invokeLater(Runnable r)
{
if (client.isClientThread())
invokeLater(() ->
{
r.run();
return true;
});
}

/**
* Will run r on the game thread, at a unspecified point in the future.
* If r returns false, r will be ran again, at a later point
*/
public void invokeLater(BooleanSupplier r)
{
if (client.isClientThread())
{
if (r.getAsBoolean())
{
invokes.add(r);
}
return;
}
invokes.add(r);
Expand All @@ -53,14 +70,14 @@ public void invokeLater(Runnable r)
void invoke()
{
assert client.isClientThread();
Iterator<Runnable> ir = invokes.iterator();
Iterator<BooleanSupplier> ir = invokes.iterator();
for (; ir.hasNext(); )
{
Runnable r = ir.next();
ir.remove();
BooleanSupplier r = ir.next();
boolean remove = true;
try
{
r.run();
remove = r.getAsBoolean();
}
catch (ThreadDeath d)
{
Expand All @@ -70,6 +87,10 @@ void invoke()
{
log.warn("Exception in invokeLater", e);
}
if (remove)
{
ir.remove();
}
}
}
}

0 comments on commit d680c91

Please sign in to comment.