Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions packages/react-native-fantom/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,17 @@ class Root {
// TODO: add an API to check if all surfaces were deallocated when tests are finished.
}

const DEFAULT_TASK_PRIORITY = schedulerPriorityImmediate;

/**
* Schedules a task to run on the event loop.
* If the work loop is running, it will be executed according to its priority.
* Otherwise, it will wait in the queue until the work loop runs.
*/
export function scheduleTask(task: () => void | Promise<void>) {
nativeRuntimeScheduler.unstable_scheduleCallback(DEFAULT_TASK_PRIORITY, task);
}

let flushingQueue = false;

/*
Expand All @@ -70,14 +81,23 @@ let flushingQueue = false;
export function runTask(task: () => void | Promise<void>) {
if (flushingQueue) {
throw new Error(
'Nested runTask calls are not allowed. If you want to schedule a task from inside another task, use scheduleTask instead.',
'Nested `runTask` calls are not allowed. If you want to schedule a task from inside another task, use `scheduleTask` instead.',
);
}

nativeRuntimeScheduler.unstable_scheduleCallback(
schedulerPriorityImmediate,
task,
);
scheduleTask(task);
runWorkLoop();
}

/**
* Runs the event loop until all tasks are executed.
*/
export function runWorkLoop(): void {
if (flushingQueue) {
throw new Error(
'Cannot start the work loop because it is already running. If you want to schedule a task from inside another task, use `scheduleTask` instead.',
);
}

try {
flushingQueue = true;
Expand Down
Loading