mirror of
https://github.com/zoriya/nitro.git
synced 2026-10-13 00:05:23 +00:00
Add NitroModules.get
This commit is contained in:
+5
-2
@@ -7,6 +7,9 @@ export interface Spec extends TurboModule {
|
||||
createHybridObject(name: string, args?: UnsafeObject): UnsafeObject
|
||||
}
|
||||
|
||||
export const NitroModules =
|
||||
// 1. Get (and initialize) the C++ TurboModule
|
||||
export const NativeNitroModules =
|
||||
TurboModuleRegistry.getEnforcing<Spec>('NitroModulesCxx')
|
||||
NitroModules.install()
|
||||
|
||||
// 2. Install Dispatcher and required bindings into the Runtime
|
||||
NativeNitroModules.install()
|
||||
@@ -0,0 +1,29 @@
|
||||
import { NativeNitroModules } from './NativeNitroModules'
|
||||
import type { HybridObject } from './HybridObject'
|
||||
|
||||
// TODO: Do we wanna support such constructors?
|
||||
// @ts-expect-error
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
type ExtractConstructors<T> = {
|
||||
[K in keyof T as K extends `constructor` ? `create` : never]: T[K]
|
||||
}
|
||||
|
||||
/**
|
||||
* A lazy proxy for initializing Nitro Modules HybridObjects.
|
||||
*/
|
||||
export const NitroModules = {
|
||||
/**
|
||||
* Create a new instance of the `HybridObject` {@linkcode T}.
|
||||
*
|
||||
* {@linkcode T} has to be registered beforehand under the name {@linkcode name}
|
||||
* in the native Nitro Modules `HybridObjectRegistry`.
|
||||
*
|
||||
* @param name The name of the `HybridObject` under which it was registered at.
|
||||
* @returns An instance of {@linkcode T}
|
||||
* @throws an Error if {@linkcode T} has not been registered under the name {@linkcode name}.
|
||||
*/
|
||||
get<T extends HybridObject<any>>(name: string): T {
|
||||
const instance = NativeNitroModules.createHybridObject(name)
|
||||
return instance as T
|
||||
},
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
import { NitroModules } from './NativeNitro'
|
||||
import { NitroModules, type HybridObject } from '.'
|
||||
|
||||
export interface TestHybridObject {
|
||||
export interface TestHybridObject extends HybridObject<{}> {
|
||||
// C++ getter & setter
|
||||
int: number
|
||||
string: string
|
||||
@@ -24,10 +24,10 @@ export interface TestHybridObject {
|
||||
}
|
||||
|
||||
export function createCppTestHybridObject(): TestHybridObject {
|
||||
return NitroModules.createHybridObject('TestHybridObject') as TestHybridObject
|
||||
return NitroModules.get<TestHybridObject>('TestHybridObject')
|
||||
}
|
||||
|
||||
export interface SwiftTestHybridObject {
|
||||
export interface SwiftTestHybridObject extends HybridObject<{}> {
|
||||
// Swift getter & setter
|
||||
int: number
|
||||
// Swift methods
|
||||
@@ -36,7 +36,5 @@ export interface SwiftTestHybridObject {
|
||||
}
|
||||
|
||||
export function createSwiftTestHybridObject(): SwiftTestHybridObject {
|
||||
return NitroModules.createHybridObject(
|
||||
'SwiftTestObject'
|
||||
) as SwiftTestHybridObject
|
||||
return NitroModules.get<SwiftTestHybridObject>('SwiftTestObject')
|
||||
}
|
||||
|
||||
@@ -1,12 +1,3 @@
|
||||
export * from './createTestObject'
|
||||
export * from './HybridObject'
|
||||
|
||||
type ExtractConstructors<T> = {
|
||||
[K in keyof T as K extends `constructor` ? `create` : never]: T[K]
|
||||
}
|
||||
|
||||
export const NitroModules = {
|
||||
get<T>(): ExtractConstructors<T> {
|
||||
return null as unknown as ExtractConstructors<T>
|
||||
},
|
||||
}
|
||||
export * from './NitroModules'
|
||||
|
||||
Reference in New Issue
Block a user