react-native-nitro-modules 0.10.0 → 0.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (111) hide show
  1. package/NitroModules.podspec +2 -0
  2. package/README.md +1 -1
  3. package/android/CMakeLists.txt +3 -0
  4. package/android/build.gradle +26 -2
  5. package/android/src/main/cpp/JNIOnLoad.cpp +5 -5
  6. package/android/src/main/cpp/turbomodule/JNitroModules.cpp +39 -0
  7. package/android/src/main/cpp/turbomodule/JNitroModules.hpp +37 -0
  8. package/android/src/main/java/com/margelo/nitro/NitroModules.kt +69 -0
  9. package/android/src/main/java/com/margelo/nitro/NitroModulesPackage.kt +34 -0
  10. package/android/src/main/java/com/margelo/nitro/core/HybridObject.kt +6 -2
  11. package/android/src/newarch/NitroModulesSpec.kt +6 -0
  12. package/android/src/oldarch/NitroModulesSpec.kt +9 -0
  13. package/cpp/core/HybridFunction.hpp +12 -3
  14. package/cpp/entrypoint/HybridNitroModulesProxy.cpp +55 -0
  15. package/cpp/entrypoint/HybridNitroModulesProxy.hpp +48 -0
  16. package/cpp/entrypoint/InstallNitro.cpp +28 -0
  17. package/cpp/entrypoint/InstallNitro.hpp +41 -0
  18. package/cpp/jsi/JSIConverter+HostObject.hpp +73 -0
  19. package/cpp/jsi/JSIConverter+HybridObject.hpp +1 -1
  20. package/cpp/jsi/JSIConverter.hpp +1 -0
  21. package/ios/core/HybridObjectSpec.swift +1 -1
  22. package/ios/platform/NitroLogger.mm +0 -1
  23. package/ios/turbomodule/NativeNitroModules+NewArch.mm +67 -0
  24. package/ios/turbomodule/NativeNitroModules+OldArch.mm +68 -0
  25. package/ios/turbomodule/NativeNitroModules.h +22 -0
  26. package/lib/BoxedHybridObject.d.ts +12 -0
  27. package/lib/BoxedHybridObject.js +1 -0
  28. package/lib/ModuleNotFoundError.js +3 -13
  29. package/lib/NitroModules.d.ts +1 -83
  30. package/lib/NitroModules.js +2 -94
  31. package/lib/NitroModulesProxy.d.ts +58 -0
  32. package/lib/NitroModulesProxy.js +1 -0
  33. package/lib/commonjs/BoxedHybridObject.js +6 -0
  34. package/lib/commonjs/BoxedHybridObject.js.map +1 -0
  35. package/lib/commonjs/ModuleNotFoundError.js +3 -15
  36. package/lib/commonjs/ModuleNotFoundError.js.map +1 -1
  37. package/lib/commonjs/NitroModules.js +11 -100
  38. package/lib/commonjs/NitroModules.js.map +1 -1
  39. package/lib/commonjs/NitroModulesProxy.js +6 -0
  40. package/lib/commonjs/NitroModulesProxy.js.map +1 -0
  41. package/lib/commonjs/turbomodule/NativeNitroModules.js +36 -0
  42. package/lib/commonjs/turbomodule/NativeNitroModules.js.map +1 -0
  43. package/lib/commonjs/turbomodule/NativeNitroModules.web.js +17 -0
  44. package/lib/commonjs/turbomodule/NativeNitroModules.web.js.map +1 -0
  45. package/lib/module/BoxedHybridObject.js +4 -0
  46. package/lib/module/BoxedHybridObject.js.map +1 -0
  47. package/lib/module/ModuleNotFoundError.js +3 -15
  48. package/lib/module/ModuleNotFoundError.js.map +1 -1
  49. package/lib/module/NitroModules.js +2 -100
  50. package/lib/module/NitroModules.js.map +1 -1
  51. package/lib/module/NitroModulesProxy.js +4 -0
  52. package/lib/module/NitroModulesProxy.js.map +1 -0
  53. package/lib/module/turbomodule/NativeNitroModules.js +31 -0
  54. package/lib/module/turbomodule/NativeNitroModules.js.map +1 -0
  55. package/lib/{NitroModulesTurboModule.web.js → module/turbomodule/NativeNitroModules.web.js} +9 -1
  56. package/lib/module/turbomodule/NativeNitroModules.web.js.map +1 -0
  57. package/lib/tsconfig.tsbuildinfo +1 -1
  58. package/lib/turbomodule/NativeNitroModules.d.ts +7 -0
  59. package/lib/turbomodule/NativeNitroModules.js +27 -0
  60. package/lib/turbomodule/NativeNitroModules.web.d.ts +2 -0
  61. package/lib/turbomodule/NativeNitroModules.web.js +9 -0
  62. package/lib/typescript/AnyMap.d.ts +20 -0
  63. package/lib/typescript/BoxedHybridObject.d.ts +13 -0
  64. package/lib/typescript/BoxedHybridObject.d.ts.map +1 -0
  65. package/lib/typescript/ModuleNotFoundError.d.ts +7 -0
  66. package/lib/typescript/ModuleNotFoundError.d.ts.map +1 -1
  67. package/lib/typescript/NitroModules.d.ts +1 -83
  68. package/lib/typescript/NitroModules.d.ts.map +1 -1
  69. package/lib/typescript/NitroModulesProxy.d.ts +59 -0
  70. package/lib/typescript/NitroModulesProxy.d.ts.map +1 -0
  71. package/lib/typescript/__tests__/index.test.d.ts +1 -0
  72. package/lib/typescript/index.d.ts +4 -0
  73. package/lib/typescript/turbomodule/NativeNitroModules.d.ts +8 -0
  74. package/lib/typescript/turbomodule/NativeNitroModules.d.ts.map +1 -0
  75. package/lib/typescript/turbomodule/NativeNitroModules.web.d.ts +3 -0
  76. package/lib/typescript/turbomodule/NativeNitroModules.web.d.ts.map +1 -0
  77. package/package.json +9 -1
  78. package/src/BoxedHybridObject.ts +13 -0
  79. package/src/ModuleNotFoundError.ts +3 -19
  80. package/src/NitroModules.ts +2 -108
  81. package/src/NitroModulesProxy.ts +61 -0
  82. package/src/turbomodule/NativeNitroModules.ts +48 -0
  83. package/src/turbomodule/NativeNitroModules.web.ts +16 -0
  84. package/android/src/main/java/com/margelo/nitro/NitroModulesPackage.java +0 -30
  85. package/cpp/turbomodule/NativeNitroModules.cpp +0 -146
  86. package/cpp/turbomodule/NativeNitroModules.h +0 -8
  87. package/cpp/turbomodule/NativeNitroModules.hpp +0 -38
  88. package/cpp/turbomodule/RegisterNativeNitroModules.cpp +0 -33
  89. package/cpp/turbomodule/RegisterNativeNitroModules.hpp +0 -21
  90. package/ios/turbomodule/NitroModuleOnLoad.mm +0 -32
  91. package/lib/NativeNitroModules.d.ts +0 -16
  92. package/lib/NativeNitroModules.js +0 -22
  93. package/lib/NativeNitroModules.web.d.ts +0 -4
  94. package/lib/NativeNitroModules.web.js +0 -3
  95. package/lib/NitroModulesTurboModule.d.ts +0 -18
  96. package/lib/NitroModulesTurboModule.js +0 -23
  97. package/lib/NitroModulesTurboModule.web.d.ts +0 -1
  98. package/lib/commonjs/NitroModulesTurboModule.js +0 -34
  99. package/lib/commonjs/NitroModulesTurboModule.js.map +0 -1
  100. package/lib/commonjs/NitroModulesTurboModule.web.js +0 -11
  101. package/lib/commonjs/NitroModulesTurboModule.web.js.map +0 -1
  102. package/lib/module/NitroModulesTurboModule.js +0 -30
  103. package/lib/module/NitroModulesTurboModule.js.map +0 -1
  104. package/lib/module/NitroModulesTurboModule.web.js +0 -7
  105. package/lib/module/NitroModulesTurboModule.web.js.map +0 -1
  106. package/lib/typescript/NitroModulesTurboModule.d.ts +0 -19
  107. package/lib/typescript/NitroModulesTurboModule.d.ts.map +0 -1
  108. package/lib/typescript/NitroModulesTurboModule.web.d.ts +0 -2
  109. package/lib/typescript/NitroModulesTurboModule.web.d.ts.map +0 -1
  110. package/src/NitroModulesTurboModule.ts +0 -50
  111. package/src/NitroModulesTurboModule.web.ts +0 -7
