react-native-nitro-modules 0.10.0 → 0.12.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 (120) hide show
  1. package/NitroModules.podspec +4 -0
  2. package/README.md +2 -2
  3. package/android/CMakeLists.txt +3 -0
  4. package/android/build.gradle +27 -3
  5. package/android/src/main/cpp/JNIOnLoad.cpp +5 -5
  6. package/android/src/main/cpp/platform/NitroLogger.cpp +1 -1
  7. package/android/src/main/cpp/turbomodule/JNitroModules.cpp +41 -0
  8. package/android/src/main/cpp/turbomodule/JNitroModules.hpp +37 -0
  9. package/android/src/main/java/com/margelo/nitro/NitroModules.kt +69 -0
  10. package/android/src/main/java/com/margelo/nitro/NitroModulesPackage.kt +34 -0
  11. package/android/src/main/java/com/margelo/nitro/core/HybridObject.kt +6 -2
  12. package/android/src/newarch/NitroModulesSpec.kt +6 -0
  13. package/android/src/oldarch/NitroModulesSpec.kt +9 -0
  14. package/cpp/core/HybridFunction.hpp +13 -4
  15. package/cpp/entrypoint/HybridNitroModulesProxy.cpp +55 -0
  16. package/cpp/entrypoint/HybridNitroModulesProxy.hpp +48 -0
  17. package/cpp/entrypoint/InstallNitro.cpp +28 -0
  18. package/cpp/entrypoint/InstallNitro.hpp +41 -0
  19. package/cpp/jsi/JSIConverter+ArrayBuffer.hpp +6 -6
  20. package/cpp/jsi/JSIConverter+HostObject.hpp +73 -0
  21. package/cpp/jsi/JSIConverter+HybridObject.hpp +1 -1
  22. package/cpp/jsi/JSIConverter+Promise.hpp +1 -0
  23. package/cpp/jsi/JSIConverter+Tuple.hpp +2 -2
  24. package/cpp/jsi/JSIConverter.hpp +1 -0
  25. package/cpp/prototype/HybridObjectPrototype.cpp +4 -3
  26. package/cpp/threading/CallInvokerDispatcher.hpp +5 -0
  27. package/cpp/utils/NitroDefines.hpp +8 -0
  28. package/cpp/utils/TypeInfo.hpp +40 -20
  29. package/ios/core/ArrayBufferHolder.hpp +1 -1
  30. package/ios/core/HybridObjectSpec.swift +1 -1
  31. package/ios/platform/NitroLogger.mm +1 -2
  32. package/ios/turbomodule/NativeNitroModules+NewArch.mm +67 -0
  33. package/ios/turbomodule/NativeNitroModules+OldArch.mm +71 -0
  34. package/ios/turbomodule/NativeNitroModules.h +22 -0
  35. package/lib/BoxedHybridObject.d.ts +12 -0
  36. package/lib/BoxedHybridObject.js +1 -0
  37. package/lib/ModuleNotFoundError.js +3 -13
  38. package/lib/NitroModules.d.ts +1 -83
  39. package/lib/NitroModules.js +2 -94
  40. package/lib/NitroModulesProxy.d.ts +58 -0
  41. package/lib/NitroModulesProxy.js +1 -0
  42. package/lib/commonjs/BoxedHybridObject.js +6 -0
  43. package/lib/commonjs/BoxedHybridObject.js.map +1 -0
  44. package/lib/commonjs/ModuleNotFoundError.js +3 -15
  45. package/lib/commonjs/ModuleNotFoundError.js.map +1 -1
  46. package/lib/commonjs/NitroModules.js +11 -100
  47. package/lib/commonjs/NitroModules.js.map +1 -1
  48. package/lib/commonjs/NitroModulesProxy.js +6 -0
  49. package/lib/commonjs/NitroModulesProxy.js.map +1 -0
  50. package/lib/commonjs/turbomodule/NativeNitroModules.js +36 -0
  51. package/lib/commonjs/turbomodule/NativeNitroModules.js.map +1 -0
  52. package/lib/commonjs/turbomodule/NativeNitroModules.web.js +17 -0
  53. package/lib/commonjs/turbomodule/NativeNitroModules.web.js.map +1 -0
  54. package/lib/module/BoxedHybridObject.js +4 -0
  55. package/lib/module/BoxedHybridObject.js.map +1 -0
  56. package/lib/module/ModuleNotFoundError.js +3 -15
  57. package/lib/module/ModuleNotFoundError.js.map +1 -1
  58. package/lib/module/NitroModules.js +2 -100
  59. package/lib/module/NitroModules.js.map +1 -1
  60. package/lib/module/NitroModulesProxy.js +4 -0
  61. package/lib/module/NitroModulesProxy.js.map +1 -0
  62. package/lib/module/turbomodule/NativeNitroModules.js +31 -0
  63. package/lib/module/turbomodule/NativeNitroModules.js.map +1 -0
  64. package/lib/{NitroModulesTurboModule.web.js → module/turbomodule/NativeNitroModules.web.js} +9 -1
  65. package/lib/module/turbomodule/NativeNitroModules.web.js.map +1 -0
  66. package/lib/tsconfig.tsbuildinfo +1 -1
  67. package/lib/turbomodule/NativeNitroModules.d.ts +7 -0
  68. package/lib/turbomodule/NativeNitroModules.js +27 -0
  69. package/lib/turbomodule/NativeNitroModules.web.d.ts +2 -0
  70. package/lib/turbomodule/NativeNitroModules.web.js +9 -0
  71. package/lib/typescript/AnyMap.d.ts +20 -0
  72. package/lib/typescript/BoxedHybridObject.d.ts +13 -0
  73. package/lib/typescript/BoxedHybridObject.d.ts.map +1 -0
  74. package/lib/typescript/ModuleNotFoundError.d.ts +7 -0
  75. package/lib/typescript/ModuleNotFoundError.d.ts.map +1 -1
  76. package/lib/typescript/NitroModules.d.ts +1 -83
  77. package/lib/typescript/NitroModules.d.ts.map +1 -1
  78. package/lib/typescript/NitroModulesProxy.d.ts +59 -0
  79. package/lib/typescript/NitroModulesProxy.d.ts.map +1 -0
  80. package/lib/typescript/__tests__/index.test.d.ts +1 -0
  81. package/lib/typescript/index.d.ts +4 -0
  82. package/lib/typescript/turbomodule/NativeNitroModules.d.ts +8 -0
  83. package/lib/typescript/turbomodule/NativeNitroModules.d.ts.map +1 -0
  84. package/lib/typescript/turbomodule/NativeNitroModules.web.d.ts +3 -0
  85. package/lib/typescript/turbomodule/NativeNitroModules.web.d.ts.map +1 -0
  86. package/package.json +10 -3
  87. package/src/BoxedHybridObject.ts +13 -0
  88. package/src/ModuleNotFoundError.ts +3 -19
  89. package/src/NitroModules.ts +2 -108
  90. package/src/NitroModulesProxy.ts +61 -0
  91. package/src/turbomodule/NativeNitroModules.ts +48 -0
  92. package/src/turbomodule/NativeNitroModules.web.ts +16 -0
  93. package/android/src/main/java/com/margelo/nitro/NitroModulesPackage.java +0 -30
  94. package/cpp/turbomodule/NativeNitroModules.cpp +0 -146
  95. package/cpp/turbomodule/NativeNitroModules.h +0 -8
  96. package/cpp/turbomodule/NativeNitroModules.hpp +0 -38
  97. package/cpp/turbomodule/RegisterNativeNitroModules.cpp +0 -33
  98. package/cpp/turbomodule/RegisterNativeNitroModules.hpp +0 -21
  99. package/ios/turbomodule/NitroModuleOnLoad.mm +0 -32
  100. package/lib/NativeNitroModules.d.ts +0 -16
  101. package/lib/NativeNitroModules.js +0 -22
  102. package/lib/NativeNitroModules.web.d.ts +0 -4
  103. package/lib/NativeNitroModules.web.js +0 -3
  104. package/lib/NitroModulesTurboModule.d.ts +0 -18
  105. package/lib/NitroModulesTurboModule.js +0 -23
  106. package/lib/NitroModulesTurboModule.web.d.ts +0 -1
  107. package/lib/commonjs/NitroModulesTurboModule.js +0 -34
  108. package/lib/commonjs/NitroModulesTurboModule.js.map +0 -1
  109. package/lib/commonjs/NitroModulesTurboModule.web.js +0 -11
  110. package/lib/commonjs/NitroModulesTurboModule.web.js.map +0 -1
  111. package/lib/module/NitroModulesTurboModule.js +0 -30
  112. package/lib/module/NitroModulesTurboModule.js.map +0 -1
  113. package/lib/module/NitroModulesTurboModule.web.js +0 -7
  114. package/lib/module/NitroModulesTurboModule.web.js.map +0 -1
  115. package/lib/typescript/NitroModulesTurboModule.d.ts +0 -19
  116. package/lib/typescript/NitroModulesTurboModule.d.ts.map +0 -1
  117. package/lib/typescript/NitroModulesTurboModule.web.d.ts +0 -2
  118. package/lib/typescript/NitroModulesTurboModule.web.d.ts.map +0 -1
  119. package/src/NitroModulesTurboModule.ts +0 -50
  120. package/src/NitroModulesTurboModule.web.ts +0 -7
