Skip to content

Commit 7f549ec

Browse files
RSNarafacebook-github-bot
authored andcommitted
Deprecate BatchedBridge.registerCallableModule (#42717)
Summary: Pull Request resolved: #42717 This diff introduces a new bridge/bridgeless-agnostic api for registering JavaScript modules with React Native. Usage: ``` import {registerCallableModule} from 'react-native'; registerCallableModule('FooModule', () => {...}); registerCallableModule('BarModule', {...}); ``` Changelog: [General][Deprecated] - Deprecate BatchedBridge.registerCallableModule. Please use registerCallableModule instead Reviewed By: javache Differential Revision: D52805387 fbshipit-source-id: c7e77f0450ce1b0de349fa540c3a812256da054f
1 parent daa3080 commit 7f549ec

3 files changed

Lines changed: 58 additions & 0 deletions

File tree

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow strict-local
8+
* @format
9+
*/
10+
11+
'use strict';
12+
13+
type Module = {...};
14+
type RegisterCallableModule = (
15+
name: string,
16+
moduleOrFactory: Module | (void => Module),
17+
) => void;
18+
19+
const registerCallableModule: RegisterCallableModule = (function () {
20+
if (global.RN$Bridgeless === true) {
21+
return (name, moduleOrFactory) => {
22+
if (typeof moduleOrFactory === 'function') {
23+
global.RN$registerCallableModule(name, moduleOrFactory);
24+
return;
25+
}
26+
27+
global.RN$registerCallableModule(name, () => moduleOrFactory);
28+
};
29+
}
30+
31+
const BatchedBridge = require('../BatchedBridge/BatchedBridge');
32+
return (name, moduleOrFactory) => {
33+
if (typeof moduleOrFactory === 'function') {
34+
BatchedBridge.registerLazyCallableModule(name, moduleOrFactory);
35+
return;
36+
}
37+
38+
BatchedBridge.registerCallableModule(name, moduleOrFactory);
39+
};
40+
})();
41+
42+
export default registerCallableModule;

packages/react-native/Libraries/__tests__/__snapshots__/public-api-test.js.snap

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3929,6 +3929,17 @@ exports[`public API should not change unintentionally Libraries/Core/checkNative
39293929

39303930
exports[`public API should not change unintentionally Libraries/Core/polyfillPromise.js 1`] = `""`;
39313931

3932+
exports[`public API should not change unintentionally Libraries/Core/registerCallableModule.js 1`] = `
3933+
"type Module = { ... };
3934+
type RegisterCallableModule = (
3935+
name: string,
3936+
moduleOrFactory: Module | ((void) => Module)
3937+
) => void;
3938+
declare const registerCallableModule: RegisterCallableModule;
3939+
declare export default typeof registerCallableModule;
3940+
"
3941+
`;
3942+
39323943
exports[`public API should not change unintentionally Libraries/Core/setUpAlert.js 1`] = `""`;
39333944

39343945
exports[`public API should not change unintentionally Libraries/Core/setUpBatchedBridge.js 1`] = `""`;
@@ -9164,6 +9175,7 @@ declare export default class EventEmitter<TEventToArgsMap: { ... }>
91649175
exports[`public API should not change unintentionally index.js 1`] = `
91659176
"export type HostComponent<T> = _HostComponentInternal<T>;
91669177
declare module.exports: {
9178+
get registerCallableModule(): RegisterCallableModule,
91679179
get AccessibilityInfo(): AccessibilityInfo,
91689180
get ActivityIndicator(): ActivityIndicator,
91699181
get Button(): Button,

packages/react-native/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import typeof TouchableNativeFeedback from './Libraries/Components/Touchable/Tou
4343
import typeof TouchableOpacity from './Libraries/Components/Touchable/TouchableOpacity';
4444
import typeof TouchableWithoutFeedback from './Libraries/Components/Touchable/TouchableWithoutFeedback';
4545
import typeof View from './Libraries/Components/View/View';
46+
import typeof RegisterCallableModule from './Libraries/Core/registerCallableModule';
4647
import typeof NativeEventEmitter from './Libraries/EventEmitter/NativeEventEmitter';
4748
import typeof RCTDeviceEventEmitter from './Libraries/EventEmitter/RCTDeviceEventEmitter';
4849
import typeof RCTNativeAppEventEmitter from './Libraries/EventEmitter/RCTNativeAppEventEmitter';
@@ -97,6 +98,9 @@ const invariant = require('invariant');
9798
export type HostComponent<T> = _HostComponentInternal<T>;
9899

99100
module.exports = {
101+
get registerCallableModule(): RegisterCallableModule {
102+
return require('./Libraries/Core/registerCallableModule').default;
103+
},
100104
// Components
101105
get AccessibilityInfo(): AccessibilityInfo {
102106
return require('./Libraries/Components/AccessibilityInfo/AccessibilityInfo')

0 commit comments

Comments
 (0)