Skip to content

Commit 9d19992

Browse files
sammy-SCfacebook-github-bot
authored andcommitted
add documentation for Fantom public APIs (facebook#50034)
Summary: Pull Request resolved: facebook#50034 changelog: [internal] add documentation for Fantom public APIs. Reviewed By: rubennorte Differential Revision: D71197744 fbshipit-source-id: dc7f97732eb6437bc0a33dec9fe2430e38c49e66
1 parent 0f45d33 commit 9d19992

File tree

1 file changed

+191
-9
lines changed
  • packages/react-native-fantom/src

1 file changed

+191
-9
lines changed

packages/react-native-fantom/src/index.js

Lines changed: 191 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -129,17 +129,41 @@ const DEFAULT_TASK_PRIORITY = schedulerPriorityImmediate;
129129
* Schedules a task to run on the event loop.
130130
* If the work loop is running, it will be executed according to its priority.
131131
* Otherwise, it will wait in the queue until the work loop runs.
132+
*
133+
* @param task - The task to be scheduled.
134+
*
135+
* @example
136+
* ```
137+
* Fantom.scheduleTask(() => {
138+
* // Task to be run within React Native's scheduling.
139+
* });
140+
*
141+
* // The task has not run yet.
142+
*
143+
* Fantom.runWorkLoop(); // Trigger work loop.
144+
*
145+
* // The task has been executed.
146+
* ```
132147
*/
133148
export function scheduleTask(task: () => void | Promise<void>) {
134149
nativeRuntimeScheduler.unstable_scheduleCallback(DEFAULT_TASK_PRIORITY, task);
135150
}
136151

137152
let flushingQueue = false;
138153

139-
/*
140-
* Runs a task on the event loop. To be used together with root.render.
141-
*
154+
/**
155+
* Runs a task on the event loop.
142156
* React must run inside of event loop to ensure scheduling environment is closer to production.
157+
*
158+
* @param task - The task to run.
159+
*
160+
* @example
161+
* ```
162+
* const root = Fantom.createRoot();
163+
* Fantom.runTask(() => {
164+
* root.render(<View />);
165+
* });
166+
* ```
143167
*/
144168
export function runTask(task: () => void | Promise<void>) {
145169
if (flushingQueue) {
@@ -152,15 +176,32 @@ export function runTask(task: () => void | Promise<void>) {
152176
runWorkLoop();
153177
}
154178

155-
/*
156-
* Simmulates running a task on the UI thread and forces side effect to drain the event queue, scheduling events to be dispatched to JavaScript.
179+
/**
180+
* Simulates running a task on the UI thread and forces side effect to drain
181+
* the event queue, scheduling events to be dispatched to JavaScript.
182+
* To be used when enqueuing native events.
183+
*
184+
* @param task - The task to run on the UI thread.
185+
*
186+
* @example
187+
* ```
188+
* Fantom.runOnUIThread(() => {
189+
* Fantom.enqueueNativeEvent(element, 'focus');
190+
* });
191+
*
192+
* // The effects of `focus` event are *not* yet observable.
193+
*
194+
* Fantom.runWorkLoop();
195+
*
196+
* // The effects of `focus` event are now observable.
197+
* ```
157198
*/
158199
export function runOnUIThread(task: () => void) {
159200
task();
160201
NativeFantom.flushEventQueue();
161202
}
162203

163-
/*
204+
/**
164205
* Runs a side effect to drain the event queue and dispatches events to JavaScript.
165206
* Useful to flash out all tasks.
166207
*/
@@ -171,6 +212,20 @@ export function flushAllNativeEvents() {
171212

172213
/**
173214
* Runs the event loop until all tasks are executed.
215+
* To be used with `Fantom.enqueueNativeEvent` and `Fantom.scheduleTask`.
216+
*
217+
* @example
218+
* ```
219+
* Fantom.scheduleTask(() => {
220+
* // Task to be run within React Native's scheduling.
221+
* });
222+
*
223+
* // The task has not run yet.
224+
*
225+
* Fantom.runWorkLoop();
226+
*
227+
* // The task has been executed.
228+
* ```
174229
*/
175230
export function runWorkLoop(): void {
176231
if (flushingQueue) {
@@ -187,8 +242,23 @@ export function runWorkLoop(): void {
187242
}
188243
}
189244

190-
// TODO: Add option to define surface props and pass it to startSurface
191-
// Surfacep rops: concurrentRoot, surfaceWidth, surfaceHeight, layoutDirection, pointScaleFactor.
245+
/**
246+
* Create a Root that can render a React component tree.
247+
*
248+
* Accepts an optional RootConfig with the following optional options:
249+
* @param devicePixelRatio - Numeric value, defaults to 3 (iPhone 14).
250+
* @param viewportHeight - Numeric value, defaults to 844 (iPhone 14).
251+
* @param viewportWidth - Numeric value, defaults to 390 (iPhone 14).
252+
*
253+
* @example
254+
* ```
255+
* const root = Fantom.createRoot({
256+
* viewportWidth: 200, // default is 390
257+
* viewportHeight: 600, // default is 844
258+
* devicePixelRatio: 2, // default is 3
259+
* });
260+
* ```
261+
*/
192262
export function createRoot(rootConfig?: RootConfig): Root {
193263
return new Root(rootConfig);
194264
}
@@ -198,7 +268,25 @@ export function createRoot(rootConfig?: RootConfig): Root {
198268
* It does not wait for it to be flushed in the UI thread or for it to be
199269
* processed by JS.
200270
*
201-
* For a higher level API, use `dispatchNativeEvent`.
271+
* When you simply need to dispatch a native event and observe its effects, use `dispatchNativeEvent`.
272+
*
273+
* @param node - The node to which the event will be dispatched. You must make sure the event is appropriate for the provided node. For example, if sending a scroll event, you must make sure the node is of type <ScrollView />.
274+
* @param type - The type of the event. e.g 'focus', 'blur', 'change', 'scroll', etc.
275+
* @param payload - The data associated with the event. What is delivered as `event.nativeEvent` on a component.
276+
* @param options - Object describing what priority the event is and whether it gets coalesced. For event priority, see `NativeEventCategory`.
277+
*
278+
* @example
279+
* ```
280+
* Fantom.runOnUIThread(() => {
281+
* Fantom.enqueueNativeEvent(element, 'focus');
282+
* });
283+
*
284+
* // The effects of `focus` event are *not* yet observable.
285+
*
286+
* Fantom.runWorkLoop();
287+
*
288+
* // The effects of `focus` event are observable.
289+
* ```
202290
*/
203291
export function enqueueNativeEvent(
204292
node: ReactNativeElement,
@@ -216,6 +304,21 @@ export function enqueueNativeEvent(
216304
);
217305
}
218306

307+
/**
308+
* Dispatches a native event and makes sure its effects are observable after calling this method.
309+
*
310+
* @param node - The node to which the event will be dispatched. You must make sure the event is appropriate for the provided node. For example, if sending a scroll event, you must make sure the node is of type <ScrollView />.
311+
* @param type - The type of the event. e.g 'focus', 'blur', 'change', 'scroll', etc.
312+
* @param payload - The data associated with the event. What is delivered as `event.nativeEvent` on a component.
313+
* @param options - Object describing what priority the event is and whether it gets coalesced. For event priority, see `NativeEventCategory`.
314+
*
315+
* @example
316+
* ```
317+
* Fantom.dispatchNativeEvent(element, 'focus');
318+
*
319+
* // The effects of `focus` are immediately observable.
320+
* ```
321+
*/
219322
export function dispatchNativeEvent(
220323
node: ReactNativeElement,
221324
type: string,
@@ -235,6 +338,47 @@ export type ScrollEventOptions = {
235338
zoomScale?: number,
236339
};
237340

341+
/**
342+
* Enqueues an event to scroll a <ScrollView /> node to the given coordinates.
343+
* It does not wait for it to be flushed in the UI thread or for it to be
344+
* processed by JS.
345+
*
346+
* When you need to simply scroll a <ScrollView /> and observe effects immediately, use `Fantom.scrollTo`.
347+
*
348+
* @params node - A node to be scrolled. Must be of type <ScrollView />.
349+
* @params options - Object describing the scroll position and zoom level. See `ScrollEventOptions` for more details.
350+
*
351+
* @example
352+
* ```
353+
* const root = Fantom.createRoot();
354+
* let maybeScrollViewNode;
355+
*
356+
* Fantom.runTask(() => {
357+
* root.render(
358+
* <ScrollView
359+
* ref={node => {
360+
* maybeScrollViewNode = node;
361+
* }} />
362+
* <ScrollViewContent />
363+
* </ScrollView>,
364+
* );
365+
* });
366+
*
367+
* const element = ensureInstance(maybeScrollViewNode, ReactNativeElement);
368+
*
369+
* Fantom.runOnUIThread(() => {
370+
* Fantom.enqueueScrollEvent(element, {
371+
* x: 20,
372+
* y: 10,
373+
* });
374+
*
375+
* // The changes from scroll event are *not* yet observable.
376+
*
377+
* Fantom.runWorkLoop();
378+
*
379+
* // The changes from scroll event are observable.
380+
* ```
381+
*/
238382
export function enqueueScrollEvent(
239383
node: ReactNativeElement,
240384
options: ScrollEventOptions,
@@ -248,6 +392,9 @@ export function enqueueScrollEvent(
248392
* The call is immediately observable unlike `Fantom.enqueueScrollEvent` where the
249393
* event is queued and not processed.
250394
*
395+
* @params node - A node to be scrolled. Must be of type <ScrollView />.
396+
* @params options - Object describing the scroll position and zoom level. See `ScrollEventOptions` for more details.
397+
*
251398
* @example
252399
* ```
253400
* const root = Fantom.createRoot();
@@ -282,6 +429,30 @@ export function scrollTo(
282429
runWorkLoop();
283430
}
284431

432+
/**
433+
* Enqueues modal size update for a given node.
434+
* It does not wait for it to be processed and effects are not observable after calling this method.
435+
* Can only be called on <Modal />.
436+
*
437+
* @params node - A node to have its size updated. Must be of type <Modal />.
438+
* @params size - New size for the node. This would typically be screen size.
439+
*
440+
* @example
441+
* ```
442+
* Fantom.runOnUIThread(() => {
443+
* Fantom.enqueueModalSizeUpdate(modalElement, {
444+
* width: 100,
445+
* height: 100,
446+
* });
447+
* });
448+
*
449+
* // The effects of `enqueueModalSizeUpdate` are *not* yet observable.
450+
*
451+
* Fantom.runWorkLoop();
452+
*
453+
* // The effects of `enqueueModalSizeUpdate` are yet observable.
454+
* ```
455+
*/
285456
export function enqueueModalSizeUpdate(
286457
node: ReactNativeElement,
287458
size: {width: number, height: number},
@@ -372,6 +543,17 @@ if (typeof global.EventTarget === 'undefined') {
372543
);
373544
}
374545

546+
/**
547+
* Saves a heap snapshot after forcing garbage collection.
548+
*
549+
* The heapsnapshot is saved to the filename supplied as an argument.
550+
* If a relative path is supplied, it will be saved relative to where you are invoking the tests.
551+
*
552+
* The supplied filename should end in .heapsnapshot, and it can be opened
553+
* using the "Memory" pane in Chrome DevTools.
554+
*
555+
* @param filepath - File where JS memory heap will be saved.
556+
*/
375557
export function saveJSMemoryHeapSnapshot(filePath: string): void {
376558
if (getConstants().isRunningFromCI) {
377559
throw new Error('Unexpected call to `saveJSMemoryHeapSnapshot` from CI');

0 commit comments

Comments
 (0)