@@ -1,84 +1,2 @@
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';
84
2
  //# sourceMappingURL=NitroModules.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"NitroModules.d.ts","sourceRoot":"","sources":["../../src/NitroModules.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAA;AAElD;;;GAGG;AACH,MAAM,WAAW,iBAAiB,CAAC,CAAC,SAAS,YAAY;IACvD;;;OAGG;IACH,KAAK,IAAI,CAAC,CAAA;CACX;AAED;;GAEG;AACH,eAAO,MAAM,YAAY;IACvB;;;;;;;;;OASG;uBACgB,CAAC,SAAS,YAAY,CAAC,GAAG,CAAC,QAAQ,MAAM,GAAG,CAAC;IAKhE;;OAEG;+BACwB,MAAM,EAAE;IAInC;;OAEG;0BACmB,MAAM,GAAG,OAAO;IAItC;;;;;;;;;;;;OAYG;2BACoB,MAAM,GAAG,OAAO;IAIvC;;OAEG;8BACuB,MAAM,GAAG,IAAI;IAIvC;;;OAGG;;IAKH;;;;;;;;;;;;;;;;;;;;;;OAsBG;QACC,CAAC,SAAS,YAAY,gBAAgB,CAAC,GAAG,iBAAiB,CAAC,CAAC,CAAC;CAInE,CAAA"}
