Task

class Task<ResultT>(val scope: CoroutineScope, val config: TaskConfig? = TaskConfig(), block: suspend CoroutineScope.() -> ResultT)

A discriminated union that encapsulates a successful outcome with a value of type ResultT or a failure with an arbitrary Throwable ClientException.

Parameters

scope

a CoroutineScope defaulted to Default can provide your own scope as well, ensure its testable by injecting the provider.

Constructors

Link copied to clipboard
private constructor(scope: CoroutineScope, config: TaskConfig? = TaskConfig(), block: suspend CoroutineScope.() -> ResultT)

Types

Link copied to clipboard
class Builder
Link copied to clipboard
object Companion

Properties

Link copied to clipboard
private var cancelTask: (ClientException) -> Unit?

using the Task's instance, we can cancel and running coroutines of this Task's scope. alternatively we can use the Task's cancel fun @see Task.cancel

Link copied to clipboard
private var complete: (ResultT?, ClientException?) -> Unit?

Using the Task's instance, we can call complete.invoke passing in a ResultT if job succeeded or preferred Throwable ClientException as a failure result

Link copied to clipboard
private var completeUI: (ResultT?, ClientException?) -> Unit?
Link copied to clipboard
private val config: TaskConfig?
Link copied to clipboard
private var failure: (ClientException) -> Unit?

using the Task's instance, we can call failure.invoke passing in a preferred Throwable ClientException as a failure result

Link copied to clipboard
private var failureUI: (ClientException) -> Unit?
Link copied to clipboard
Link copied to clipboard
internal var job: Deferred<ResultT>

Can be used to assign the task job while doing unit testing, not meant to be exposed or used in actual logic

Link copied to clipboard
private val scope: CoroutineScope
Link copied to clipboard
private val scopeUI: CoroutineScope

This scope is used only created and used when invoking successUI and failureUI

Link copied to clipboard
private var success: (ResultT) -> Unit?

using the Task's instance, we can call success.invoke and passing a success result

Link copied to clipboard
private var successUI: (ResultT) -> Unit?
Link copied to clipboard
private var tries: Int

Number of tires to run the task , this will configured using TaskConfig.retry

Functions

Link copied to clipboard
Link copied to clipboard
suspend fun <ResultT> Task<ResultT>.await(): ResultT

Wait for the task to finish

Link copied to clipboard

Wait for the task to finish

Link copied to clipboard
fun cancel(message: String, throwable: ClientException? = null)

cancels the current task's scope and invokes a cancelTask.

Link copied to clipboard
Link copied to clipboard
fun get(): ResultT

Wait for the task to finish it is a blocking call, if using it inside coroutine or suspended function use await instead

Link copied to clipboard

Wait for the task to finish it is a blocking call, if using it inside coroutine or suspended function use awaitOrNull instead

Link copied to clipboard

a cancel scope that is attachable to a task instance * task().onCancel { // Do this on cancel } use this scope to listen on the cancel response of a task

Link copied to clipboard

a task finished scope that is attachable to a task instance * task().onComplete {res, e -> // handle task completed } Use this scope to retrieve the end result of a task with a ResultT if succeed or ClientException if failed

Link copied to clipboard

Experimental callback to avoid adding switch threads logic, Also allow a chance to perform UI and background jobs after a task is done without dealing with multithreading

Link copied to clipboard

a failure scope that is attachable to a task instance * task().onFailure { // handle failure } use this scope to retrieve the failure response of a task

Link copied to clipboard

Experimental callback to avoid adding switch threads logic, Also allow a chance to perform UI and background jobs after a task is done without dealing with multithreading

Link copied to clipboard
fun onSuccess(action: (ResultT) -> Unit): Task<ResultT>

a success scope that is attachable to a task instance * task().onSuccess { // do background logic , call another api } use this scope to retrieve the success response of a task

Link copied to clipboard
fun onSuccessUI(action: (ResultT) -> Unit): Task<ResultT>

Experimental callback to avoid letting switch threads, Also allow a chance to perform UI and background jobs after a task is done without dealing with multithreading

Link copied to clipboard
private suspend fun runBlock(scope: CoroutineScope, config: TaskConfig, handleFailure: Boolean, block: suspend CoroutineScope.() -> ResultT): ResultT