react-native-nitro-modules 0.13.0 → 0.14.1
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.
- package/android/CMakeLists.txt +13 -1
- package/android/build.gradle +1 -1
- package/cpp/core/HybridFunction.hpp +1 -1
- package/cpp/utils/NitroDefines.hpp +0 -8
- package/lib/Constructor.d.ts +17 -0
- package/lib/Constructor.js +61 -0
- package/lib/SyncCallback.d.ts +19 -0
- package/lib/SyncCallback.js +1 -0
- package/lib/commonjs/Constructor.js +71 -0
- package/lib/commonjs/Constructor.js.map +1 -0
- package/lib/commonjs/index.js +11 -0
- package/lib/commonjs/index.js.map +1 -1
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/module/Constructor.js +67 -0
- package/lib/module/Constructor.js.map +1 -0
- package/lib/module/index.js +1 -0
- package/lib/module/index.js.map +1 -1
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/lib/typescript/Constructor.d.ts +18 -0
- package/lib/typescript/Constructor.d.ts.map +1 -0
- package/lib/typescript/index.d.ts +1 -0
- package/lib/typescript/index.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/Constructor.ts +69 -0
- package/src/index.ts +1 -0
- package/lib/typescript/AnyMap.d.ts +0 -20
- package/lib/typescript/AnyMap.d.ts.map +0 -1
- package/lib/typescript/BoxedHybridObject.d.ts +0 -13
- package/lib/typescript/BoxedHybridObject.d.ts.map +0 -1
- package/lib/typescript/HybridObject.d.ts +0 -99
- package/lib/typescript/HybridObject.d.ts.map +0 -1
- package/lib/typescript/ModuleNotFoundError.d.ts +0 -7
- package/lib/typescript/ModuleNotFoundError.d.ts.map +0 -1
- package/lib/typescript/NitroModules.d.ts +0 -2
- package/lib/typescript/NitroModules.d.ts.map +0 -1
- package/lib/typescript/NitroModulesProxy.d.ts +0 -59
- package/lib/typescript/NitroModulesProxy.d.ts.map +0 -1
- package/lib/typescript/__tests__/index.test.d.ts +0 -1
- package/lib/typescript/__tests__/index.test.d.ts.map +0 -1
- package/lib/typescript/turbomodule/NativeNitroModules.d.ts +0 -8
- package/lib/typescript/turbomodule/NativeNitroModules.d.ts.map +0 -1
- package/lib/typescript/turbomodule/NativeNitroModules.web.d.ts +0 -3
- package/lib/typescript/turbomodule/NativeNitroModules.web.d.ts.map +0 -1
package/android/CMakeLists.txt
CHANGED
|
@@ -52,6 +52,18 @@ target_link_libraries(
|
|
|
52
52
|
android # <-- Android JNI core
|
|
53
53
|
fbjni::fbjni # <-- Facebook C++ JNI helpers
|
|
54
54
|
ReactAndroid::jsi # <-- RN: JSI
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
# Link react-native (different prefab between RN 0.75 and RN 0.76)
|
|
58
|
+
if(ReactAndroid_VERSION_MINOR GREATER_EQUAL 76)
|
|
59
|
+
target_link_libraries(
|
|
60
|
+
NitroModules
|
|
61
|
+
ReactAndroid::reactnative # <-- RN: Native Modules umbrella prefab
|
|
62
|
+
)
|
|
63
|
+
else()
|
|
64
|
+
target_link_libraries(
|
|
65
|
+
NitroModules
|
|
55
66
|
ReactAndroid::react_nativemodule_core # <-- RN: TurboModules Core
|
|
56
67
|
ReactAndroid::turbomodulejsijni # <-- RN: TurboModules utils (e.g. CallInvokerHolder)
|
|
57
|
-
)
|
|
68
|
+
)
|
|
69
|
+
endif()
|
package/android/build.gradle
CHANGED
|
@@ -150,7 +150,7 @@ private:
|
|
|
150
150
|
template <typename Derived, typename ReturnType, typename... Args, size_t... Is>
|
|
151
151
|
static inline jsi::Value callMethod(Derived* obj, ReturnType (Derived::*method)(Args...), jsi::Runtime& runtime, const jsi::Value* args,
|
|
152
152
|
size_t argsSize, std::index_sequence<Is...>) {
|
|
153
|
-
jsi::Value defaultValue;
|
|
153
|
+
static const jsi::Value defaultValue;
|
|
154
154
|
|
|
155
155
|
if constexpr (std::is_void_v<ReturnType>) {
|
|
156
156
|
// It's a void method.
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { HybridObject } from './HybridObject';
|
|
2
|
+
/**
|
|
3
|
+
* Get a constructor function for the given `HybridObject` {@linkcode T}.
|
|
4
|
+
* @param name The name of the `HybridObject` under which it was registered at.
|
|
5
|
+
* @returns An instance of {@linkcode T}
|
|
6
|
+
* @example
|
|
7
|
+
* ```ts
|
|
8
|
+
* export const HybridImage = getHybridObjectConstructor<Image>('Image')
|
|
9
|
+
*
|
|
10
|
+
* const image1 = new HybridImage()
|
|
11
|
+
* const image2 = new HybridImage()
|
|
12
|
+
* image1 instanceof HybridImage // --> true
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
export declare function getHybridObjectConstructor<T extends HybridObject>(name: string): {
|
|
16
|
+
new (): T;
|
|
17
|
+
};
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { NitroModules } from './NitroModules';
|
|
2
|
+
const cache = new Map();
|
|
3
|
+
/**
|
|
4
|
+
* Get a constructor function for the given `HybridObject` {@linkcode T}.
|
|
5
|
+
* @param name The name of the `HybridObject` under which it was registered at.
|
|
6
|
+
* @returns An instance of {@linkcode T}
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* export const HybridImage = getHybridObjectConstructor<Image>('Image')
|
|
10
|
+
*
|
|
11
|
+
* const image1 = new HybridImage()
|
|
12
|
+
* const image2 = new HybridImage()
|
|
13
|
+
* image1 instanceof HybridImage // --> true
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
export function getHybridObjectConstructor(name) {
|
|
17
|
+
// Cache functions for performance.
|
|
18
|
+
if (cache.has(name)) {
|
|
19
|
+
return cache.get(name);
|
|
20
|
+
}
|
|
21
|
+
// A function that creates the HybridObject.
|
|
22
|
+
// This can be called with `new`, and internally sets the prototype.
|
|
23
|
+
const constructorFunc = (() => {
|
|
24
|
+
const instance = NitroModules.createHybridObject(name);
|
|
25
|
+
const prototype = Object.getPrototypeOf(instance);
|
|
26
|
+
if (constructorFunc.prototype !== prototype) {
|
|
27
|
+
constructorFunc.prototype = prototype;
|
|
28
|
+
constructorFunc.prototypeInitialized = true;
|
|
29
|
+
}
|
|
30
|
+
return instance;
|
|
31
|
+
});
|
|
32
|
+
// Configure lazy prototype. If `instanceof` is called before a `T`
|
|
33
|
+
// has been created, we just lazy-create a new `T` instance to set the proto.
|
|
34
|
+
constructorFunc.prototypeInitialized = false;
|
|
35
|
+
Object.defineProperty(constructorFunc, Symbol.hasInstance, {
|
|
36
|
+
value: (instance) => {
|
|
37
|
+
if (!constructorFunc.prototypeInitialized) {
|
|
38
|
+
// User didn't call `new T()` yet, so we don't
|
|
39
|
+
// know the prototype yet. Just create one temp object to find
|
|
40
|
+
// out the prototype.
|
|
41
|
+
const tempInstance = NitroModules.createHybridObject(name);
|
|
42
|
+
constructorFunc.prototype = Object.getPrototypeOf(tempInstance);
|
|
43
|
+
constructorFunc.prototypeInitialized = true;
|
|
44
|
+
}
|
|
45
|
+
// Loop through the prototype chain of the value
|
|
46
|
+
// we're testing for to see if it is a direct instance
|
|
47
|
+
// of `T`, or a derivative of it.
|
|
48
|
+
let proto = Object.getPrototypeOf(instance);
|
|
49
|
+
while (proto != null) {
|
|
50
|
+
if (proto === constructorFunc.prototype) {
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
proto = Object.getPrototypeOf(proto);
|
|
54
|
+
}
|
|
55
|
+
// No prototype overlap.
|
|
56
|
+
return false;
|
|
57
|
+
},
|
|
58
|
+
});
|
|
59
|
+
cache.set(name, constructorFunc);
|
|
60
|
+
return constructorFunc;
|
|
61
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Marks the given function {@linkcode T} as synchronous
|
|
3
|
+
*
|
|
4
|
+
* The function can then later be called synchronously and immediately from the native side,
|
|
5
|
+
* without scheduling a call or delaying the result.
|
|
6
|
+
*
|
|
7
|
+
* This is useful if you have full control over the Threading and want to run instant callbacks,
|
|
8
|
+
* like render callbacks in 3D engines.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* interface Scene extends HybridObject {
|
|
13
|
+
* startRendering(renderCallback: SyncCallback<() => void>): void
|
|
14
|
+
* }
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
export type SyncCallback<T extends Function> = T & {
|
|
18
|
+
__syncCallback?: true;
|
|
19
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.getHybridObjectConstructor = getHybridObjectConstructor;
|
|
7
|
+
var _NitroModules = require("./NitroModules");
|
|
8
|
+
const cache = new Map();
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Get a constructor function for the given `HybridObject` {@linkcode T}.
|
|
12
|
+
* @param name The name of the `HybridObject` under which it was registered at.
|
|
13
|
+
* @returns An instance of {@linkcode T}
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* export const HybridImage = getHybridObjectConstructor<Image>('Image')
|
|
17
|
+
*
|
|
18
|
+
* const image1 = new HybridImage()
|
|
19
|
+
* const image2 = new HybridImage()
|
|
20
|
+
* image1 instanceof HybridImage // --> true
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
function getHybridObjectConstructor(name) {
|
|
24
|
+
// Cache functions for performance.
|
|
25
|
+
if (cache.has(name)) {
|
|
26
|
+
return cache.get(name);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// A function that creates the HybridObject.
|
|
30
|
+
// This can be called with `new`, and internally sets the prototype.
|
|
31
|
+
const constructorFunc = () => {
|
|
32
|
+
const instance = _NitroModules.NitroModules.createHybridObject(name);
|
|
33
|
+
const prototype = Object.getPrototypeOf(instance);
|
|
34
|
+
if (constructorFunc.prototype !== prototype) {
|
|
35
|
+
constructorFunc.prototype = prototype;
|
|
36
|
+
constructorFunc.prototypeInitialized = true;
|
|
37
|
+
}
|
|
38
|
+
return instance;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
// Configure lazy prototype. If `instanceof` is called before a `T`
|
|
42
|
+
// has been created, we just lazy-create a new `T` instance to set the proto.
|
|
43
|
+
constructorFunc.prototypeInitialized = false;
|
|
44
|
+
Object.defineProperty(constructorFunc, Symbol.hasInstance, {
|
|
45
|
+
value: instance => {
|
|
46
|
+
if (!constructorFunc.prototypeInitialized) {
|
|
47
|
+
// User didn't call `new T()` yet, so we don't
|
|
48
|
+
// know the prototype yet. Just create one temp object to find
|
|
49
|
+
// out the prototype.
|
|
50
|
+
const tempInstance = _NitroModules.NitroModules.createHybridObject(name);
|
|
51
|
+
constructorFunc.prototype = Object.getPrototypeOf(tempInstance);
|
|
52
|
+
constructorFunc.prototypeInitialized = true;
|
|
53
|
+
}
|
|
54
|
+
// Loop through the prototype chain of the value
|
|
55
|
+
// we're testing for to see if it is a direct instance
|
|
56
|
+
// of `T`, or a derivative of it.
|
|
57
|
+
let proto = Object.getPrototypeOf(instance);
|
|
58
|
+
while (proto != null) {
|
|
59
|
+
if (proto === constructorFunc.prototype) {
|
|
60
|
+
return true;
|
|
61
|
+
}
|
|
62
|
+
proto = Object.getPrototypeOf(proto);
|
|
63
|
+
}
|
|
64
|
+
// No prototype overlap.
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
cache.set(name, constructorFunc);
|
|
69
|
+
return constructorFunc;
|
|
70
|
+
}
|
|
71
|
+
//# sourceMappingURL=Constructor.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["_NitroModules","require","cache","Map","getHybridObjectConstructor","name","has","get","constructorFunc","instance","NitroModules","createHybridObject","prototype","Object","getPrototypeOf","prototypeInitialized","defineProperty","Symbol","hasInstance","value","tempInstance","proto","set"],"sourceRoot":"../../src","sources":["Constructor.ts"],"mappings":";;;;;;AACA,IAAAA,aAAA,GAAAC,OAAA;AAEA,MAAMC,KAAK,GAAG,IAAIC,GAAG,CAAmB,CAAC;;AAEzC;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAASC,0BAA0BA,CACxCC,IAAY,EACG;EACf;EACA,IAAIH,KAAK,CAACI,GAAG,CAACD,IAAI,CAAC,EAAE;IACnB,OAAOH,KAAK,CAACK,GAAG,CAACF,IAAI,CAAC;EACxB;;EAEA;EACA;EACA,MAAMG,eAAe,GAAIA,CAAA,KAAM;IAC7B,MAAMC,QAAQ,GAAGC,0BAAY,CAACC,kBAAkB,CAAIN,IAAI,CAAC;IACzD,MAAMO,SAAS,GAAGC,MAAM,CAACC,cAAc,CAACL,QAAQ,CAAC;IACjD,IAAID,eAAe,CAACI,SAAS,KAAKA,SAAS,EAAE;MAC3CJ,eAAe,CAACI,SAAS,GAAGA,SAAS;MACrCJ,eAAe,CAACO,oBAAoB,GAAG,IAAI;IAC7C;IACA,OAAON,QAAQ;EACjB,CAAmE;;EAEnE;EACA;EACAD,eAAe,CAACO,oBAAoB,GAAG,KAAK;EAC5CF,MAAM,CAACG,cAAc,CAACR,eAAe,EAAES,MAAM,CAACC,WAAW,EAAE;IACzDC,KAAK,EAAGV,QAAiB,IAAK;MAC5B,IAAI,CAACD,eAAe,CAACO,oBAAoB,EAAE;QACzC;QACA;QACA;QACA,MAAMK,YAAY,GAAGV,0BAAY,CAACC,kBAAkB,CAAIN,IAAI,CAAC;QAC7DG,eAAe,CAACI,SAAS,GAAGC,MAAM,CAACC,cAAc,CAACM,YAAY,CAAC;QAC/DZ,eAAe,CAACO,oBAAoB,GAAG,IAAI;MAC7C;MACA;MACA;MACA;MACA,IAAIM,KAAK,GAAGR,MAAM,CAACC,cAAc,CAACL,QAAQ,CAAC;MAC3C,OAAOY,KAAK,IAAI,IAAI,EAAE;QACpB,IAAIA,KAAK,KAAKb,eAAe,CAACI,SAAS,EAAE;UACvC,OAAO,IAAI;QACb;QACAS,KAAK,GAAGR,MAAM,CAACC,cAAc,CAACO,KAAK,CAAC;MACtC;MACA;MACA,OAAO,KAAK;IACd;EACF,CAAC,CAAC;EAEFnB,KAAK,CAACoB,GAAG,CAACjB,IAAI,EAAEG,eAAe,CAAC;EAChC,OAAOA,eAAe;AACxB","ignoreList":[]}
|
package/lib/commonjs/index.js
CHANGED
|
@@ -36,4 +36,15 @@ Object.keys(_AnyMap).forEach(function (key) {
|
|
|
36
36
|
}
|
|
37
37
|
});
|
|
38
38
|
});
|
|
39
|
+
var _Constructor = require("./Constructor");
|
|
40
|
+
Object.keys(_Constructor).forEach(function (key) {
|
|
41
|
+
if (key === "default" || key === "__esModule") return;
|
|
42
|
+
if (key in exports && exports[key] === _Constructor[key]) return;
|
|
43
|
+
Object.defineProperty(exports, key, {
|
|
44
|
+
enumerable: true,
|
|
45
|
+
get: function () {
|
|
46
|
+
return _Constructor[key];
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
});
|
|
39
50
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["_HybridObject","require","Object","keys","forEach","key","exports","defineProperty","enumerable","get","_NitroModules","_AnyMap"],"sourceRoot":"../../src","sources":["index.ts"],"mappings":";;;;;AAAA,IAAAA,aAAA,GAAAC,OAAA;AAAAC,MAAA,CAAAC,IAAA,CAAAH,aAAA,EAAAI,OAAA,WAAAC,GAAA;EAAA,IAAAA,GAAA,kBAAAA,GAAA;EAAA,IAAAA,GAAA,IAAAC,OAAA,IAAAA,OAAA,CAAAD,GAAA,MAAAL,aAAA,CAAAK,GAAA;EAAAH,MAAA,CAAAK,cAAA,CAAAD,OAAA,EAAAD,GAAA;IAAAG,UAAA;IAAAC,GAAA,WAAAA,CAAA;MAAA,OAAAT,aAAA,CAAAK,GAAA;IAAA;EAAA;AAAA;AACA,IAAAK,aAAA,GAAAT,OAAA;AAAAC,MAAA,CAAAC,IAAA,CAAAO,aAAA,EAAAN,OAAA,WAAAC,GAAA;EAAA,IAAAA,GAAA,kBAAAA,GAAA;EAAA,IAAAA,GAAA,IAAAC,OAAA,IAAAA,OAAA,CAAAD,GAAA,MAAAK,aAAA,CAAAL,GAAA;EAAAH,MAAA,CAAAK,cAAA,CAAAD,OAAA,EAAAD,GAAA;IAAAG,UAAA;IAAAC,GAAA,WAAAA,CAAA;MAAA,OAAAC,aAAA,CAAAL,GAAA;IAAA;EAAA;AAAA;AACA,IAAAM,OAAA,GAAAV,OAAA;AAAAC,MAAA,CAAAC,IAAA,CAAAQ,OAAA,EAAAP,OAAA,WAAAC,GAAA;EAAA,IAAAA,GAAA,kBAAAA,GAAA;EAAA,IAAAA,GAAA,IAAAC,OAAA,IAAAA,OAAA,CAAAD,GAAA,MAAAM,OAAA,CAAAN,GAAA;EAAAH,MAAA,CAAAK,cAAA,CAAAD,OAAA,EAAAD,GAAA;IAAAG,UAAA;IAAAC,GAAA,WAAAA,CAAA;MAAA,OAAAE,OAAA,CAAAN,GAAA;IAAA;EAAA;AAAA","ignoreList":[]}
|
|
1
|
+
{"version":3,"names":["_HybridObject","require","Object","keys","forEach","key","exports","defineProperty","enumerable","get","_NitroModules","_AnyMap","_Constructor"],"sourceRoot":"../../src","sources":["index.ts"],"mappings":";;;;;AAAA,IAAAA,aAAA,GAAAC,OAAA;AAAAC,MAAA,CAAAC,IAAA,CAAAH,aAAA,EAAAI,OAAA,WAAAC,GAAA;EAAA,IAAAA,GAAA,kBAAAA,GAAA;EAAA,IAAAA,GAAA,IAAAC,OAAA,IAAAA,OAAA,CAAAD,GAAA,MAAAL,aAAA,CAAAK,GAAA;EAAAH,MAAA,CAAAK,cAAA,CAAAD,OAAA,EAAAD,GAAA;IAAAG,UAAA;IAAAC,GAAA,WAAAA,CAAA;MAAA,OAAAT,aAAA,CAAAK,GAAA;IAAA;EAAA;AAAA;AACA,IAAAK,aAAA,GAAAT,OAAA;AAAAC,MAAA,CAAAC,IAAA,CAAAO,aAAA,EAAAN,OAAA,WAAAC,GAAA;EAAA,IAAAA,GAAA,kBAAAA,GAAA;EAAA,IAAAA,GAAA,IAAAC,OAAA,IAAAA,OAAA,CAAAD,GAAA,MAAAK,aAAA,CAAAL,GAAA;EAAAH,MAAA,CAAAK,cAAA,CAAAD,OAAA,EAAAD,GAAA;IAAAG,UAAA;IAAAC,GAAA,WAAAA,CAAA;MAAA,OAAAC,aAAA,CAAAL,GAAA;IAAA;EAAA;AAAA;AACA,IAAAM,OAAA,GAAAV,OAAA;AAAAC,MAAA,CAAAC,IAAA,CAAAQ,OAAA,EAAAP,OAAA,WAAAC,GAAA;EAAA,IAAAA,GAAA,kBAAAA,GAAA;EAAA,IAAAA,GAAA,IAAAC,OAAA,IAAAA,OAAA,CAAAD,GAAA,MAAAM,OAAA,CAAAN,GAAA;EAAAH,MAAA,CAAAK,cAAA,CAAAD,OAAA,EAAAD,GAAA;IAAAG,UAAA;IAAAC,GAAA,WAAAA,CAAA;MAAA,OAAAE,OAAA,CAAAN,GAAA;IAAA;EAAA;AAAA;AACA,IAAAO,YAAA,GAAAX,OAAA;AAAAC,MAAA,CAAAC,IAAA,CAAAS,YAAA,EAAAR,OAAA,WAAAC,GAAA;EAAA,IAAAA,GAAA,kBAAAA,GAAA;EAAA,IAAAA,GAAA,IAAAC,OAAA,IAAAA,OAAA,CAAAD,GAAA,MAAAO,YAAA,CAAAP,GAAA;EAAAH,MAAA,CAAAK,cAAA,CAAAD,OAAA,EAAAD,GAAA;IAAAG,UAAA;IAAAC,GAAA,WAAAA,CAAA;MAAA,OAAAG,YAAA,CAAAP,GAAA;IAAA;EAAA;AAAA","ignoreList":[]}
|
package/lib/index.d.ts
CHANGED
package/lib/index.js
CHANGED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import { NitroModules } from './NitroModules';
|
|
4
|
+
const cache = new Map();
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Get a constructor function for the given `HybridObject` {@linkcode T}.
|
|
8
|
+
* @param name The name of the `HybridObject` under which it was registered at.
|
|
9
|
+
* @returns An instance of {@linkcode T}
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* export const HybridImage = getHybridObjectConstructor<Image>('Image')
|
|
13
|
+
*
|
|
14
|
+
* const image1 = new HybridImage()
|
|
15
|
+
* const image2 = new HybridImage()
|
|
16
|
+
* image1 instanceof HybridImage // --> true
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
export function getHybridObjectConstructor(name) {
|
|
20
|
+
// Cache functions for performance.
|
|
21
|
+
if (cache.has(name)) {
|
|
22
|
+
return cache.get(name);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// A function that creates the HybridObject.
|
|
26
|
+
// This can be called with `new`, and internally sets the prototype.
|
|
27
|
+
const constructorFunc = () => {
|
|
28
|
+
const instance = NitroModules.createHybridObject(name);
|
|
29
|
+
const prototype = Object.getPrototypeOf(instance);
|
|
30
|
+
if (constructorFunc.prototype !== prototype) {
|
|
31
|
+
constructorFunc.prototype = prototype;
|
|
32
|
+
constructorFunc.prototypeInitialized = true;
|
|
33
|
+
}
|
|
34
|
+
return instance;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
// Configure lazy prototype. If `instanceof` is called before a `T`
|
|
38
|
+
// has been created, we just lazy-create a new `T` instance to set the proto.
|
|
39
|
+
constructorFunc.prototypeInitialized = false;
|
|
40
|
+
Object.defineProperty(constructorFunc, Symbol.hasInstance, {
|
|
41
|
+
value: instance => {
|
|
42
|
+
if (!constructorFunc.prototypeInitialized) {
|
|
43
|
+
// User didn't call `new T()` yet, so we don't
|
|
44
|
+
// know the prototype yet. Just create one temp object to find
|
|
45
|
+
// out the prototype.
|
|
46
|
+
const tempInstance = NitroModules.createHybridObject(name);
|
|
47
|
+
constructorFunc.prototype = Object.getPrototypeOf(tempInstance);
|
|
48
|
+
constructorFunc.prototypeInitialized = true;
|
|
49
|
+
}
|
|
50
|
+
// Loop through the prototype chain of the value
|
|
51
|
+
// we're testing for to see if it is a direct instance
|
|
52
|
+
// of `T`, or a derivative of it.
|
|
53
|
+
let proto = Object.getPrototypeOf(instance);
|
|
54
|
+
while (proto != null) {
|
|
55
|
+
if (proto === constructorFunc.prototype) {
|
|
56
|
+
return true;
|
|
57
|
+
}
|
|
58
|
+
proto = Object.getPrototypeOf(proto);
|
|
59
|
+
}
|
|
60
|
+
// No prototype overlap.
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
cache.set(name, constructorFunc);
|
|
65
|
+
return constructorFunc;
|
|
66
|
+
}
|
|
67
|
+
//# sourceMappingURL=Constructor.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["NitroModules","cache","Map","getHybridObjectConstructor","name","has","get","constructorFunc","instance","createHybridObject","prototype","Object","getPrototypeOf","prototypeInitialized","defineProperty","Symbol","hasInstance","value","tempInstance","proto","set"],"sourceRoot":"../../src","sources":["Constructor.ts"],"mappings":";;AACA,SAASA,YAAY,QAAQ,gBAAgB;AAE7C,MAAMC,KAAK,GAAG,IAAIC,GAAG,CAAmB,CAAC;;AAEzC;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,0BAA0BA,CACxCC,IAAY,EACG;EACf;EACA,IAAIH,KAAK,CAACI,GAAG,CAACD,IAAI,CAAC,EAAE;IACnB,OAAOH,KAAK,CAACK,GAAG,CAACF,IAAI,CAAC;EACxB;;EAEA;EACA;EACA,MAAMG,eAAe,GAAIA,CAAA,KAAM;IAC7B,MAAMC,QAAQ,GAAGR,YAAY,CAACS,kBAAkB,CAAIL,IAAI,CAAC;IACzD,MAAMM,SAAS,GAAGC,MAAM,CAACC,cAAc,CAACJ,QAAQ,CAAC;IACjD,IAAID,eAAe,CAACG,SAAS,KAAKA,SAAS,EAAE;MAC3CH,eAAe,CAACG,SAAS,GAAGA,SAAS;MACrCH,eAAe,CAACM,oBAAoB,GAAG,IAAI;IAC7C;IACA,OAAOL,QAAQ;EACjB,CAAmE;;EAEnE;EACA;EACAD,eAAe,CAACM,oBAAoB,GAAG,KAAK;EAC5CF,MAAM,CAACG,cAAc,CAACP,eAAe,EAAEQ,MAAM,CAACC,WAAW,EAAE;IACzDC,KAAK,EAAGT,QAAiB,IAAK;MAC5B,IAAI,CAACD,eAAe,CAACM,oBAAoB,EAAE;QACzC;QACA;QACA;QACA,MAAMK,YAAY,GAAGlB,YAAY,CAACS,kBAAkB,CAAIL,IAAI,CAAC;QAC7DG,eAAe,CAACG,SAAS,GAAGC,MAAM,CAACC,cAAc,CAACM,YAAY,CAAC;QAC/DX,eAAe,CAACM,oBAAoB,GAAG,IAAI;MAC7C;MACA;MACA;MACA;MACA,IAAIM,KAAK,GAAGR,MAAM,CAACC,cAAc,CAACJ,QAAQ,CAAC;MAC3C,OAAOW,KAAK,IAAI,IAAI,EAAE;QACpB,IAAIA,KAAK,KAAKZ,eAAe,CAACG,SAAS,EAAE;UACvC,OAAO,IAAI;QACb;QACAS,KAAK,GAAGR,MAAM,CAACC,cAAc,CAACO,KAAK,CAAC;MACtC;MACA;MACA,OAAO,KAAK;IACd;EACF,CAAC,CAAC;EAEFlB,KAAK,CAACmB,GAAG,CAAChB,IAAI,EAAEG,eAAe,CAAC;EAChC,OAAOA,eAAe;AACxB","ignoreList":[]}
|
package/lib/module/index.js
CHANGED
package/lib/module/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":[],"sourceRoot":"../../src","sources":["index.ts"],"mappings":";;AAAA,cAAc,gBAAgB;AAC9B,cAAc,gBAAgB;AAC9B,cAAc,UAAU","ignoreList":[]}
|
|
1
|
+
{"version":3,"names":[],"sourceRoot":"../../src","sources":["index.ts"],"mappings":";;AAAA,cAAc,gBAAgB;AAC9B,cAAc,gBAAgB;AAC9B,cAAc,UAAU;AACxB,cAAc,eAAe","ignoreList":[]}
|