Skip to Content
API reference

JobQueue

The promise jobs of one execution owner and the host loop that runs them.

AsyncType: typescriptnative.JobQueueArtifact: com.margelo:typescript-native-stdlibJVM only

JobQueue class

public final class JobQueue

The promise jobs of one execution owner: a FIFO, and the host loop that runs it.

An execution owner is a thread whose host loop runs tasks one at a time: an Android thread with a running Looper (found with no setup), or a thread whose loop the embedder attached once with attach. Promise reactions, continuations of async functions, adoption jobs and enqueue tasks of an owner go into its FIFO. Queueing a job never runs it. The queue asks the host loop for one turn, and that turn runs the jobs in order, including jobs that jobs add, on the owner thread.

A thread without such a loop runs synchronous TypeScript and can construct and fulfill promises or reject already observed ones. It cannot register a reaction or start an async function: current throws an IllegalStateException that names the thread and the remedy.

Fields marked "owner" are read and written only by the owner thread.

Static methods

static JobQueue current()

The queue of the calling thread. Throws IllegalStateException when the thread has no host loop, or when its loop has stopped. Promise code calls this where it registers a reaction or starts an async function.

static JobQueue forAsync(String entry)

Validates setup before a generated async frame or its body is created.

entry
the async entry name and source location
Returns
the calling thread's live owner
Throws IllegalStateException
if setup is missing or the owner has stopped
static void attach(Driver loop)

Makes the calling thread an execution owner whose host loop is loop: for an embedder whose thread already runs an event loop, once when that thread starts. Android Looper threads need no call. Throws IllegalStateException when the thread already has a live host loop.

static void detach()

Ends what attach began. Jobs that are still queued, and jobs that arrive later, never run and are reported. On a thread whose loop was not attached, such as a Looper thread, it does nothing.

Methods

void enqueue(Runnable task)

Runs task as a job of this queue, after the jobs that are queued now: queueMicrotask, and the way another thread hands work to an owner. Throws IllegalStateException when the host loop has stopped.

String toString()

JobQueue.Driver interface

public interface Driver

The host loop of an owner. post hands over one task. The loop runs it later, exactly once, on the thread that attached the loop, after the code that is running on that thread has returned to the loop. post returns without waiting for the task. A loop that can no longer run tasks throws java.util.concurrent.RejectedExecutionException, and only that: the queue then runs no job any more. Any other exception from post means that this one task was not taken and the loop is still there. An executor with more than one thread, a loop that runs the task inside post, and a queue of another thread are no host loop: the first turn refuses them.

Methods

void post(Runnable turn)

Kotlin extensions Kotlin

Artifact: com.margelo:typescript-native-runtime-kotlin

Extension functions

asJobDriverKotlinextension
fun CoroutineDispatcher.asJobDriver(): JobQueue.Driver

A dispatcher as the host loop of the thread that it runs on: JobQueue.attach(dispatcher.asJobDriver()), called on that thread. It always dispatches, so an immediate dispatcher defers too. Only a dispatcher that runs everything on one thread is a host loop: Dispatchers.Default, Dispatchers.IO and limitedParallelism views of them are refused at their first turn, and so is a dispatcher that runs blocks inside dispatch. On Android a Looper thread needs no call.

Generated when the website builds, from JobQueue.java, PromiseCoroutines.kt.

Language

TypeScriptNo TypeScript typeJVMJava and KotlinSwiftNot generated yet

Remembered on every API page

On this page