react-native-nitro-modules 0.1.3 → 0.1.4
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/NitroModules.podspec +3 -1
- package/android/src/main/cpp/platform/ThreadUtils.cpp +5 -0
- package/cpp/core/AnyMap.hpp +2 -1
- package/cpp/core/HybridObject.hpp +3 -4
- package/cpp/jsi/JSIConverter+AnyMap.hpp +64 -0
- package/cpp/jsi/JSIConverter+ArrayBuffer.hpp +41 -0
- package/cpp/jsi/JSIConverter+Function.hpp +124 -0
- package/cpp/jsi/JSIConverter+HybridObject.hpp +123 -0
- package/cpp/jsi/JSIConverter+Optional.hpp +41 -0
- package/cpp/jsi/JSIConverter+Promise.hpp +87 -0
- package/cpp/jsi/JSIConverter+Tuple.hpp +60 -0
- package/cpp/jsi/JSIConverter+UnorderedMap.hpp +50 -0
- package/cpp/jsi/JSIConverter+Variant.hpp +97 -0
- package/cpp/jsi/JSIConverter+Vector.hpp +48 -0
- package/cpp/jsi/JSIConverter.hpp +22 -498
- package/cpp/platform/ThreadUtils.hpp +6 -0
- package/cpp/threading/ThreadPool.cpp +84 -0
- package/cpp/threading/ThreadPool.hpp +53 -0
- package/ios/core/HybridContext.cpp +8 -0
- package/{cpp → ios}/core/HybridContext.hpp +6 -3
- package/ios/platform/ThreadUtils.cpp +4 -0
- package/lib/NativeNitroModules.d.ts +5 -0
- package/lib/NativeNitroModules.js +5 -0
- package/lib/NativeNitroModules.web.d.ts +4 -0
- package/lib/NativeNitroModules.web.js +3 -0
- package/lib/commonjs/NativeNitroModules.js +6 -0
- package/lib/commonjs/NativeNitroModules.js.map +1 -1
- package/lib/commonjs/NativeNitroModules.web.js +10 -0
- package/lib/commonjs/NativeNitroModules.web.js.map +1 -0
- package/lib/module/NativeNitroModules.js +5 -0
- package/lib/module/NativeNitroModules.js.map +1 -1
- package/lib/module/NativeNitroModules.web.js +4 -0
- package/lib/module/NativeNitroModules.web.js.map +1 -0
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/NativeNitroModules.ts +11 -0
- package/src/NativeNitroModules.web.ts +9 -0
|
@@ -18,6 +18,12 @@ public:
|
|
|
18
18
|
* This is implemented differently on iOS and Android.
|
|
19
19
|
*/
|
|
20
20
|
static std::string getThreadName();
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Set the current Thread's name.
|
|
24
|
+
* This is implemented differently on iOS and Android.
|
|
25
|
+
*/
|
|
26
|
+
static void setThreadName(const std::string& name);
|
|
21
27
|
};
|
|
22
28
|
|
|
23
29
|
} // namespace margelo::nitro
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
//
|
|
2
|
+
// ThreadPool.cpp
|
|
3
|
+
// NitroModules
|
|
4
|
+
//
|
|
5
|
+
// Created by Marc Rousavy on 21.06.24.
|
|
6
|
+
//
|
|
7
|
+
|
|
8
|
+
#include "ThreadPool.hpp"
|
|
9
|
+
#include "NitroLogger.hpp"
|
|
10
|
+
#include "ThreadUtils.hpp"
|
|
11
|
+
|
|
12
|
+
namespace margelo::nitro {
|
|
13
|
+
|
|
14
|
+
ThreadPool::ThreadPool(const char* name, size_t numThreads) : _isAlive(true), _name(name) {
|
|
15
|
+
Logger::log(TAG, "Creating ThreadPool \"%s\" with %i threads...", name, numThreads);
|
|
16
|
+
|
|
17
|
+
for (size_t i = 0; i < numThreads; ++i) {
|
|
18
|
+
std::string threadName = std::string(name) + "-" + std::to_string(i + 1);
|
|
19
|
+
_workers.emplace_back([this, threadName] {
|
|
20
|
+
// Set the Thread's name
|
|
21
|
+
ThreadUtils::setThreadName(threadName);
|
|
22
|
+
|
|
23
|
+
// Start the run-loop
|
|
24
|
+
while (true) {
|
|
25
|
+
std::function<void()> task;
|
|
26
|
+
{
|
|
27
|
+
// Lock on the mutex so only one Worker receives the condition signal at a time
|
|
28
|
+
std::unique_lock<std::mutex> lock(_queueMutex);
|
|
29
|
+
this->_condition.wait(lock, [this] { return !_isAlive || !_tasks.empty(); });
|
|
30
|
+
if (!_isAlive && _tasks.empty()) {
|
|
31
|
+
// ThreadPool is dead - stop run-loop.
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
// Schedule the oldest task
|
|
35
|
+
task = std::move(_tasks.front());
|
|
36
|
+
_tasks.pop();
|
|
37
|
+
}
|
|
38
|
+
// Run it (outside of the mutex so others can run in parallel)
|
|
39
|
+
task();
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
void ThreadPool::run(std::function<void()>&& task) {
|
|
46
|
+
{
|
|
47
|
+
// lock on the mutex - we want to emplace the task back in the queue
|
|
48
|
+
std::unique_lock<std::mutex> lock(_queueMutex);
|
|
49
|
+
if (!_isAlive) {
|
|
50
|
+
throw std::runtime_error("Cannot queue the given task - the ThreadPool has already been stopped!");
|
|
51
|
+
}
|
|
52
|
+
_tasks.emplace(std::move(task));
|
|
53
|
+
}
|
|
54
|
+
// Notify about a new task - one of the workers will pick it up
|
|
55
|
+
_condition.notify_one();
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
ThreadPool::~ThreadPool() {
|
|
59
|
+
Logger::log(TAG, "Destroying ThreadPool \"%s\"...", _name);
|
|
60
|
+
|
|
61
|
+
{
|
|
62
|
+
// Lock and set `_isAlive` to false.
|
|
63
|
+
std::unique_lock<std::mutex> lock(_queueMutex);
|
|
64
|
+
_isAlive = false;
|
|
65
|
+
}
|
|
66
|
+
// Notify all workers - they will stop the work since `_isAlive` is false.
|
|
67
|
+
_condition.notify_all();
|
|
68
|
+
for (std::thread& worker : _workers) {
|
|
69
|
+
// Wait for each worker to exit.
|
|
70
|
+
worker.join();
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
std::shared_ptr<ThreadPool> ThreadPool::getSharedPool() {
|
|
75
|
+
static std::shared_ptr<ThreadPool> shared;
|
|
76
|
+
if (shared == nullptr) {
|
|
77
|
+
int availableThreads = std::thread::hardware_concurrency();
|
|
78
|
+
auto numThreads = std::min(availableThreads, 3);
|
|
79
|
+
shared = std::make_shared<ThreadPool>("nitro-thread", numThreads);
|
|
80
|
+
}
|
|
81
|
+
return shared;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
} // namespace margelo::nitro
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
//
|
|
2
|
+
// ThreadPool.hpp
|
|
3
|
+
// NitroModules
|
|
4
|
+
//
|
|
5
|
+
// Created by Marc Rousavy on 21.06.24.
|
|
6
|
+
//
|
|
7
|
+
|
|
8
|
+
#pragma once
|
|
9
|
+
|
|
10
|
+
#include <atomic>
|
|
11
|
+
#include <condition_variable>
|
|
12
|
+
#include <functional>
|
|
13
|
+
#include <memory>
|
|
14
|
+
#include <mutex>
|
|
15
|
+
#include <queue>
|
|
16
|
+
#include <string>
|
|
17
|
+
#include <thread>
|
|
18
|
+
#include <vector>
|
|
19
|
+
|
|
20
|
+
namespace margelo::nitro {
|
|
21
|
+
|
|
22
|
+
class ThreadPool final {
|
|
23
|
+
public:
|
|
24
|
+
/**
|
|
25
|
+
* Create a new ThreadPool with the given number of fixed workers/threads.
|
|
26
|
+
*/
|
|
27
|
+
explicit ThreadPool(const char* const name, size_t numThreads);
|
|
28
|
+
~ThreadPool();
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Schedules the given task asynchronously on the ThreadPool.
|
|
32
|
+
* It will run once a worker is available.
|
|
33
|
+
*/
|
|
34
|
+
void run(std::function<void()>&& task);
|
|
35
|
+
|
|
36
|
+
public:
|
|
37
|
+
/**
|
|
38
|
+
* Get a static singleton instance - a shared ThreadPool.
|
|
39
|
+
* The shared ThreadPool has 3 threads.
|
|
40
|
+
*/
|
|
41
|
+
static std::shared_ptr<ThreadPool> getSharedPool();
|
|
42
|
+
|
|
43
|
+
private:
|
|
44
|
+
std::vector<std::thread> _workers;
|
|
45
|
+
std::queue<std::function<void()>> _tasks;
|
|
46
|
+
std::mutex _queueMutex;
|
|
47
|
+
std::condition_variable _condition;
|
|
48
|
+
std::atomic<bool> _isAlive;
|
|
49
|
+
const char* _name;
|
|
50
|
+
static constexpr auto TAG = "ThreadPool";
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
} // namespace margelo::nitro
|
|
@@ -1,20 +1,23 @@
|
|
|
1
1
|
//
|
|
2
2
|
// HybridContext.hpp
|
|
3
|
-
//
|
|
3
|
+
// NitroModules
|
|
4
4
|
//
|
|
5
5
|
// Created by Marc Rousavy on 17.07.24.
|
|
6
6
|
//
|
|
7
7
|
|
|
8
8
|
#pragma once
|
|
9
9
|
|
|
10
|
+
namespace margelo::nitro {
|
|
11
|
+
class HybridObject;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
#include "HybridObject.hpp"
|
|
10
15
|
#include "NitroLogger.hpp"
|
|
11
16
|
#include "TypeInfo.hpp"
|
|
12
17
|
#include <memory>
|
|
13
18
|
|
|
14
19
|
namespace margelo::nitro {
|
|
15
20
|
|
|
16
|
-
class HybridObject;
|
|
17
|
-
|
|
18
21
|
/**
|
|
19
22
|
* Represents contextual state for a `HybridObject`.
|
|
20
23
|
*
|
|
@@ -5,3 +5,8 @@ export interface Spec extends TurboModule {
|
|
|
5
5
|
createHybridObject(name: string, args?: UnsafeObject): UnsafeObject;
|
|
6
6
|
}
|
|
7
7
|
export declare function getNativeNitroModules(): Spec;
|
|
8
|
+
declare global {
|
|
9
|
+
var __nitroModulesJSICache: {};
|
|
10
|
+
var __nitroDispatcher: {};
|
|
11
|
+
}
|
|
12
|
+
export declare function isRuntimeAlive(): boolean;
|
|
@@ -4,6 +4,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.getNativeNitroModules = getNativeNitroModules;
|
|
7
|
+
exports.isRuntimeAlive = isRuntimeAlive;
|
|
7
8
|
var _reactNative = require("react-native");
|
|
8
9
|
var _ModuleNotFoundError = require("./ModuleNotFoundError");
|
|
9
10
|
let turboModule;
|
|
@@ -21,4 +22,9 @@ function getNativeNitroModules() {
|
|
|
21
22
|
}
|
|
22
23
|
return turboModule;
|
|
23
24
|
}
|
|
25
|
+
function isRuntimeAlive() {
|
|
26
|
+
const cache = global.__nitroModulesJSICache;
|
|
27
|
+
const dispatcher = global.__nitroDispatcher;
|
|
28
|
+
return cache != null && dispatcher != null;
|
|
29
|
+
}
|
|
24
30
|
//# sourceMappingURL=NativeNitroModules.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["_reactNative","require","_ModuleNotFoundError","turboModule","getNativeNitroModules","TurboModuleRegistry","getEnforcing","install","e","ModuleNotFoundError"],"sourceRoot":"../../src","sources":["NativeNitroModules.ts"],"mappings":"
|
|
1
|
+
{"version":3,"names":["_reactNative","require","_ModuleNotFoundError","turboModule","getNativeNitroModules","TurboModuleRegistry","getEnforcing","install","e","ModuleNotFoundError","isRuntimeAlive","cache","global","__nitroModulesJSICache","dispatcher","__nitroDispatcher"],"sourceRoot":"../../src","sources":["NativeNitroModules.ts"],"mappings":";;;;;;;AACA,IAAAA,YAAA,GAAAC,OAAA;AAEA,IAAAC,oBAAA,GAAAD,OAAA;AAOA,IAAIE,WAA6B;AAC1B,SAASC,qBAAqBA,CAAA,EAAS;EAC5C,IAAID,WAAW,IAAI,IAAI,EAAE;IACvB,IAAI;MACF;MACAA,WAAW,GAAGE,gCAAmB,CAACC,YAAY,CAAO,iBAAiB,CAAC;;MAEvE;MACAH,WAAW,CAACI,OAAO,CAAC,CAAC;IACvB,CAAC,CAAC,OAAOC,CAAC,EAAE;MACV,MAAM,IAAIC,wCAAmB,CAACD,CAAC,CAAC;IAClC;EACF;EAEA,OAAOL,WAAW;AACpB;AAOO,SAASO,cAAcA,CAAA,EAAG;EAC/B,MAAMC,KAAK,GAAGC,MAAM,CAACC,sBAAsB;EAC3C,MAAMC,UAAU,GAAGF,MAAM,CAACG,iBAAiB;EAC3C,OAAOJ,KAAK,IAAI,IAAI,IAAIG,UAAU,IAAI,IAAI;AAC5C","ignoreList":[]}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.getNativeNitroModules = getNativeNitroModules;
|
|
7
|
+
function getNativeNitroModules() {
|
|
8
|
+
throw new Error(`Native NitroModules are not available on web! Make sure you're not calling getNativeNitroModules() in a web (.web.ts) environment.`);
|
|
9
|
+
}
|
|
10
|
+
//# sourceMappingURL=NativeNitroModules.web.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["getNativeNitroModules","Error"],"sourceRoot":"../../src","sources":["NativeNitroModules.web.ts"],"mappings":";;;;;;AAIO,SAASA,qBAAqBA,CAAA,EAAS;EAC5C,MAAM,IAAIC,KAAK,CACb,oIACF,CAAC;AACH","ignoreList":[]}
|
|
@@ -15,4 +15,9 @@ export function getNativeNitroModules() {
|
|
|
15
15
|
}
|
|
16
16
|
return turboModule;
|
|
17
17
|
}
|
|
18
|
+
export function isRuntimeAlive() {
|
|
19
|
+
const cache = global.__nitroModulesJSICache;
|
|
20
|
+
const dispatcher = global.__nitroDispatcher;
|
|
21
|
+
return cache != null && dispatcher != null;
|
|
22
|
+
}
|
|
18
23
|
//# sourceMappingURL=NativeNitroModules.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["TurboModuleRegistry","ModuleNotFoundError","turboModule","getNativeNitroModules","getEnforcing","install","e"],"sourceRoot":"../../src","sources":["NativeNitroModules.ts"],"mappings":"AACA,SAASA,mBAAmB,QAAQ,cAAc;AAElD,SAASC,mBAAmB,QAAQ,uBAAuB;AAO3D,IAAIC,WAA6B;AACjC,OAAO,SAASC,qBAAqBA,CAAA,EAAS;EAC5C,IAAID,WAAW,IAAI,IAAI,EAAE;IACvB,IAAI;MACF;MACAA,WAAW,GAAGF,mBAAmB,CAACI,YAAY,CAAO,iBAAiB,CAAC;;MAEvE;MACAF,WAAW,CAACG,OAAO,CAAC,CAAC;IACvB,CAAC,CAAC,OAAOC,CAAC,EAAE;MACV,MAAM,IAAIL,mBAAmB,CAACK,CAAC,CAAC;IAClC;EACF;EAEA,OAAOJ,WAAW;AACpB","ignoreList":[]}
|
|
1
|
+
{"version":3,"names":["TurboModuleRegistry","ModuleNotFoundError","turboModule","getNativeNitroModules","getEnforcing","install","e","isRuntimeAlive","cache","global","__nitroModulesJSICache","dispatcher","__nitroDispatcher"],"sourceRoot":"../../src","sources":["NativeNitroModules.ts"],"mappings":"AACA,SAASA,mBAAmB,QAAQ,cAAc;AAElD,SAASC,mBAAmB,QAAQ,uBAAuB;AAO3D,IAAIC,WAA6B;AACjC,OAAO,SAASC,qBAAqBA,CAAA,EAAS;EAC5C,IAAID,WAAW,IAAI,IAAI,EAAE;IACvB,IAAI;MACF;MACAA,WAAW,GAAGF,mBAAmB,CAACI,YAAY,CAAO,iBAAiB,CAAC;;MAEvE;MACAF,WAAW,CAACG,OAAO,CAAC,CAAC;IACvB,CAAC,CAAC,OAAOC,CAAC,EAAE;MACV,MAAM,IAAIL,mBAAmB,CAACK,CAAC,CAAC;IAClC;EACF;EAEA,OAAOJ,WAAW;AACpB;AAOA,OAAO,SAASK,cAAcA,CAAA,EAAG;EAC/B,MAAMC,KAAK,GAAGC,MAAM,CAACC,sBAAsB;EAC3C,MAAMC,UAAU,GAAGF,MAAM,CAACG,iBAAiB;EAC3C,OAAOJ,KAAK,IAAI,IAAI,IAAIG,UAAU,IAAI,IAAI;AAC5C","ignoreList":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["getNativeNitroModules","Error"],"sourceRoot":"../../src","sources":["NativeNitroModules.web.ts"],"mappings":"AAIA,OAAO,SAASA,qBAAqBA,CAAA,EAAS;EAC5C,MAAM,IAAIC,KAAK,CACb,oIACF,CAAC;AACH","ignoreList":[]}
|