Skip to content

Commit

Permalink
Deprecate BatchedBridge.registerCallableModule (facebook#42717)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: facebook#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: ff469fea3ff78e6bceb79baaa19d9654eb6bbf0b
  • Loading branch information
RSNara authored and facebook-github-bot committed Jan 30, 2024
1 parent 3a789a1 commit 2372461
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
42 changes: 42 additions & 0 deletions packages/react-native/Libraries/Core/registerCallableModule.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
* @format
*/

'use strict';

type Module = {...};
type RegisterCallableModule = (
name: string,
moduleOrFactory: Module | (void => Module),
) => void;

const registerCallableModule: RegisterCallableModule = (function () {
if (global.RN$Bridgeless === true) {
return (name, moduleOrFactory) => {
if (moduleOrFactory instanceof Function) {
global.RN$registerCallableModule(name, moduleOrFactory);
return;
}

global.RN$registerCallableModule(name, () => moduleOrFactory);
};
}

const BatchedBridge = require('../BatchedBridge/BatchedBridge');
return (name, moduleOrFactory) => {
if (moduleOrFactory instanceof Function) {
BatchedBridge.registerLazyCallableModule(name, moduleOrFactory);
return;
}

BatchedBridge.registerCallableModule(name, moduleOrFactory);
};
})();

export default registerCallableModule;
Original file line number Diff line number Diff line change
Expand Up @@ -3929,6 +3929,17 @@ exports[`public API should not change unintentionally Libraries/Core/checkNative

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

exports[`public API should not change unintentionally Libraries/Core/registerCallableModule.js 1`] = `
"type Module = { ... };
type RegisterCallableModule = (
name: string,
moduleOrFactory: Module | ((void) => Module)
) => void;
declare const registerCallableModule: RegisterCallableModule;
declare export default typeof registerCallableModule;
"
`;

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

exports[`public API should not change unintentionally Libraries/Core/setUpBatchedBridge.js 1`] = `""`;
Expand Down Expand Up @@ -9164,6 +9175,7 @@ declare export default class EventEmitter<TEventToArgsMap: { ... }>
exports[`public API should not change unintentionally index.js 1`] = `
"export type HostComponent<T> = _HostComponentInternal<T>;
declare module.exports: {
get registerCallableModule(): RegisterCallableModule,
get AccessibilityInfo(): AccessibilityInfo,
get ActivityIndicator(): ActivityIndicator,
get Button(): Button,
Expand Down
4 changes: 4 additions & 0 deletions packages/react-native/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import typeof TouchableNativeFeedback from './Libraries/Components/Touchable/Tou
import typeof TouchableOpacity from './Libraries/Components/Touchable/TouchableOpacity';
import typeof TouchableWithoutFeedback from './Libraries/Components/Touchable/TouchableWithoutFeedback';
import typeof View from './Libraries/Components/View/View';
import typeof RegisterCallableModule from './Libraries/Core/registerCallableModule';
import typeof NativeEventEmitter from './Libraries/EventEmitter/NativeEventEmitter';
import typeof RCTDeviceEventEmitter from './Libraries/EventEmitter/RCTDeviceEventEmitter';
import typeof RCTNativeAppEventEmitter from './Libraries/EventEmitter/RCTNativeAppEventEmitter';
Expand Down Expand Up @@ -97,6 +98,9 @@ const invariant = require('invariant');
export type HostComponent<T> = _HostComponentInternal<T>;

module.exports = {
get registerCallableModule(): RegisterCallableModule {
return require('./Libraries/Core/registerCallableModule').default;
},
// Components
get AccessibilityInfo(): AccessibilityInfo {
return require('./Libraries/Components/AccessibilityInfo/AccessibilityInfo')
Expand Down

0 comments on commit 2372461

Please sign in to comment.