@@ -0,0 +1,67 @@
1
+ //
2
+ // NativeNitroModules+NewArch.mm
3
+ // DoubleConversion
4
+ //
5
+ // Created by Marc Rousavy on 21.06.24.
6
+ //
7
+
8
+ #import "NativeNitroModules.h"
9
+
10
+ #ifdef RCT_NEW_ARCH_ENABLED
11
+
12
+ #import "CallInvokerDispatcher.hpp"
13
+ #import "InstallNitro.hpp"
14
+
15
+ #import <ReactCommon/CallInvoker.h>
16
+ #import <ReactCommon/RCTTurboModuleWithJSIBindings.h>
17
+
18
+ using namespace facebook;
19
+ using namespace margelo;
20
+
21
+ // Make NativeNitroModules comply to RCTTurboModuleWithJSIBindings
22
+ @interface NativeNitroModules () <RCTTurboModuleWithJSIBindings>
23
+ @end
24
+
25
+ /**
26
+ * NativeNitroModules implementation for the new architecture.
27
+ * This uses `installJSIBindingsWithRuntime:` to install the `global.NitroModulesProxy` into the JS Runtime.
28
+ */
29
+ @implementation NativeNitroModules {
30
+ bool _didInstall;
31
+ std::weak_ptr<react::CallInvoker> _callInvoker;
32
+ }
33
+ RCT_EXPORT_MODULE(NitroModules)
34
+
35
+ - (void)installJSIBindingsWithRuntime:(jsi::Runtime&)runtime {
36
+ // 1. Get CallInvoker we cached statically
37
+ auto callInvoker = _callInvoker.lock();
38
+ if (callInvoker == nullptr) {
39
+ throw std::runtime_error("Cannot install global.NitroModulesProxy - CallInvoker was null!");
40
+ }
41
+
42
+ // 2. Wrap CallInvoker as Dispatcher
43
+ auto dispatcher = std::make_shared<nitro::CallInvokerDispatcher>(callInvoker);
44
+
45
+ // 3. Install Nitro
46
+ nitro::install(runtime, dispatcher);
47
+ _didInstall = true;
48
+ }
49
+
50
+ - (NSString*)install {
51
+ if (_didInstall) {
52
+ // installJSIBindingsWithRuntime ran successfully.
53
+ return nil;
54
+ } else {
55
+ return @"installJSIBindingsWithRuntime: was not called - JSI Bindings could not be installed!";
56
+ }
57
+ }
58
+
59
+ - (std::shared_ptr<react::TurboModule>)getTurboModule:(const react::ObjCTurboModule::InitParams&)params {
60
+ // Cache the CallInvoker statically (weak) - we use it later in `installJSIBindingsWithRuntime:`.
61
+ _callInvoker = params.jsInvoker;
62
+ return std::make_shared<react::NativeNitroModulesSpecJSI>(params);
63
+ }
64
+
65
+ @end
66
+
67
+ #endif
@@ -0,0 +1,68 @@
1
+ //
2
+ // NativeNitroModules+OldArch.mm
3
+ // DoubleConversion
4
+ //
5
+ // Created by Marc Rousavy on 21.06.24.
6
+ //
7
+
8
+ #import "NativeNitroModules.h"
9
+
10
+ #ifndef RCT_NEW_ARCH_ENABLED
11
+
12
+ #import "CallInvokerDispatcher.hpp"
13
+ #import "InstallNitro.hpp"
14
+
15
+ #import <React/RCTBridge+Private.h>
16
+ #import <React/RCTBridge.h>
17
+
18
+ using namespace facebook;
19
+ using namespace margelo;
20
+
21
+ // forward-declaration (private API)
22
+ @interface RCTBridge (JSIRuntime)
23
+ - (void*)runtime;
24
+ - (std::shared_ptr<react::CallInvoker>)jsCallInvoker;
25
+ @end
26
+
27
+ /**
28
+ * NativeNitroModules implementation for the old architecture.
29
+ * This uses `RCTBridge` to grab the `jsi::Runtime` and `react::CallInvoker`.
30
+ */
31
+ @implementation NativeNitroModules
32
+ RCT_EXPORT_MODULE(NitroModules)
33
+
34
+ RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(install) {
35
+ try {
36
+ // 1. Cast RCTBridge to a RCTCxxBridge (ObjC)
37
+ RCTCxxBridge* cxxBridge = (RCTCxxBridge*)RCTBridge.currentBridge;
38
+ if (!cxxBridge) {
39
+ return @"RCTBridge is not a RCTCxxBridge!";
40
+ }
41
+
42
+ // 2. Access jsi::Runtime and cast from void*
43
+ jsi::Runtime* runtime = reinterpret_cast<jsi::Runtime*>(cxxBridge.runtime);
44
+ if (!runtime) {
45
+ return @"jsi::Runtime on RCTCxxBridge was null!";
46
+ }
47
+
48
+ // 3. Access react::CallInvoker
49
+ std::shared_ptr<react::CallInvoker> callInvoker = cxxBridge.jsCallInvoker;
50
+ if (!callInvoker) {
51
+ return @"react::CallInvoker on RCTCxxBridge was null!";
52
+ }
53
+
54
+ // 4. Wrap react::CallInvoker in nitro::Dispatcher
55
+ auto dispatcher = std::make_shared<nitro::CallInvokerDispatcher>(callInvoker);
56
+
57
+ // 5. Install Nitro
58
+ nitro::install(*runtime, dispatcher);
59
+ return nil;
60
+ } catch (std::runtime_error& error) {
61
+ // ?. Any C++ error occurred (probably in nitro::install()?)
62
+ return [NSString stringWithCString:error.what() encoding:kCFStringEncodingUTF8];
63
+ }
64
+ }
65
+
66
+ @end
67
+
68
+ #endif
@@ -0,0 +1,22 @@
1
+ //
2
+ // NativeNitroModules.h
3
+ // NitroModules
4
+ //
5
+ // Created by Marc Rousavy on 07.10.24.
6
+ //
7
+
8
+ #ifdef RCT_NEW_ARCH_ENABLED
9
+
10
+ // New Architecture uses the Codegen'd Spec (TurboModule)
11
+ #import "NitroModulesSpec.h"
12
+ @interface NativeNitroModules : NSObject <NativeNitroModulesSpec>
13
+ @end
14
+
15
+ #else
16
+
17
+ // Old Architecture is an untyped RCTBridgeModule
18
+ #import <React/RCTBridgeModule.h>
19
+ @interface NativeNitroModules : NSObject <RCTBridgeModule>
20
+ @end
21
+
22
+ #endif
@@ -0,0 +1,12 @@
1
+ import type { HybridObject } from './HybridObject';
2
+ /**
3
+ * Represents a boxed {@linkcode HybridObject} that can later be unboxed again.
4
+ * This is implemented as a `jsi::HostObject`.
5
+ */
6
+ export interface BoxedHybridObject<T extends HybridObject> {
7
+ /**
8
+ * Unboxes the {@linkcode HybridObject}.
9
+ * This can be called from a different Runtime than the one it was boxed in.
10
+ */
11
+ unbox(): T;
12
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -20,26 +20,16 @@ function getFrameworkType() {
20
20
  }
21
21
  export class ModuleNotFoundError extends Error {
22
22
  constructor(cause) {
23
- // TurboModule not found, something went wrong!
24
- if (global.__turboModuleProxy == null) {
25
- // TurboModules are not available/new arch is not enabled.
26
- const message = 'Failed to get NitroModules: NitroModules require the new architecture to be enabled!';
27
- const suggestions = [];
28
- suggestions.push('Enable the new architecture in your app to use NitroModules. (See https://github.com/reactwg/react-native-new-architecture/blob/main/docs/enable-apps.md)');
29
- const error = messageWithSuggestions(message, suggestions);
30
- super(error, { cause: cause });
31
- return;
32
- }
33
23
  const framework = getFrameworkType();
34
24
  if (framework === 'expo-go') {
35
25
  super('NitroModules are not supported in Expo Go! Use EAS (`expo prebuild`) or eject to a bare workflow instead.');
36
26
  return;
37
27
  }
38
- const message = 'Failed to get NitroModules: The native "NitroModules" TurboModule could not be found.';
28
+ const message = 'Failed to get NitroModules: The native "NitroModules" Turbo/Native-Module could not be found.';
39
29
  const suggestions = [];
40
30
  suggestions.push('Make sure react-native-nitro-modules/NitroModules is correctly autolinked (run `npx react-native config` to verify)');
41
- suggestions.push('Make sure you enabled the new architecture (TurboModules) and CodeGen properly generated the "NativeNitroModules"/NitroModulesCxx specs. See https://github.com/reactwg/react-native-new-architecture/blob/main/docs/enable-apps.md');
42
- suggestions.push('Make sure you are using react-native 0.74.0 or higher, because NitroModules are built with C++ TurboModules.');
31
+ suggestions.push('Make sure you enabled the new architecture (TurboModules) and CodeGen properly generated the "NativeNitroModules"/NitroModules specs. See https://github.com/reactwg/react-native-new-architecture/blob/main/docs/enable-apps.md');
32
+ suggestions.push('Make sure you are using react-native 0.74.0 or higher.');
43
33
  suggestions.push('Make sure you rebuilt the app.');
44
34
  if (framework === 'expo') {
45
35
  suggestions.push('Make sure you ran `expo prebuild`.');
@@ -1,83 +1 @@
1
- import type { HybridObject } from './HybridObject';
2
- /**
3
- * Represents a boxed {@linkcode HybridObject} that can later be unboxed again.
4
- * This is implemented as a `jsi::HostObject`.
5
- */
6
- export interface BoxedHybridObject<T extends HybridObject> {
7
- /**
8
- * Unboxes the {@linkcode HybridObject}.
9
- * This can be called from a different Runtime than the one it was boxed in.
10
- */
11
- unbox(): T;
12
- }
13
- /**
14
- * A lazy proxy for initializing Nitro Modules HybridObjects.
15
- */
16
- export declare const NitroModules: {
17
- /**
18
- * Create a new instance of the `HybridObject` {@linkcode T}.
19
- *
20
- * {@linkcode T} has to be registered beforehand under the name {@linkcode name}
21
- * in the native Nitro Modules `HybridObjectRegistry`.
22
- *
23
- * @param name The name of the `HybridObject` under which it was registered at.
24
- * @returns An instance of {@linkcode T}
25
- * @throws an Error if {@linkcode T} has not been registered under the name {@linkcode name}.
26
- */
27
- createHybridObject<T extends HybridObject<any>>(name: string): T;
28
- /**
29
- * Get a list of all registered Hybrid Objects.
30
- */
31
- getAllHybridObjectNames(): string[];
32
- /**
33
- * Returns whether a HybridObject under the given {@linkcode name} is registered, or not.
34
- */
35
- hasHybridObject(name: string): boolean;
36
- /**
37
- * Returns whether the given {@linkcode object} has a `NativeState` or not.
38
- *
39
- * This can be a quick way to check if an object is a valid {@linkcode HybridObject},
40
- * and has not yet been disposed.
41
- * @example
42
- * ```ts
43
- * const someObject = NitroModules.createHybridObject<Some>('Some')
44
- * console.log(NitroModules.hasNativeState(someObject)) // -> true
45
- * someObject.dispose()
46
- * console.log(NitroModules.hasNativeState(someObject)) // -> false
47
- * ```
48
- */
49
- hasNativeState(object: object): boolean;
50
- /**
51
- * Forcefully removes the `NativeState` of the given {@linkcode object}.
52
- */
53
- removeNativeState(object: object): void;
54
- /**
55
- * Gets the current build type configuration as defined in the `NITRO_DEBUG`
56
- * preprocessor flag.
57
- */
58
- readonly buildType: "debug" | "release";
59
- /**
60
- * Boxes the given {@linkcode hybridObject} into a {@linkcode BoxedHybridObject<T>}, which can
61
- * later be unboxed in a separate Runtime.
62
- *
63
- * While Nitro is runtime-agnostic and all `HybridObject`s can be used from a any Runtime,
64
- * some threading/worklet libraries (like [react-native-worklets-core](https://github.com/margelo/react-native-worklets-core))
65
- * do not yet support copying over `HybridObject`s as they use newer JSI APIs like `jsi::NativeState`.
66
- *
67
- * While those APIs are not yet available, you can still use every Nitro Hybrid Object in a separate
68
- * Runtime/Worklet context by just boxing it yourself:
69
- *
70
- * @example
71
- * ```ts
72
- * const something = NitroModules.createHybridObject<Something>('Something')
73
- * const boxed = NitroModules.box(something)
74
- * const context = Worklets.createContext('DummyContext')
75
- * context.runAsync(() => {
76
- * 'worklet'
77
- * const unboxed = boxed.unbox()
78
- * console.log(unboxed.name) // --> "Something"
79
- * })
80
- * ```
81
- */
82
- box<T extends HybridObject>(hybridObject: T): BoxedHybridObject<T>;
83
- };
1
+ export * from './turbomodule/NativeNitroModules';
@@ -1,94 +1,2 @@
1
- import { getNativeNitroModules } from './NitroModulesTurboModule';
2
- /**
3
- * A lazy proxy for initializing Nitro Modules HybridObjects.
4
- */
5
- export const NitroModules = {
6
- /**
7
- * Create a new instance of the `HybridObject` {@linkcode T}.
8
- *
9
- * {@linkcode T} has to be registered beforehand under the name {@linkcode name}
10
- * in the native Nitro Modules `HybridObjectRegistry`.
11
- *
12
- * @param name The name of the `HybridObject` under which it was registered at.
13
- * @returns An instance of {@linkcode T}
14
- * @throws an Error if {@linkcode T} has not been registered under the name {@linkcode name}.
15
- */
16
- createHybridObject(name) {
17
- const nitro = getNativeNitroModules();
18
- const instance = nitro.createHybridObject(name);
19
- return instance;
20
- },
21
- /**
22
- * Get a list of all registered Hybrid Objects.
23
- */
24
- getAllHybridObjectNames() {
25
- const nitro = getNativeNitroModules();
26
- return nitro.getAllHybridObjectNames();
27
- },
28
- /**
29
- * Returns whether a HybridObject under the given {@linkcode name} is registered, or not.
30
- */
31
- hasHybridObject(name) {
32
- const nitro = getNativeNitroModules();
33
- return nitro.hasHybridObject(name);
34
- },
35
- /**
36
- * Returns whether the given {@linkcode object} has a `NativeState` or not.
37
- *
38
- * This can be a quick way to check if an object is a valid {@linkcode HybridObject},
39
- * and has not yet been disposed.
40
- * @example
41
- * ```ts
42
- * const someObject = NitroModules.createHybridObject<Some>('Some')
43
- * console.log(NitroModules.hasNativeState(someObject)) // -> true
44
- * someObject.dispose()
45
- * console.log(NitroModules.hasNativeState(someObject)) // -> false
46
- * ```
47
- */
48
- hasNativeState(object) {
49
- const nitro = getNativeNitroModules();
50
- return nitro.hasNativeState(object);
51
- },
52
- /**
53
- * Forcefully removes the `NativeState` of the given {@linkcode object}.
54
- */
55
- removeNativeState(object) {
56
- const nitro = getNativeNitroModules();
57
- nitro.removeNativeState(object);
58
- },
59
- /**
60
- * Gets the current build type configuration as defined in the `NITRO_DEBUG`
61
- * preprocessor flag.
62
- */
63
- get buildType() {
64
- const nitro = getNativeNitroModules();
65
- return nitro.buildType;
66
- },
67
- /**
68
- * Boxes the given {@linkcode hybridObject} into a {@linkcode BoxedHybridObject<T>}, which can
69
- * later be unboxed in a separate Runtime.
70
- *
71
- * While Nitro is runtime-agnostic and all `HybridObject`s can be used from a any Runtime,
72
- * some threading/worklet libraries (like [react-native-worklets-core](https://github.com/margelo/react-native-worklets-core))
73
- * do not yet support copying over `HybridObject`s as they use newer JSI APIs like `jsi::NativeState`.
74
- *
75
- * While those APIs are not yet available, you can still use every Nitro Hybrid Object in a separate
76
- * Runtime/Worklet context by just boxing it yourself:
77
- *
78
- * @example
79
- * ```ts
80
- * const something = NitroModules.createHybridObject<Something>('Something')
81
- * const boxed = NitroModules.box(something)
82
- * const context = Worklets.createContext('DummyContext')
83
- * context.runAsync(() => {
84
- * 'worklet'
85
- * const unboxed = boxed.unbox()
86
- * console.log(unboxed.name) // --> "Something"
87
- * })
88
- * ```
89
- */
90
- box(hybridObject) {
91
- const nitro = getNativeNitroModules();
92
- return nitro.box(hybridObject);
93
- },
94
- };
1
+ // Re-exports the platform specific `NitroModulesProxy` (or a stub-implementation if not found)
2
+ export * from './turbomodule/NativeNitroModules';
@@ -0,0 +1,58 @@
1
+ import type { HybridObject } from './HybridObject';
2
+ import type { BoxedHybridObject } from './BoxedHybridObject';
3
+ /**
4
+ * The Proxy class that acts as a main entry point for Nitro Modules in React Native.
5
+ *
6
+ * This is a `HybridObject` on the native side as well, and is expected to be
7
+ * installed into the runtime's `global` via the NativeModule/TurboModule's `install()` function.
8
+ */
9
+ export interface NitroModulesProxy extends HybridObject {
10
+ /**
11
+ * Create a new instance of the `HybridObject` {@linkcode T}.
12
+ *
13
+ * {@linkcode T} has to be registered beforehand under the name {@linkcode name}
14
+ * in the native Nitro Modules `HybridObjectRegistry`.
15
+ *
16
+ * @param name The name of the `HybridObject` under which it was registered at.
17
+ * @returns An instance of {@linkcode T}
18
+ * @throws an Error if {@linkcode T} has not been registered under the name {@linkcode name}.
19
+ */
20
+ createHybridObject<T extends HybridObject>(name: string): T;
21
+ /**
22
+ * Returns whether a HybridObject under the given {@linkcode name} is registered, or not.
23
+ */
24
+ hasHybridObject(name: string): boolean;
25
+ /**
26
+ * Get a list of all registered Hybrid Objects.
27
+ */
28
+ getAllHybridObjectNames(): string[];
29
+ /**
30
+ * Gets the current build type configuration as defined in the `NITRO_DEBUG`
31
+ * preprocessor flag.
32
+ */
33
+ buildType: 'debug' | 'release';
34
+ /**
35
+ * Boxes the given {@linkcode hybridObject} into a {@linkcode BoxedHybridObject<T>}, which can
36
+ * later be unboxed in a separate Runtime.
37
+ *
38
+ * While Nitro is runtime-agnostic and all `HybridObject`s can be used from a any Runtime,
39
+ * some threading/worklet libraries (like [react-native-worklets-core](https://github.com/margelo/react-native-worklets-core))
40
+ * do not yet support copying over `HybridObject`s as they use newer JSI APIs like `jsi::NativeState`.
41
+ *
42
+ * While those APIs are not yet available, you can still use every Nitro Hybrid Object in a separate
43
+ * Runtime/Worklet context by just boxing it yourself:
44
+ *
45
+ * @example
46
+ * ```ts
47
+ * const something = NitroModules.createHybridObject<Something>('Something')
48
+ * const boxed = NitroModules.box(something)
49
+ * const context = Worklets.createContext('DummyContext')
50
+ * context.runAsync(() => {
51
+ * 'worklet'
52
+ * const unboxed = boxed.unbox()
53
+ * console.log(unboxed.name) // --> "Something"
54
+ * })
55
+ * ```
56
+ */
57
+ box<T extends HybridObject>(obj: T): BoxedHybridObject<T>;
58
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ //# sourceMappingURL=BoxedHybridObject.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":[],"sourceRoot":"../../src","sources":["BoxedHybridObject.ts"],"mappings":"","ignoreList":[]}
@@ -25,28 +25,16 @@ function getFrameworkType() {
25
25
  }
26
26
  class ModuleNotFoundError extends Error {
27
27
  constructor(cause) {
28
- // TurboModule not found, something went wrong!
29
- if (global.__turboModuleProxy == null) {
30
- // TurboModules are not available/new arch is not enabled.
31
- const message = 'Failed to get NitroModules: NitroModules require the new architecture to be enabled!';
32
- const suggestions = [];
33
- suggestions.push('Enable the new architecture in your app to use NitroModules. (See https://github.com/reactwg/react-native-new-architecture/blob/main/docs/enable-apps.md)');
34
- const error = messageWithSuggestions(message, suggestions);
35
- super(error, {
36
- cause: cause
37
- });
38
- return;
39
- }
40
28
  const framework = getFrameworkType();
41
29
  if (framework === 'expo-go') {
42
30
  super('NitroModules are not supported in Expo Go! Use EAS (`expo prebuild`) or eject to a bare workflow instead.');
43
31
  return;
44
32
  }
45
- const message = 'Failed to get NitroModules: The native "NitroModules" TurboModule could not be found.';
33
+ const message = 'Failed to get NitroModules: The native "NitroModules" Turbo/Native-Module could not be found.';
46
34
  const suggestions = [];
47
35
  suggestions.push('Make sure react-native-nitro-modules/NitroModules is correctly autolinked (run `npx react-native config` to verify)');
48
- suggestions.push('Make sure you enabled the new architecture (TurboModules) and CodeGen properly generated the "NativeNitroModules"/NitroModulesCxx specs. See https://github.com/reactwg/react-native-new-architecture/blob/main/docs/enable-apps.md');
49
- suggestions.push('Make sure you are using react-native 0.74.0 or higher, because NitroModules are built with C++ TurboModules.');
36
+ suggestions.push('Make sure you enabled the new architecture (TurboModules) and CodeGen properly generated the "NativeNitroModules"/NitroModules specs. See https://github.com/reactwg/react-native-new-architecture/blob/main/docs/enable-apps.md');
37
+ suggestions.push('Make sure you are using react-native 0.74.0 or higher.');
50
38
  suggestions.push('Make sure you rebuilt the app.');
51
39
  if (framework === 'expo') {
52
40
  suggestions.push('Make sure you ran `expo prebuild`.');
@@ -1 +1 @@
1
- {"version":3,"names":["_reactNative","require","BULLET_POINT","messageWithSuggestions","message","suggestions","join","getFrameworkType","ExpoConstants","NativeModules","NativeUnimoduleProxy","modulesConstants","ExponentConstants","appOwnership","ModuleNotFoundError","Error","constructor","cause","global","__turboModuleProxy","push","error","framework","Platform","OS","exports"],"sourceRoot":"../../src","sources":["ModuleNotFoundError.ts"],"mappings":";;;;;;AAAA,IAAAA,YAAA,GAAAC,OAAA;AAOA,MAAMC,YAAY,GAAG,MAAM;AAE3B,SAASC,sBAAsBA,CAC7BC,OAAe,EACfC,WAAqB,EACb;EACR,OAAOD,OAAO,GAAGF,YAAY,GAAGG,WAAW,CAACC,IAAI,CAACJ,YAAY,CAAC;AAChE;AAEA,SAASK,gBAAgBA,CAAA,EAAwC;EAC/D;EACA,MAAMC,aAAa,GACjBC,0BAAa,CAACC,oBAAoB,EAAEC,gBAAgB,EAAEC,iBAAiB;EACzE,IAAIJ,aAAa,IAAI,IAAI,EAAE;IACzB,IAAIA,aAAa,CAACK,YAAY,KAAK,MAAM,EAAE;MACzC;MACA,OAAO,SAAS;IAClB,CAAC,MAAM;MACL;MACA,OAAO,MAAM;IACf;EACF;EACA,OAAO,cAAc;AACvB;AAEO,MAAMC,mBAAmB,SAASC,KAAK,CAAC;EAC7CC,WAAWA,CAACC,KAAe,EAAE;IAC3B;IACA,IAAIC,MAAM,CAACC,kBAAkB,IAAI,IAAI,EAAE;MACrC;MACA,MAAMf,OAAO,GACX,sFAAsF;MACxF,MAAMC,WAAqB,GAAG,EAAE;MAChCA,WAAW,CAACe,IAAI,CACd,2JACF,CAAC;MACD,MAAMC,KAAK,GAAGlB,sBAAsB,CAACC,OAAO,EAAEC,WAAW,CAAC;MAC1D,KAAK,CAACgB,KAAK,EAAE;QAAEJ,KAAK,EAAEA;MAAM,CAAC,CAAC;MAC9B;IACF;IAEA,MAAMK,SAAS,GAAGf,gBAAgB,CAAC,CAAC;IACpC,IAAIe,SAAS,KAAK,SAAS,EAAE;MAC3B,KAAK,CACH,2GACF,CAAC;MACD;IACF;IAEA,MAAMlB,OAAO,GACX,uFAAuF;IACzF,MAAMC,WAAqB,GAAG,EAAE;IAChCA,WAAW,CAACe,IAAI,CACd,qHACF,CAAC;IACDf,WAAW,CAACe,IAAI,CACd,qOACF,CAAC;IACDf,WAAW,CAACe,IAAI,CACd,8GACF,CAAC;IACDf,WAAW,CAACe,IAAI,CAAC,gCAAgC,CAAC;IAClD,IAAIE,SAAS,KAAK,MAAM,EAAE;MACxBjB,WAAW,CAACe,IAAI,CAAC,oCAAoC,CAAC;IACxD;IACA,QAAQG,qBAAQ,CAACC,EAAE;MACjB,KAAK,KAAK;MACV,KAAK,OAAO;QACVnB,WAAW,CAACe,IAAI,CACd,wDACF,CAAC;QACD;MACF,KAAK,SAAS;QACZf,WAAW,CAACe,IAAI,CAAC,6BAA6B,CAAC;QAC/C;MACF;QACE,MAAM,IAAIL,KAAK,CAAC,yCAAyCQ,qBAAQ,CAACC,EAAE,GAAG,CAAC;IAC5E;IAEA,MAAMH,KAAK,GAAGlB,sBAAsB,CAACC,OAAO,EAAEC,WAAW,CAAC;IAC1D,KAAK,CAACgB,KAAK,EAAE;MAAEJ,KAAK,EAAEA;IAAM,CAAC,CAAC;EAChC;AACF;AAACQ,OAAA,CAAAX,mBAAA,GAAAA,mBAAA","ignoreList":[]}
1
+ {"version":3,"names":["_reactNative","require","BULLET_POINT","messageWithSuggestions","message","suggestions","join","getFrameworkType","ExpoConstants","NativeModules","NativeUnimoduleProxy","modulesConstants","ExponentConstants","appOwnership","ModuleNotFoundError","Error","constructor","cause","framework","push","Platform","OS","error","exports"],"sourceRoot":"../../src","sources":["ModuleNotFoundError.ts"],"mappings":";;;;;;AAAA,IAAAA,YAAA,GAAAC,OAAA;AAOA,MAAMC,YAAY,GAAG,MAAM;AAE3B,SAASC,sBAAsBA,CAC7BC,OAAe,EACfC,WAAqB,EACb;EACR,OAAOD,OAAO,GAAGF,YAAY,GAAGG,WAAW,CAACC,IAAI,CAACJ,YAAY,CAAC;AAChE;AAEA,SAASK,gBAAgBA,CAAA,EAAwC;EAC/D;EACA,MAAMC,aAAa,GACjBC,0BAAa,CAACC,oBAAoB,EAAEC,gBAAgB,EAAEC,iBAAiB;EACzE,IAAIJ,aAAa,IAAI,IAAI,EAAE;IACzB,IAAIA,aAAa,CAACK,YAAY,KAAK,MAAM,EAAE;MACzC;MACA,OAAO,SAAS;IAClB,CAAC,MAAM;MACL;MACA,OAAO,MAAM;IACf;EACF;EACA,OAAO,cAAc;AACvB;AAEO,MAAMC,mBAAmB,SAASC,KAAK,CAAC;EAC7CC,WAAWA,CAACC,KAAe,EAAE;IAC3B,MAAMC,SAAS,GAAGX,gBAAgB,CAAC,CAAC;IACpC,IAAIW,SAAS,KAAK,SAAS,EAAE;MAC3B,KAAK,CACH,2GACF,CAAC;MACD;IACF;IAEA,MAAMd,OAAO,GACX,+FAA+F;IACjG,MAAMC,WAAqB,GAAG,EAAE;IAChCA,WAAW,CAACc,IAAI,CACd,qHACF,CAAC;IACDd,WAAW,CAACc,IAAI,CACd,kOACF,CAAC;IACDd,WAAW,CAACc,IAAI,CAAC,wDAAwD,CAAC;IAC1Ed,WAAW,CAACc,IAAI,CAAC,gCAAgC,CAAC;IAClD,IAAID,SAAS,KAAK,MAAM,EAAE;MACxBb,WAAW,CAACc,IAAI,CAAC,oCAAoC,CAAC;IACxD;IACA,QAAQC,qBAAQ,CAACC,EAAE;MACjB,KAAK,KAAK;MACV,KAAK,OAAO;QACVhB,WAAW,CAACc,IAAI,CACd,wDACF,CAAC;QACD;MACF,KAAK,SAAS;QACZd,WAAW,CAACc,IAAI,CAAC,6BAA6B,CAAC;QAC/C;MACF;QACE,MAAM,IAAIJ,KAAK,CAAC,yCAAyCK,qBAAQ,CAACC,EAAE,GAAG,CAAC;IAC5E;IAEA,MAAMC,KAAK,GAAGnB,sBAAsB,CAACC,OAAO,EAAEC,WAAW,CAAC;IAC1D,KAAK,CAACiB,KAAK,EAAE;MAAEL,KAAK,EAAEA;IAAM,CAAC,CAAC;EAChC;AACF;AAACM,OAAA,CAAAT,mBAAA,GAAAA,mBAAA","ignoreList":[]}
@@ -3,104 +3,15 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.NitroModules = void 0;
7
- var _NitroModulesTurboModule = require("./NitroModulesTurboModule");
8
- /**
9
- * Represents a boxed {@linkcode HybridObject} that can later be unboxed again.
10
- * This is implemented as a `jsi::HostObject`.
11
- */
12
-
13
- /**
14
- * A lazy proxy for initializing Nitro Modules HybridObjects.
15
- */
16
- const NitroModules = exports.NitroModules = {
17
- /**
18
- * Create a new instance of the `HybridObject` {@linkcode T}.
19
- *
20
- * {@linkcode T} has to be registered beforehand under the name {@linkcode name}
21
- * in the native Nitro Modules `HybridObjectRegistry`.
22
- *
23
- * @param name The name of the `HybridObject` under which it was registered at.
24
- * @returns An instance of {@linkcode T}
25
- * @throws an Error if {@linkcode T} has not been registered under the name {@linkcode name}.
26
- */
27
- createHybridObject(name) {
28
- const nitro = (0, _NitroModulesTurboModule.getNativeNitroModules)();
29
- const instance = nitro.createHybridObject(name);
30
- return instance;
31
- },
32
- /**
33
- * Get a list of all registered Hybrid Objects.
34
- */
35
- getAllHybridObjectNames() {
36
- const nitro = (0, _NitroModulesTurboModule.getNativeNitroModules)();
37
- return nitro.getAllHybridObjectNames();
38
- },
39
- /**
40
- * Returns whether a HybridObject under the given {@linkcode name} is registered, or not.
41
- */
42
- hasHybridObject(name) {
43
- const nitro = (0, _NitroModulesTurboModule.getNativeNitroModules)();
44
- return nitro.hasHybridObject(name);
45
- },
46
- /**
47
- * Returns whether the given {@linkcode object} has a `NativeState` or not.
48
- *
49
- * This can be a quick way to check if an object is a valid {@linkcode HybridObject},
50
- * and has not yet been disposed.
51
- * @example
52
- * ```ts
53
- * const someObject = NitroModules.createHybridObject<Some>('Some')
54
- * console.log(NitroModules.hasNativeState(someObject)) // -> true
55
- * someObject.dispose()
56
- * console.log(NitroModules.hasNativeState(someObject)) // -> false
57
- * ```
58
- */
59
- hasNativeState(object) {
60
- const nitro = (0, _NitroModulesTurboModule.getNativeNitroModules)();
61
- return nitro.hasNativeState(object);
62
- },
63
- /**
64
- * Forcefully removes the `NativeState` of the given {@linkcode object}.
65
- */
66
- removeNativeState(object) {
67
- const nitro = (0, _NitroModulesTurboModule.getNativeNitroModules)();
68
- nitro.removeNativeState(object);
69
- },
70
- /**
71
- * Gets the current build type configuration as defined in the `NITRO_DEBUG`
72
- * preprocessor flag.
73
- */
74
- get buildType() {
75
- const nitro = (0, _NitroModulesTurboModule.getNativeNitroModules)();
76
- return nitro.buildType;
77
- },
78
- /**
79
- * Boxes the given {@linkcode hybridObject} into a {@linkcode BoxedHybridObject<T>}, which can
80
- * later be unboxed in a separate Runtime.
81
- *
82
- * While Nitro is runtime-agnostic and all `HybridObject`s can be used from a any Runtime,
83
- * some threading/worklet libraries (like [react-native-worklets-core](https://github.com/margelo/react-native-worklets-core))
84
- * do not yet support copying over `HybridObject`s as they use newer JSI APIs like `jsi::NativeState`.
85
- *
86
- * While those APIs are not yet available, you can still use every Nitro Hybrid Object in a separate
87
- * Runtime/Worklet context by just boxing it yourself:
88
- *
89
- * @example
90
- * ```ts
91
- * const something = NitroModules.createHybridObject<Something>('Something')
92
- * const boxed = NitroModules.box(something)
93
- * const context = Worklets.createContext('DummyContext')
94
- * context.runAsync(() => {
95
- * 'worklet'
96
- * const unboxed = boxed.unbox()
97
- * console.log(unboxed.name) // --> "Something"
98
- * })
99
- * ```
100
- */
101
- box(hybridObject) {
102
- const nitro = (0, _NitroModulesTurboModule.getNativeNitroModules)();
103
- return nitro.box(hybridObject);
104
- }
105
- };
6
+ var _NativeNitroModules = require("./turbomodule/NativeNitroModules");
7
+ Object.keys(_NativeNitroModules).forEach(function (key) {
8
+ if (key === "default" || key === "__esModule") return;
9
+ if (key in exports && exports[key] === _NativeNitroModules[key]) return;
10
+ Object.defineProperty(exports, key, {
11
+ enumerable: true,
12
+ get: function () {
13
+ return _NativeNitroModules[key];
14
+ }
15
+ });
16
+ });
106
17
  //# sourceMappingURL=NitroModules.js.map
@@ -1 +1 @@
1
- {"version":3,"names":["_NitroModulesTurboModule","require","NitroModules","exports","createHybridObject","name","nitro","getNativeNitroModules","instance","getAllHybridObjectNames","hasHybridObject","hasNativeState","object","removeNativeState","buildType","box","hybridObject"],"sourceRoot":"../../src","sources":["NitroModules.ts"],"mappings":";;;;;;AAAA,IAAAA,wBAAA,GAAAC,OAAA;AAGA;AACA;AACA;AACA;;AASA;AACA;AACA;AACO,MAAMC,YAAY,GAAAC,OAAA,CAAAD,YAAA,GAAG;EAC1B;AACF;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;EACEE,kBAAkBA,CAA8BC,IAAY,EAAK;IAC/D,MAAMC,KAAK,GAAG,IAAAC,8CAAqB,EAAC,CAAC;IACrC,MAAMC,QAAQ,GAAGF,KAAK,CAACF,kBAAkB,CAACC,IAAI,CAAC;IAC/C,OAAOG,QAAQ;EACjB,CAAC;EACD;AACF;AACA;EACEC,uBAAuBA,CAAA,EAAa;IAClC,MAAMH,KAAK,GAAG,IAAAC,8CAAqB,EAAC,CAAC;IACrC,OAAOD,KAAK,CAACG,uBAAuB,CAAC,CAAC;EACxC,CAAC;EACD;AACF;AACA;EACEC,eAAeA,CAACL,IAAY,EAAW;IACrC,MAAMC,KAAK,GAAG,IAAAC,8CAAqB,EAAC,CAAC;IACrC,OAAOD,KAAK,CAACI,eAAe,CAACL,IAAI,CAAC;EACpC,CAAC;EACD;AACF;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;EACEM,cAAcA,CAACC,MAAc,EAAW;IACtC,MAAMN,KAAK,GAAG,IAAAC,8CAAqB,EAAC,CAAC;IACrC,OAAOD,KAAK,CAACK,cAAc,CAACC,MAAM,CAAC;EACrC,CAAC;EACD;AACF;AACA;EACEC,iBAAiBA,CAACD,MAAc,EAAQ;IACtC,MAAMN,KAAK,GAAG,IAAAC,8CAAqB,EAAC,CAAC;IACrCD,KAAK,CAACO,iBAAiB,CAACD,MAAM,CAAC;EACjC,CAAC;EACD;AACF;AACA;AACA;EACE,IAAIE,SAASA,CAAA,EAAwB;IACnC,MAAMR,KAAK,GAAG,IAAAC,8CAAqB,EAAC,CAAC;IACrC,OAAOD,KAAK,CAACQ,SAAS;EACxB,CAAC;EACD;AACF;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;EACEC,GAAGA,CAAyBC,YAAe,EAAwB;IACjE,MAAMV,KAAK,GAAG,IAAAC,8CAAqB,EAAC,CAAC;IACrC,OAAOD,KAAK,CAACS,GAAG,CAACC,YAAY,CAAC;EAChC;AACF,CAAC","ignoreList":[]}
1
+ {"version":3,"names":["_NativeNitroModules","require","Object","keys","forEach","key","exports","defineProperty","enumerable","get"],"sourceRoot":"../../src","sources":["NitroModules.ts"],"mappings":";;;;;AACA,IAAAA,mBAAA,GAAAC,OAAA;AAAAC,MAAA,CAAAC,IAAA,CAAAH,mBAAA,EAAAI,OAAA,WAAAC,GAAA;EAAA,IAAAA,GAAA,kBAAAA,GAAA;EAAA,IAAAA,GAAA,IAAAC,OAAA,IAAAA,OAAA,CAAAD,GAAA,MAAAL,mBAAA,CAAAK,GAAA;EAAAH,MAAA,CAAAK,cAAA,CAAAD,OAAA,EAAAD,GAAA;IAAAG,UAAA;IAAAC,GAAA,WAAAA,CAAA;MAAA,OAAAT,mBAAA,CAAAK,GAAA;IAAA;EAAA;AAAA","ignoreList":[]}
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ //# sourceMappingURL=NitroModulesProxy.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":[],"sourceRoot":"../../src","sources":["NitroModulesProxy.ts"],"mappings":"","ignoreList":[]}