Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ghQueueMicrotask #2612

Merged
merged 11 commits into from
Sep 28, 2023
Merged
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
5 changes: 5 additions & 0 deletions src/ghQueueMicrotask.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// `queueMicrotask` was introduced to react-native in version 0.66 (https://github.com/react-native-community/releases/blob/master/CHANGELOG.md#v0660)
// Because Gesture Handler supports versions 0.64+, we have to handle situations where someone uses older version of react native.
// That's why if `queueMicrotask` doesn't exist, we use `setImmediate` instead, since it was used before we switched to `queueMicrotask` in version 2.11.0
export const ghQueueMicrotask =
typeof queueMicrotask === 'function' ? queueMicrotask : setImmediate;
m-bert marked this conversation as resolved.
Show resolved Hide resolved
5 changes: 3 additions & 2 deletions src/handlers/createHandler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { isFabric, isJestEnv, tagMessage } from '../utils';
import { ActionType } from '../ActionType';
import { PressabilityDebugView } from './PressabilityDebugView';
import GestureHandlerRootViewContext from '../GestureHandlerRootViewContext';
import { ghQueueMicrotask } from '../ghQueueMicrotask';

const UIManagerAny = UIManager as any;

Expand Down Expand Up @@ -214,7 +215,7 @@ export default function createHandler<
// queueMicrotask. This makes it so update() function gets called after all
// react components are mounted and we expect the missing ref object to
// be resolved by then.
queueMicrotask(() => {
ghQueueMicrotask(() => {
this.update(UNRESOLVED_REFS_RETRY_LIMIT);
});
}
Expand Down Expand Up @@ -378,7 +379,7 @@ export default function createHandler<
// `ref={refObject}` it's possible that it won't be resolved in time. Seems like trying
// again is easy enough fix.
if (hasUnresolvedRefs(props) && remainingTries > 0) {
queueMicrotask(() => {
ghQueueMicrotask(() => {
this.update(remainingTries - 1);
});
} else {
Expand Down
3 changes: 2 additions & 1 deletion src/handlers/gestureHandlerCommon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { ValueOf } from '../typeUtils';
import { handlerIDToTag } from './handlersRegistry';
import { toArray } from '../utils';
import RNGestureHandlerModule from '../RNGestureHandlerModule';
import { ghQueueMicrotask } from '../ghQueueMicrotask';

const commonProps = [
'id',
Expand Down Expand Up @@ -235,7 +236,7 @@ let flushOperationsScheduled = false;
export function scheduleFlushOperations() {
if (!flushOperationsScheduled) {
flushOperationsScheduled = true;
queueMicrotask(() => {
ghQueueMicrotask(() => {
RNGestureHandlerModule.flushOperations();

flushOperationsScheduled = false;
Expand Down
7 changes: 4 additions & 3 deletions src/handlers/gestures/GestureDetector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import { RNRenderer } from '../../RNRenderer';
import { isNewWebImplementationEnabled } from '../../EnableNewWebImplementation';
import { nativeViewGestureHandlerProps } from '../NativeViewGestureHandler';
import GestureHandlerRootViewContext from '../../GestureHandlerRootViewContext';
import { ghQueueMicrotask } from '../../ghQueueMicrotask';

declare const global: {
isFormsStackingContext: (node: unknown) => boolean | null; // JSI function
Expand Down Expand Up @@ -159,7 +160,7 @@ function attachHandlers({

// use queueMicrotask to extract handlerTags, because all refs should be initialized
// when it's ran
queueMicrotask(() => {
ghQueueMicrotask(() => {
if (!mountedRef.current) {
return;
}
Expand All @@ -179,7 +180,7 @@ function attachHandlers({

// use queueMicrotask to extract handlerTags, because all refs should be initialized
// when it's ran
queueMicrotask(() => {
ghQueueMicrotask(() => {
if (!mountedRef.current) {
return;
}
Expand Down Expand Up @@ -267,7 +268,7 @@ function updateHandlers(
// use queueMicrotask to extract handlerTags, because when it's ran, all refs should be updated
// and handlerTags in BaseGesture references should be updated in the loop above (we need to wait
// in case of external relations)
queueMicrotask(() => {
ghQueueMicrotask(() => {
if (!mountedRef.current) {
return;
}
Expand Down
3 changes: 2 additions & 1 deletion src/web_hammer/GestureHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { findNodeHandle } from 'react-native';
import { State } from '../State';
import { EventMap } from './constants';
import * as NodeManager from './NodeManager';
import { ghQueueMicrotask } from '../ghQueueMicrotask';

// TODO(TS) Replace with HammerInput if https://github.com/DefinitelyTyped/DefinitelyTyped/pull/50438/files is merged
export type HammerInputExt = Omit<HammerInput, 'destroy' | 'handler' | 'init'>;
Expand Down Expand Up @@ -508,7 +509,7 @@ abstract class GestureHandler {
.filter((v) => v);

if (shouldUseTouchEvents !== this.shouldUseTouchEvents(props)) {
queueMicrotask(() => {
ghQueueMicrotask(() => {
// if the undelying event API needs to be changed, we need to unmount and mount
// the hammer instance again.
this.destroy();
Expand Down
Loading