Package 

Class Task


  • 
    public final class Task<T extends Object>
    
                        

    When interacting with the scanner, we usually don't want to block the main thread Instead we want to e.g. update the UI and use the result once it is available

    Since we are supporting multiple platforms, we have to use Kotlin coroutines, which support every platform to some extent. The only function that is not available on JS is runBlocking, that's why this Task does not support being compiled to JS

    The first way of receiving the result of a task is by using await. This will block the coroutine execution until the result is available or an exception is thrown. This is only available in a Coroutine Context.

    The second way is by using onComplete, onSuccess or onError. By using these methods, we are not blocking and instead are executing the task asynchronously Once a result is received, the result can be used in the closure

    • Field Summary

      Fields 
      Modifier and Type Field Description
      private final <ERROR CLASS> scope
      private final T result
      private final Boolean isComplete
    • Method Summary

      Modifier and Type Method Description
      final <ERROR CLASS> getScope()
      final T getResult()
      final Boolean getIsComplete()
      final <ERROR CLASS> waitForResult() Waits for the result of the current task, without caring about the result or an error.
      final T await() waits for finishing the task within a suspending function.
      final Task<T> onComplete(Function1<Task<T>, Unit> completeCallback) Lambda is called once task has completed.
      final Task<T> onComplete(TaskCallback<T> taskCallback) Lambda is called once task has completed.
      final Task<T> onSuccess(Function1<T, Unit> doneCallback) Lambda is called once task was successful.
      final Task<T> onError(Function1<Throwable, Unit> errorCallback) Lambda is called once error was thrown.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • getScope

         final <ERROR CLASS> getScope()
      • waitForResult

         final <ERROR CLASS> waitForResult()

        Waits for the result of the current task, without caring about the result or an error. It won't return the result neither will it throw. You have to call Task.result for this.

      • await

         final T await()

        waits for finishing the task within a suspending function. returns result or throws exception if exception occurred

      • onComplete

         final Task<T> onComplete(Function1<Task<T>, Unit> completeCallback)

        Lambda is called once task has completed. You can then check .result for successful result or it will throw an error. Returns Task.

      • onComplete

         final Task<T> onComplete(TaskCallback<T> taskCallback)

        Lambda is called once task has completed. You can then check .result for successful result or it will throw an error. Returns Task.

      • onSuccess

         final Task<T> onSuccess(Function1<T, Unit> doneCallback)

        Lambda is called once task was successful. Won't be called for errors. Returns Task.

      • onError

         final Task<T> onError(Function1<Throwable, Unit> errorCallback)

        Lambda is called once error was thrown. Returns Task.