1
+ {"version":3,"file":"NitroModules.d.ts","sourceRoot":"","sources":["../../src/NitroModules.ts"],"names":[],"mappings":"AACA,cAAc,kCAAkC,CAAA"}
@@ -0,0 +1,59 @@
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
+ }
59
+ //# sourceMappingURL=NitroModulesProxy.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"NitroModulesProxy.d.ts","sourceRoot":"","sources":["../../src/NitroModulesProxy.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAA;AAClD,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAA;AAE5D;;;;;GAKG;AACH,MAAM,WAAW,iBAAkB,SAAQ,YAAY;IACrD;;;;;;;;;OASG;IACH,kBAAkB,CAAC,CAAC,SAAS,YAAY,EAAE,IAAI,EAAE,MAAM,GAAG,CAAC,CAAA;IAC3D;;OAEG;IACH,eAAe,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAA;IACtC;;OAEG;IACH,uBAAuB,IAAI,MAAM,EAAE,CAAA;IAEnC;;;OAGG;IACH,SAAS,EAAE,OAAO,GAAG,SAAS,CAAA;IAE9B;;;;;;;;;;;;;;;;;;;;;;OAsBG;IACH,GAAG,CAAC,CAAC,SAAS,YAAY,EAAE,GAAG,EAAE,CAAC,GAAG,iBAAiB,CAAC,CAAC,CAAC,CAAA;CAC1D"}
@@ -0,0 +1 @@
1
+ //# sourceMappingURL=index.test.d.ts.map
@@ -0,0 +1,4 @@
1
+ export * from './HybridObject';
2
+ export * from './NitroModules';
3
+ export * from './AnyMap';
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1,8 @@
1
+ import type { NitroModulesProxy } from '../NitroModulesProxy';
2
+ export declare const NitroModules: NitroModulesProxy;
3
+ declare global {
4
+ var __nitroModulesJSICache: {};
5
+ var __nitroDispatcher: {};
6
+ }
7
+ export declare function isRuntimeAlive(): boolean;
8
+ //# sourceMappingURL=NativeNitroModules.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"NativeNitroModules.d.ts","sourceRoot":"","sources":["../../../src/turbomodule/NativeNitroModules.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAA;AA2B7D,eAAO,MAAM,YAAY,EAA+B,iBAAiB,CAAA;AAQzE,OAAO,CAAC,MAAM,CAAC;IACb,IAAI,sBAAsB,EAAE,EAAE,CAAA;IAC9B,IAAI,iBAAiB,EAAE,EAAE,CAAA;CAC1B;AAED,wBAAgB,cAAc,YAI7B"}
@@ -0,0 +1,3 @@
1
+ export declare const NitroModules: {};
2
+ export declare function isRuntimeAlive(): boolean;
3
+ //# sourceMappingURL=NativeNitroModules.web.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"NativeNitroModules.web.d.ts","sourceRoot":"","sources":["../../../src/turbomodule/NativeNitroModules.web.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,YAAY,IASxB,CAAA;AAED,wBAAgB,cAAc,IAAI,OAAO,CAExC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-nitro-modules",
3
- "version": "0.10.0",
3
+ "version": "0.12.0",
4
4
  "description": "Insanely fast native C++, Swift or Kotlin modules with a statically compiled binding layer to JSI.",
