Dataflow async
Library ID dataflow-async
Latest version 1.0.5

Wait while loading

Sometimes it may be necessary to synchronously wait for an attribute to finish loading, albeit not frequently. We can easily do this by using the function waitWhileLoading(). The function has this behavior:

  • Returns the first Loaded or Error instance that the attribute assumes since this call (current value included)
  • Returns a Loading instance if the maxWaitTime parameter expires. If the maxWaitTime is null then this function can never return a Loading instance.
  • Throws InterruptedException if the caller thread is interrupted while waiting.

Sample usage:

import com.femastudios.dataflow.async.util.* fun main() { val attr = attributeOf { Thread.sleep(1000) "hello" } val start = System.currentTimeMillis() println("Starting waiting...") val data = attr.waitWhileLoading() println("Wait over after " + (System.currentTimeMillis() - start) + "ms") println("Data is $data") }
val attr = Attribute.of(wc -> { Thread.sleep(1000) return "hello" }); long start = System.currentTimeMillis(); System.out.println("Starting waiting..."); AttributeData<String> data = attr.waitWhileLoading(); System.out.println("Wait over after " + (System.currentTimeMillis() - start) + "ms"); System.out.println("Data is " + data);

You'll see that if you add a small maxTime parameter the returned data will be loading.