2019-01-05 03:14:38 +03:00
|
|
|
/**
|
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
|
|
|
*
|
2020-07-22 19:43:45 +03:00
|
|
|
* @flow strict
|
2019-01-05 03:14:38 +03:00
|
|
|
* @format
|
|
|
|
*/
|
|
|
|
|
2019-06-13 02:14:51 +03:00
|
|
|
const NativeModules = require('../BatchedBridge/NativeModules');
|
2019-11-08 22:41:57 +03:00
|
|
|
import type {TurboModule} from './RCTExport';
|
2019-01-05 03:14:38 +03:00
|
|
|
import invariant from 'invariant';
|
|
|
|
|
2019-02-04 22:04:32 +03:00
|
|
|
const turboModuleProxy = global.__turboModuleProxy;
|
|
|
|
|
2021-01-15 06:11:29 +03:00
|
|
|
function requireModule<T: TurboModule>(name: string): ?T {
|
2019-06-18 01:08:21 +03:00
|
|
|
// Bridgeless mode requires TurboModules
|
|
|
|
if (!global.RN$Bridgeless) {
|
|
|
|
// Backward compatibility layer during migration.
|
|
|
|
const legacyModule = NativeModules[name];
|
|
|
|
if (legacyModule != null) {
|
2020-07-22 19:43:45 +03:00
|
|
|
return ((legacyModule: $FlowFixMe): T);
|
2019-06-18 01:08:21 +03:00
|
|
|
}
|
2019-01-05 03:14:38 +03:00
|
|
|
}
|
|
|
|
|
2019-06-14 23:50:45 +03:00
|
|
|
if (turboModuleProxy != null) {
|
2021-01-15 06:11:29 +03:00
|
|
|
const module: ?T = turboModuleProxy(name);
|
2019-06-14 23:50:45 +03:00
|
|
|
return module;
|
|
|
|
}
|
|
|
|
|
2019-02-04 22:04:32 +03:00
|
|
|
return null;
|
2019-01-05 03:14:38 +03:00
|
|
|
}
|
|
|
|
|
2020-08-05 01:47:30 +03:00
|
|
|
export function get<T: TurboModule>(name: string): ?T {
|
2021-01-15 06:11:29 +03:00
|
|
|
return requireModule<T>(name);
|
2020-08-05 01:47:30 +03:00
|
|
|
}
|
|
|
|
|
2019-06-04 12:04:40 +03:00
|
|
|
export function getEnforcing<T: TurboModule>(name: string): T {
|
2021-01-15 06:11:29 +03:00
|
|
|
const module = requireModule<T>(name);
|
2019-06-06 03:30:04 +03:00
|
|
|
invariant(
|
|
|
|
module != null,
|
|
|
|
`TurboModuleRegistry.getEnforcing(...): '${name}' could not be found. ` +
|
|
|
|
'Verify that a module by this name is registered in the native binary.',
|
|
|
|
);
|
2019-01-05 03:14:38 +03:00
|
|
|
return module;
|
|
|
|
}
|