5
5
  "main": "lib/index",
6
6
  "module": "lib/index",
@@ -26,7 +26,7 @@
26
26
  "postinstall": "tsc || exit 0;",
27
27
  "test": "jest",
28
28
  "typecheck": "tsc --noEmit",
29
- "clean": "del-cli android/build node_modules/**/android/build lib",
29
+ "clean": "rm -rf android/build node_modules/**/android/build lib",
30
30
  "lint": "eslint \"**/*.{js,ts,tsx}\" --fix",
31
31
  "lint-ci": "eslint \"**/*.{js,ts,tsx}\" -f @jamesacarr/github-actions",
32
32
  "release": "bob build && release-it",
@@ -67,7 +67,6 @@
67
67
  "@react-native/eslint-config": "^0.75.2",
68
68
  "@types/jest": "^29.5.12",
69
69
  "@types/react": "^18.3.4",
70
- "del-cli": "^5.1.0",
71
70
  "eslint": "^8.57.0",
72
71
  "eslint-config-prettier": "^9.1.0",
73
72
  "eslint-plugin-prettier": "^5.2.1",
@@ -147,5 +146,13 @@
147
146
  }
148
147
  ]
149
148
  ]
149
+ },
150
+ "codegenConfig": {
151
+ "name": "NitroModulesSpec",
152
+ "type": "modules",
153
+ "jsSrcsDir": "./src",
154
+ "android": {
155
+ "javaPackageName": "com.margelo.nitro"
156
+ }
150
157
  }
151
158
  }
