Skip to content
Closed
Show file tree
Hide file tree
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
27 changes: 27 additions & 0 deletions packages/react-native-fantom/src/__tests__/Fantom-itest.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*/

import 'react-native/Libraries/Core/InitializeCore';

import {createRoot, runTask} from '..';
import * as React from 'react';
import {Text, View} from 'react-native';
Expand All @@ -25,7 +26,7 @@
});

// TODO: fix error handling and make this pass
it.skip('should re-throw errors from the task synchronously', () => {

Check warning on line 29 in packages/react-native-fantom/src/__tests__/Fantom-itest.js

View workflow job for this annotation

GitHub Actions / test_js (20)

Disabled test

Check warning on line 29 in packages/react-native-fantom/src/__tests__/Fantom-itest.js

View workflow job for this annotation

GitHub Actions / test_js (18)

Disabled test
expect(() => {
runTask(() => {
throw new Error('test error');
Expand All @@ -50,7 +51,7 @@
});

// TODO: fix error handling and make this pass
it.skip('should re-throw errors from microtasks synchronously', () => {

Check warning on line 54 in packages/react-native-fantom/src/__tests__/Fantom-itest.js

View workflow job for this annotation

GitHub Actions / test_js (20)

Disabled test

Check warning on line 54 in packages/react-native-fantom/src/__tests__/Fantom-itest.js

View workflow job for this annotation

GitHub Actions / test_js (18)

Disabled test
expect(() => {
runTask(() => {
queueMicrotask(() => {
Expand All @@ -70,6 +71,32 @@

expect(completed).toBe(true);
});

// TODO: when error handling is fixed, this should verify using `toThrow`
it('should throw when running a task inside another task', () => {
let lastCallbackExecuted = 0;
runTask(() => {
lastCallbackExecuted = 1;
runTask(() => {
lastCallbackExecuted = 2;
throw new Error('Recursive runTask should be unreachable');
});
});
expect(lastCallbackExecuted).toBe(1);

runTask(() => {
queueMicrotask(() => {
lastCallbackExecuted = 3;
runTask(() => {
lastCallbackExecuted = 4;
throw new Error(
'Recursive runTask from micro-task should be unreachable',
);
});
});
});
expect(lastCallbackExecuted).toBe(3);
});
});

describe('getRenderedOutput', () => {
Expand Down
16 changes: 15 additions & 1 deletion packages/react-native-fantom/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,31 @@ class Root {
// TODO: add an API to check if all surfaces were deallocated when tests are finished.
}

let flushingQueue = false;

/*
* Runs a task on on the event loop. To be used together with root.render.
*
* React must run inside of event loop to ensure scheduling environment is closer to production.
*/
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.',
);
}

nativeRuntimeScheduler.unstable_scheduleCallback(
schedulerPriorityImmediate,
task,
);
global.$$JSTesterModuleName$$.flushMessageQueue();

try {
flushingQueue = true;
global.$$JSTesterModuleName$$.flushMessageQueue();
} finally {
flushingQueue = false;
}
}

// TODO: Add option to define surface props and pass it to startSurface
Expand Down
Loading