TurboModuleRegistry.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /**
  2. * Copyright (c) Facebook, Inc. and its 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
  8. * @format
  9. */
  10. 'use strict';
  11. const NativeModules = require('../BatchedBridge/NativeModules');
  12. import type {TurboModule} from './RCTExport';
  13. import invariant from 'invariant';
  14. const turboModuleProxy = global.__turboModuleProxy;
  15. export function get<T: TurboModule>(name: string): ?T {
  16. // Bridgeless mode requires TurboModules
  17. if (!global.RN$Bridgeless) {
  18. // Backward compatibility layer during migration.
  19. const legacyModule = NativeModules[name];
  20. if (legacyModule != null) {
  21. return ((legacyModule: any): T);
  22. }
  23. }
  24. if (turboModuleProxy != null) {
  25. const module: ?T = turboModuleProxy(name);
  26. return module;
  27. }
  28. return null;
  29. }
  30. export function getEnforcing<T: TurboModule>(name: string): T {
  31. const module = get(name);
  32. invariant(
  33. module != null,
  34. `TurboModuleRegistry.getEnforcing(...): '${name}' could not be found. ` +
  35. 'Verify that a module by this name is registered in the native binary.',
  36. );
  37. return module;
  38. }