Skip to content
Open
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
19 changes: 14 additions & 5 deletions packages/react-native/Libraries/BatchedBridge/NativeModules.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,19 @@
'use strict';

import type {ExtendedError} from '../Core/ExtendedError';
import typeof BatchedBridgeT from './BatchedBridge';

const BatchedBridge = require('./BatchedBridge').default;
const invariant = require('invariant');

// Lazy-require to avoid loading the legacy bridge in bridgeless mode.
let BatchedBridge: ?BatchedBridgeT = null;
function getBatchedBridge(): BatchedBridgeT {
if (BatchedBridge == null) {
BatchedBridge = require('./BatchedBridge').default;
}
return BatchedBridge;
}

export type ModuleConfig = [
string /* name */,
?{...} /* constants */,
Expand Down Expand Up @@ -77,7 +86,7 @@ function genModule(
}

if (__DEV__) {
BatchedBridge.createDebugLookup(moduleID, moduleName, methods);
getBatchedBridge().createDebugLookup(moduleID, moduleName, methods);
}

return {name: moduleName, module};
Expand Down Expand Up @@ -106,7 +115,7 @@ function genMethod(moduleID: number, methodID: number, type: MethodType) {
// $FlowFixMe[incompatible-type]
const enqueueingFrameError: ExtendedError = new Error();
return new Promise((resolve, reject) => {
BatchedBridge.enqueueNativeCall(
getBatchedBridge().enqueueNativeCall(
moduleID,
methodID,
args,
Expand Down Expand Up @@ -142,15 +151,15 @@ function genMethod(moduleID: number, methodID: number, type: MethodType) {
const callbackCount = hasSuccessCallback + hasErrorCallback;
const newArgs = args.slice(0, args.length - callbackCount);
if (type === 'sync') {
return BatchedBridge.callNativeSyncHook(
return getBatchedBridge().callNativeSyncHook(
moduleID,
methodID,
newArgs,
onFail,
onSuccess,
);
} else {
BatchedBridge.enqueueNativeCall(
getBatchedBridge().enqueueNativeCall(
moduleID,
methodID,
newArgs,
Expand Down