@@ -0,0 +1,13 @@
1
+ import type { HybridObject } from './HybridObject'
2
+
3
+ /**
4
+ * Represents a boxed {@linkcode HybridObject} that can later be unboxed again.
5
+ * This is implemented as a `jsi::HostObject`.
6
+ */
7
+ export interface BoxedHybridObject<T extends HybridObject> {
8
+ /**
9
+ * Unboxes the {@linkcode HybridObject}.
10
+ * This can be called from a different Runtime than the one it was boxed in.
11
+ */
12
+ unbox(): T
13
+ }
@@ -32,20 +32,6 @@ function getFrameworkType(): 'react-native' | 'expo' | 'expo-go' {
32
32
 
33
33
  export class ModuleNotFoundError extends Error {
34
34
  constructor(cause?: unknown) {
35
- // TurboModule not found, something went wrong!
36
- if (global.__turboModuleProxy == null) {
37
- // TurboModules are not available/new arch is not enabled.
38
- const message =
39
- 'Failed to get NitroModules: NitroModules require the new architecture to be enabled!'
40
- const suggestions: string[] = []
41
- suggestions.push(
42
- '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)'
43
- )
44
- const error = messageWithSuggestions(message, suggestions)
45
- super(error, { cause: cause })
46
- return
47
- }
48
-
49
35
  const framework = getFrameworkType()
50
36
  if (framework === 'expo-go') {
51
37
  super(
@@ -55,17 +41,15 @@ export class ModuleNotFoundError extends Error {
55
41
  }
56
42
 
57
43
  const message =
58
- 'Failed to get NitroModules: The native "NitroModules" TurboModule could not be found.'
44
+ 'Failed to get NitroModules: The native "NitroModules" Turbo/Native-Module could not be found.'
59
45
  const suggestions: string[] = []
60
46
  suggestions.push(
61
47
  'Make sure react-native-nitro-modules/NitroModules is correctly autolinked (run `npx react-native config` to verify)'
62
48
  )
63
49
  suggestions.push(
64
- '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'
65
- )
66
- suggestions.push(
67
- 'Make sure you are using react-native 0.74.0 or higher, because NitroModules are built with C++ TurboModules.'
50
+ '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'
68
51
  )
52
+ suggestions.push('Make sure you are using react-native 0.75.0 or higher.')
69
53
  suggestions.push('Make sure you rebuilt the app.')
70
54
  if (framework === 'expo') {
71
55
  suggestions.push('Make sure you ran `expo prebuild`.')
@@ -1,108 +1,2 @@
1
- import { getNativeNitroModules } from './NitroModulesTurboModule'
2
- import type { HybridObject } from './HybridObject'
3
-
4
- /**
5
- * Represents a boxed {@linkcode HybridObject} that can later be unboxed again.
6
- * This is implemented as a `jsi::HostObject`.
7
- */
8
- export interface BoxedHybridObject<T extends HybridObject> {
9
- /**
10
- * Unboxes the {@linkcode HybridObject}.
11
- * This can be called from a different Runtime than the one it was boxed in.
12
- */
13
- unbox(): T
14
- }
15
-
16
- /**
17
- * A lazy proxy for initializing Nitro Modules HybridObjects.
18
- */
19
- export const NitroModules = {
20
- /**
21
- * Create a new instance of the `HybridObject` {@linkcode T}.
22
- *
23
- * {@linkcode T} has to be registered beforehand under the name {@linkcode name}
24
- * in the native Nitro Modules `HybridObjectRegistry`.
25
- *
26
- * @param name The name of the `HybridObject` under which it was registered at.
27
- * @returns An instance of {@linkcode T}
28
- * @throws an Error if {@linkcode T} has not been registered under the name {@linkcode name}.
29
- */
30
- createHybridObject<T extends HybridObject<any>>(name: string): T {
31
- const nitro = getNativeNitroModules()
32
- const instance = nitro.createHybridObject(name)
33
- return instance as T
34
- },
35
- /**
36
- * Get a list of all registered Hybrid Objects.
37
- */
38
- getAllHybridObjectNames(): string[] {
39
- const nitro = getNativeNitroModules()
40
- return nitro.getAllHybridObjectNames()
41
- },
42
- /**
43
- * Returns whether a HybridObject under the given {@linkcode name} is registered, or not.
44
- */
45
- hasHybridObject(name: string): boolean {
46
- const nitro = getNativeNitroModules()
47
- return nitro.hasHybridObject(name)
48
- },
49
- /**
50
- * Returns whether the given {@linkcode object} has a `NativeState` or not.
51
- *
52
- * This can be a quick way to check if an object is a valid {@linkcode HybridObject},
53
- * and has not yet been disposed.
54
- * @example
55
- * ```ts
56
- * const someObject = NitroModules.createHybridObject<Some>('Some')
57
- * console.log(NitroModules.hasNativeState(someObject)) // -> true
58
- * someObject.dispose()
59
- * console.log(NitroModules.hasNativeState(someObject)) // -> false
60
- * ```
61
- */
62
- hasNativeState(object: object): boolean {
63
- const nitro = getNativeNitroModules()
64
- return nitro.hasNativeState(object)
65
- },
66
- /**
67
- * Forcefully removes the `NativeState` of the given {@linkcode object}.
68
- */
69
- removeNativeState(object: object): void {
70
- const nitro = getNativeNitroModules()
71
- nitro.removeNativeState(object)
72
- },
73
- /**
74
- * Gets the current build type configuration as defined in the `NITRO_DEBUG`
75
- * preprocessor flag.
76
- */
77
- get buildType(): 'debug' | 'release' {
78
- const nitro = getNativeNitroModules()
79
- return nitro.buildType
80
- },
81
- /**
82
- * Boxes the given {@linkcode hybridObject} into a {@linkcode BoxedHybridObject<T>}, which can
83
- * later be unboxed in a separate Runtime.
84
- *
85
- * While Nitro is runtime-agnostic and all `HybridObject`s can be used from a any Runtime,
86
- * some threading/worklet libraries (like [react-native-worklets-core](https://github.com/margelo/react-native-worklets-core))
87
- * do not yet support copying over `HybridObject`s as they use newer JSI APIs like `jsi::NativeState`.
88
- *
89
- * While those APIs are not yet available, you can still use every Nitro Hybrid Object in a separate
90
- * Runtime/Worklet context by just boxing it yourself:
91
- *
92
- * @example
93
- * ```ts
94
- * const something = NitroModules.createHybridObject<Something>('Something')
95
- * const boxed = NitroModules.box(something)
96
- * const context = Worklets.createContext('DummyContext')
97
- * context.runAsync(() => {
98
- * 'worklet'
99
- * const unboxed = boxed.unbox()
100
- * console.log(unboxed.name) // --> "Something"
101
- * })
102
- * ```
103
- */
104
- box<T extends HybridObject>(hybridObject: T): BoxedHybridObject<T> {
105
- const nitro = getNativeNitroModules()
106
- return nitro.box(hybridObject) as BoxedHybridObject<T>
107
- },
108
- }
1
+ // Re-exports the platform specific `NitroModulesProxy` (or a stub-implementation if not found)
2
+ export * from './turbomodule/NativeNitroModules'
@@ -0,0 +1,61 @@
1
+ import type { HybridObject } from './HybridObject'
2
+ import type { BoxedHybridObject } from './BoxedHybridObject'
3
+
4
+ /**
5
+ * The Proxy class that acts as a main entry point for Nitro Modules in React Native.
6
+ *
7
+ * This is a `HybridObject` on the native side as well, and is expected to be
8
+ * installed into the runtime's `global` via the NativeModule/TurboModule's `install()` function.
9
+ */
10
+ export interface NitroModulesProxy extends HybridObject {
11
+ /**
12
+ * Create a new instance of the `HybridObject` {@linkcode T}.
13
+ *
14
+ * {@linkcode T} has to be registered beforehand under the name {@linkcode name}
15
+ * in the native Nitro Modules `HybridObjectRegistry`.
16
+ *
17
+ * @param name The name of the `HybridObject` under which it was registered at.
18
+ * @returns An instance of {@linkcode T}
19
+ * @throws an Error if {@linkcode T} has not been registered under the name {@linkcode name}.
20
+ */
21
+ createHybridObject<T extends HybridObject>(name: string): T
22
+ /**
23
+ * Returns whether a HybridObject under the given {@linkcode name} is registered, or not.
24
+ */
25
+ hasHybridObject(name: string): boolean
26
+ /**
27
+ * Get a list of all registered Hybrid Objects.
28
+ */
29
+ getAllHybridObjectNames(): string[]
30
+
31
+ /**
32
+ * Gets the current build type configuration as defined in the `NITRO_DEBUG`
33
+ * preprocessor flag.
34
+ */
35
+ buildType: 'debug' | 'release'
36
+
37
+ /**
38
+ * Boxes the given {@linkcode hybridObject} into a {@linkcode BoxedHybridObject<T>}, which can
39
+ * later be unboxed in a separate Runtime.
40
+ *
41
+ * While Nitro is runtime-agnostic and all `HybridObject`s can be used from a any Runtime,
42
+ * some threading/worklet libraries (like [react-native-worklets-core](https://github.com/margelo/react-native-worklets-core))
43
+ * do not yet support copying over `HybridObject`s as they use newer JSI APIs like `jsi::NativeState`.
44
+ *
45
+ * While those APIs are not yet available, you can still use every Nitro Hybrid Object in a separate
46
+ * Runtime/Worklet context by just boxing it yourself:
47
+ *
48
+ * @example
49
+ * ```ts
50
+ * const something = NitroModules.createHybridObject<Something>('Something')
51
+ * const boxed = NitroModules.box(something)
52
+ * const context = Worklets.createContext('DummyContext')
53
+ * context.runAsync(() => {
54
+ * 'worklet'
55
+ * const unboxed = boxed.unbox()
56
+ * console.log(unboxed.name) // --> "Something"
57
+ * })
58
+ * ```
59
+ */
60
+ box<T extends HybridObject>(obj: T): BoxedHybridObject<T>
61
+ }
@@ -0,0 +1,48 @@
1
+ import type { TurboModule } from 'react-native'
2
+ import { TurboModuleRegistry } from 'react-native'
3
+ import { ModuleNotFoundError } from '../ModuleNotFoundError'
4
+ import type { NitroModulesProxy } from '../NitroModulesProxy'
5
+
6
+ interface Spec extends TurboModule {
7
+ /**
8
+ * Installs `NitroModulesProxy` into this JS Runtime's `global`.
9
+ * Also sets up a Dispatcher for the main JS Runtime.
10
+ * @returns A `string` error message if this function failed to run, or `undefined` if everything went successful.
11
+ */
12
+ install(): string | undefined
13
+ }
14
+
15
+ // 1. Get (and initialize) the TurboModule
16
+ let turboModule: Spec | undefined
17
+ try {
18
+ turboModule = TurboModuleRegistry.getEnforcing<Spec>('NitroModules')
19
+ } catch (e) {
20
+ throw new ModuleNotFoundError(e)
21
+ }
22
+
23
+ // 2. Install Dispatcher and install `NitroModulesProxy` into the Runtime's `global`
24
+ const errorMessage = turboModule.install()
25
+ if (errorMessage != null) {
26
+ throw new Error(`Failed to install Nitro: ${errorMessage}`)
27
+ }
28
+
29
+ // 3. Find `NitroModulesProxy` in `global`
30
+ // @ts-expect-error
31
+ export const NitroModules = global.NitroModulesProxy as NitroModulesProxy
32
+ if (NitroModules == null) {
33
+ const cause = new Error(
34
+ 'NitroModules was installed, but `global.NitroModulesProxy` was null!'
35
+ )
36
+ throw new ModuleNotFoundError(cause)
37
+ }
38
+
39
+ declare global {
40
+ var __nitroModulesJSICache: {}
41
+ var __nitroDispatcher: {}
42
+ }
43
+
44
+ export function isRuntimeAlive() {
45
+ const cache = global.__nitroModulesJSICache
46
+ const dispatcher = global.__nitroDispatcher
47
+ return cache != null && dispatcher != null
48
+ }
@@ -0,0 +1,16 @@
1
+ import { Platform } from 'react-native'
2
+
3
+ export const NitroModules = new Proxy(
4
+ {},
5
+ {
6
+ get: () => {
7
+ throw new Error(
8
+ `Native NitroModules are not available on ${Platform.OS}! Make sure you're not calling getNativeNitroModules() in a ${Platform.OS} (.${Platform.OS}.ts) environment.`
9
+ )
10
+ },
11
+ }
12
+ )
13
+
14
+ export function isRuntimeAlive(): boolean {
15
+ return false
16
+ }
@@ -1,30 +0,0 @@
1
- package com.margelo.nitro;
2
-
3
- import android.util.Log;
4
- import androidx.annotation.Nullable;
5
-
6
- import com.facebook.react.bridge.NativeModule;
7
- import com.facebook.react.bridge.ReactApplicationContext;
8
- import com.facebook.react.module.model.ReactModuleInfoProvider;
9
- import com.facebook.react.TurboReactPackage;
10
-
11
- import java.util.HashMap;
12
-
13
- public class NitroModulesPackage extends TurboReactPackage {
14
- static {
15
- JNIOnLoad.initializeNativeNitro();
16
- }
17
-
18
- @Nullable
19
- @Override
20
- public NativeModule getModule(String name, ReactApplicationContext reactContext) {
21
- return null;
22
- }
23
-
24
- @Override
25
- public ReactModuleInfoProvider getReactModuleInfoProvider() {
26
- return () -> {
27
- return new HashMap<>();
28
- };
29
- }
30
- }