react-native-nitro-modules 0.31.3 → 0.31.5
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/src/main/cpp/core/JAnyMap.hpp +30 -3
- package/android/src/main/cpp/core/JAnyValue.hpp +2 -2
- package/android/src/main/java/com/margelo/nitro/core/AnyMap.kt +48 -0
- package/android/src/main/java/com/margelo/nitro/core/AnyValue.kt +58 -0
- package/android/src/main/java/com/margelo/nitro/core/Promise.kt +16 -0
- package/cpp/core/AnyMap.cpp +13 -0
- package/cpp/core/AnyMap.hpp +16 -10
- package/cpp/utils/NitroDefines.hpp +1 -1
- package/ios/core/AnyMap.swift +126 -9
- package/ios/core/Promise.swift +17 -0
- package/package.json +1 -1
|
@@ -29,6 +29,12 @@ public:
|
|
|
29
29
|
static jni::local_ref<JAnyMap::jhybriddata> initHybrid(jni::alias_ref<jhybridobject>) {
|
|
30
30
|
return makeCxxInstance();
|
|
31
31
|
}
|
|
32
|
+
/**
|
|
33
|
+
* Create a new, empty `AnyMap` with the given preallocated size from Java.
|
|
34
|
+
*/
|
|
35
|
+
static jni::local_ref<JAnyMap::jhybriddata> initHybridPreallocatedSize(jni::alias_ref<jhybridobject>, jint preallocatedSize) {
|
|
36
|
+
return makeCxxInstance(preallocatedSize);
|
|
37
|
+
}
|
|
32
38
|
/**
|
|
33
39
|
* Create a new `JAnyMap` from an existing `AnyMap`.
|
|
34
40
|
*/
|
|
@@ -38,7 +44,10 @@ public:
|
|
|
38
44
|
|
|
39
45
|
private:
|
|
40
46
|
JAnyMap() {
|
|
41
|
-
_map =
|
|
47
|
+
_map = AnyMap::make();
|
|
48
|
+
}
|
|
49
|
+
JAnyMap(jint preallocatedSize) {
|
|
50
|
+
_map = AnyMap::make(static_cast<size_t>(preallocatedSize));
|
|
42
51
|
}
|
|
43
52
|
explicit JAnyMap(const std::shared_ptr<AnyMap>& map) : _map(map) {}
|
|
44
53
|
|
|
@@ -100,7 +109,7 @@ protected:
|
|
|
100
109
|
std::string getString(const std::string& key) {
|
|
101
110
|
return _map->getString(key);
|
|
102
111
|
}
|
|
103
|
-
jni::
|
|
112
|
+
jni::local_ref<JAnyArray> getAnyArray(const std::string& key) {
|
|
104
113
|
const auto& vector = _map->getArray(key);
|
|
105
114
|
auto javaArray = jni::JArrayClass<JAnyValue::javaobject>::newArray(vector.size());
|
|
106
115
|
for (size_t i = 0; i < vector.size(); i++) {
|
|
@@ -109,7 +118,7 @@ protected:
|
|
|
109
118
|
}
|
|
110
119
|
return javaArray;
|
|
111
120
|
}
|
|
112
|
-
jni::
|
|
121
|
+
jni::local_ref<JAnyObject> getAnyObject(const std::string& key) {
|
|
113
122
|
const auto& map = _map->getObject(key);
|
|
114
123
|
auto javaMap = jni::JHashMap<jni::JString, JAnyValue::javaobject>::create(map.size());
|
|
115
124
|
for (const auto& entry : map) {
|
|
@@ -119,6 +128,10 @@ protected:
|
|
|
119
128
|
}
|
|
120
129
|
return javaMap;
|
|
121
130
|
}
|
|
131
|
+
jni::local_ref<JAnyValue::javaobject> getAnyValue(const std::string& key) {
|
|
132
|
+
const auto& any = _map->getAny(key);
|
|
133
|
+
return JAnyValue::create(any);
|
|
134
|
+
}
|
|
122
135
|
|
|
123
136
|
protected:
|
|
124
137
|
void setNull(const std::string& key) {
|
|
@@ -154,8 +167,17 @@ protected:
|
|
|
154
167
|
}
|
|
155
168
|
_map->setObject(key, map);
|
|
156
169
|
}
|
|
170
|
+
void setAnyValue(const std::string& key, const jni::alias_ref<JAnyValue::javaobject>& value) {
|
|
171
|
+
_map->setAny(key, value->cthis()->getValue());
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
protected:
|
|
175
|
+
void merge(jni::alias_ref<JAnyMap::javaobject> other) {
|
|
176
|
+
_map->merge(other->cthis()->_map);
|
|
177
|
+
}
|
|
157
178
|
|
|
158
179
|
public:
|
|
180
|
+
[[nodiscard]]
|
|
159
181
|
std::shared_ptr<AnyMap> getMap() const {
|
|
160
182
|
return _map;
|
|
161
183
|
}
|
|
@@ -170,6 +192,7 @@ public:
|
|
|
170
192
|
registerHybrid({
|
|
171
193
|
// init
|
|
172
194
|
makeNativeMethod("initHybrid", JAnyMap::initHybrid),
|
|
195
|
+
makeNativeMethod("initHybrid", JAnyMap::initHybridPreallocatedSize),
|
|
173
196
|
// helpers
|
|
174
197
|
makeNativeMethod("contains", JAnyMap::contains),
|
|
175
198
|
makeNativeMethod("remove", JAnyMap::remove),
|
|
@@ -190,6 +213,7 @@ public:
|
|
|
190
213
|
makeNativeMethod("getString", JAnyMap::getString),
|
|
191
214
|
makeNativeMethod("getAnyArray", JAnyMap::getAnyArray),
|
|
192
215
|
makeNativeMethod("getAnyObject", JAnyMap::getAnyObject),
|
|
216
|
+
makeNativeMethod("getAnyValue", JAnyMap::getAnyValue),
|
|
193
217
|
// set
|
|
194
218
|
makeNativeMethod("setNull", JAnyMap::setNull),
|
|
195
219
|
makeNativeMethod("setDouble", JAnyMap::setDouble),
|
|
@@ -198,6 +222,9 @@ public:
|
|
|
198
222
|
makeNativeMethod("setString", JAnyMap::setString),
|
|
199
223
|
makeNativeMethod("setAnyArray", JAnyMap::setAnyArray),
|
|
200
224
|
makeNativeMethod("setAnyObject", JAnyMap::setAnyObject),
|
|
225
|
+
makeNativeMethod("setAnyValue", JAnyMap::setAnyValue),
|
|
226
|
+
// merge
|
|
227
|
+
makeNativeMethod("merge", JAnyMap::merge),
|
|
201
228
|
});
|
|
202
229
|
}
|
|
203
230
|
};
|
|
@@ -125,7 +125,7 @@ protected:
|
|
|
125
125
|
std::string asString() {
|
|
126
126
|
return std::get<std::string>(_value);
|
|
127
127
|
}
|
|
128
|
-
jni::
|
|
128
|
+
jni::local_ref<JAnyArray> asAnyArray() {
|
|
129
129
|
auto vector = std::get<AnyArray>(_value);
|
|
130
130
|
auto javaArray = jni::JArrayClass<JAnyValue::javaobject>::newArray(vector.size());
|
|
131
131
|
for (size_t i = 0; i < vector.size(); i++) {
|
|
@@ -134,7 +134,7 @@ protected:
|
|
|
134
134
|
}
|
|
135
135
|
return javaArray;
|
|
136
136
|
}
|
|
137
|
-
jni::
|
|
137
|
+
jni::local_ref<JAnyObject> asAnyObject() {
|
|
138
138
|
auto map = std::get<AnyObject>(_value);
|
|
139
139
|
auto javaMap = jni::JHashMap<jni::JString, JAnyValue::javaobject>::create(map.size());
|
|
140
140
|
for (const auto& entry : map) {
|
|
@@ -4,6 +4,7 @@ import androidx.annotation.Keep
|
|
|
4
4
|
import com.facebook.jni.HybridData
|
|
5
5
|
import com.facebook.proguard.annotations.DoNotStrip
|
|
6
6
|
import dalvik.annotation.optimization.FastNative
|
|
7
|
+
import java.util.Dictionary
|
|
7
8
|
|
|
8
9
|
/**
|
|
9
10
|
* Represents an untyped map of string keys with associated values.
|
|
@@ -22,6 +23,13 @@ class AnyMap {
|
|
|
22
23
|
mHybridData = initHybrid()
|
|
23
24
|
}
|
|
24
25
|
|
|
26
|
+
/**
|
|
27
|
+
* Create a new empty `AnyMap` with the given preallocated size
|
|
28
|
+
*/
|
|
29
|
+
constructor(preallocatedSize: Int) {
|
|
30
|
+
mHybridData = initHybrid(preallocatedSize)
|
|
31
|
+
}
|
|
32
|
+
|
|
25
33
|
/**
|
|
26
34
|
* Create a new `AnyMap` from C++, which potentially already holds data.
|
|
27
35
|
*/
|
|
@@ -30,6 +38,35 @@ class AnyMap {
|
|
|
30
38
|
mHybridData = hybridData
|
|
31
39
|
}
|
|
32
40
|
|
|
41
|
+
companion object {
|
|
42
|
+
fun fromMap(map: Map<String, Any?>): AnyMap {
|
|
43
|
+
val anyMap = AnyMap(map.size)
|
|
44
|
+
for ((key, value) in map) {
|
|
45
|
+
anyMap.setAny(key, value)
|
|
46
|
+
}
|
|
47
|
+
return anyMap
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
fun toMap(): Map<String, Any?> {
|
|
52
|
+
val map = HashMap<String, Any?>()
|
|
53
|
+
for (key in getAllKeys()) {
|
|
54
|
+
map.put(key, getAny(key))
|
|
55
|
+
}
|
|
56
|
+
return map
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
fun setAny(
|
|
60
|
+
key: String,
|
|
61
|
+
value: Any?,
|
|
62
|
+
) {
|
|
63
|
+
setAnyValue(key, AnyValue.fromAny(value))
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
fun getAny(key: String): Any? {
|
|
67
|
+
return getAnyValue(key).toAny()
|
|
68
|
+
}
|
|
69
|
+
|
|
33
70
|
@FastNative
|
|
34
71
|
external fun contains(key: String): Boolean
|
|
35
72
|
|
|
@@ -77,6 +114,8 @@ class AnyMap {
|
|
|
77
114
|
|
|
78
115
|
external fun getAnyObject(key: String): AnyObject
|
|
79
116
|
|
|
117
|
+
private external fun getAnyValue(key: String): AnyValue
|
|
118
|
+
|
|
80
119
|
@FastNative
|
|
81
120
|
external fun setNull(key: String)
|
|
82
121
|
|
|
@@ -114,5 +153,14 @@ class AnyMap {
|
|
|
114
153
|
value: AnyObject,
|
|
115
154
|
)
|
|
116
155
|
|
|
156
|
+
private external fun setAnyValue(
|
|
157
|
+
key: String,
|
|
158
|
+
value: AnyValue,
|
|
159
|
+
)
|
|
160
|
+
|
|
161
|
+
external fun merge(other: AnyMap)
|
|
162
|
+
|
|
117
163
|
private external fun initHybrid(): HybridData
|
|
164
|
+
|
|
165
|
+
private external fun initHybrid(preallocatedSize: Int): HybridData
|
|
118
166
|
}
|
|
@@ -31,6 +31,12 @@ class AnyValue {
|
|
|
31
31
|
mHybridData = initHybrid()
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
+
@DoNotStrip
|
|
35
|
+
@Keep
|
|
36
|
+
private constructor(mHybridData: HybridData) {
|
|
37
|
+
this.mHybridData = mHybridData
|
|
38
|
+
}
|
|
39
|
+
|
|
34
40
|
/**
|
|
35
41
|
* Create a new [AnyValue] that holds the given [Double]
|
|
36
42
|
*/
|
|
@@ -167,4 +173,56 @@ class AnyValue {
|
|
|
167
173
|
private external fun initHybrid(value: AnyArray): HybridData
|
|
168
174
|
|
|
169
175
|
private external fun initHybrid(value: AnyObject): HybridData
|
|
176
|
+
|
|
177
|
+
fun toAny(): Any? {
|
|
178
|
+
if (isNull()) {
|
|
179
|
+
return null
|
|
180
|
+
} else if (isDouble()) {
|
|
181
|
+
return asDouble()
|
|
182
|
+
} else if (isBigInt()) {
|
|
183
|
+
return asBigInt()
|
|
184
|
+
} else if (isBoolean()) {
|
|
185
|
+
return asBoolean()
|
|
186
|
+
} else if (isString()) {
|
|
187
|
+
return asString()
|
|
188
|
+
} else if (isAnyArray()) {
|
|
189
|
+
val mapped = asAnyArray().map { it.toAny() }
|
|
190
|
+
return mapped.toTypedArray()
|
|
191
|
+
} else if (isAnyObject()) {
|
|
192
|
+
val mapped = asAnyObject().mapValues { (key, value) -> value.toAny() }
|
|
193
|
+
return mapped
|
|
194
|
+
} else {
|
|
195
|
+
throw Error("AnyValue holds unknown type!")
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
companion object {
|
|
200
|
+
fun fromAny(value: Any?): AnyValue {
|
|
201
|
+
when (value) {
|
|
202
|
+
null -> return AnyValue()
|
|
203
|
+
is Double -> return AnyValue(value)
|
|
204
|
+
is Float -> return AnyValue(value.toDouble())
|
|
205
|
+
is Int -> return AnyValue(value.toDouble())
|
|
206
|
+
is Boolean -> return AnyValue(value)
|
|
207
|
+
is Long -> return AnyValue(value)
|
|
208
|
+
is String -> return AnyValue(value)
|
|
209
|
+
is Array<*> -> {
|
|
210
|
+
val mapped = value.map { v -> AnyValue.fromAny(v) }
|
|
211
|
+
return AnyValue(mapped.toTypedArray())
|
|
212
|
+
}
|
|
213
|
+
is List<*> -> {
|
|
214
|
+
val mapped = value.map { v -> AnyValue.fromAny(v) }
|
|
215
|
+
return AnyValue(mapped.toTypedArray())
|
|
216
|
+
}
|
|
217
|
+
is Map<*, *> -> {
|
|
218
|
+
val mapped = value.map { (k, v) -> k.toString() to AnyValue.fromAny(v) }
|
|
219
|
+
return AnyValue(mapped.toMap())
|
|
220
|
+
}
|
|
221
|
+
is AnyValue, is AnyMap -> {
|
|
222
|
+
throw Error("Cannot box AnyValue ($value) twice!")
|
|
223
|
+
}
|
|
224
|
+
else -> throw Error("Value \"$value\" cannot be represented as AnyValue!")
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
}
|
|
170
228
|
}
|
|
@@ -187,3 +187,19 @@ class Promise<T> {
|
|
|
187
187
|
}
|
|
188
188
|
}
|
|
189
189
|
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Resolves this `Promise<Unit>`.
|
|
193
|
+
* @since void overload
|
|
194
|
+
*/
|
|
195
|
+
fun Promise<Unit>.resolve() {
|
|
196
|
+
resolve(Unit)
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Create an already resolved `Promise<Unit>`.
|
|
201
|
+
* @since void overload
|
|
202
|
+
*/
|
|
203
|
+
fun Promise.Companion.resolved(): Promise<Unit> {
|
|
204
|
+
return Promise.resolved(Unit)
|
|
205
|
+
}
|
package/cpp/core/AnyMap.cpp
CHANGED
|
@@ -154,6 +154,13 @@ AnyObject AnyMap::getObject(const std::string& key) const {
|
|
|
154
154
|
throw std::runtime_error("The value at key \"" + key + "\" is not an object!");
|
|
155
155
|
}
|
|
156
156
|
}
|
|
157
|
+
AnyValue AnyMap::getAny(const std::string& key) const {
|
|
158
|
+
auto found = _map.find(key);
|
|
159
|
+
if (found == _map.end()) {
|
|
160
|
+
throw std::runtime_error("The key \"" + key + "\" does not exist in this Map!");
|
|
161
|
+
}
|
|
162
|
+
return found->second;
|
|
163
|
+
}
|
|
157
164
|
|
|
158
165
|
// Set
|
|
159
166
|
void AnyMap::setNull(const std::string& key) {
|
|
@@ -186,4 +193,10 @@ const std::unordered_map<std::string, AnyValue>& AnyMap::getMap() const {
|
|
|
186
193
|
return _map;
|
|
187
194
|
}
|
|
188
195
|
|
|
196
|
+
void AnyMap::merge(const std::shared_ptr<AnyMap>& other) {
|
|
197
|
+
for (auto& [key, value] : other->_map) {
|
|
198
|
+
_map[key] = value;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
189
202
|
} // namespace margelo::nitro
|
package/cpp/core/AnyMap.hpp
CHANGED
|
@@ -36,15 +36,9 @@ struct AnyValue : VariantType {
|
|
|
36
36
|
* 3. Objects of primitives
|
|
37
37
|
*/
|
|
38
38
|
class AnyMap final {
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* Create a new instance of AnyMap.
|
|
42
|
-
*/
|
|
39
|
+
private:
|
|
43
40
|
explicit AnyMap() {}
|
|
44
|
-
|
|
45
|
-
* Create a new instance of AnyMap with the given amount of spaces pre-allocated.
|
|
46
|
-
*/
|
|
47
|
-
explicit AnyMap(size_t size) {
|
|
41
|
+
AnyMap(size_t size) {
|
|
48
42
|
_map.reserve(size);
|
|
49
43
|
}
|
|
50
44
|
|
|
@@ -53,13 +47,13 @@ public:
|
|
|
53
47
|
* Create a new `shared_ptr` instance of AnyMap.
|
|
54
48
|
*/
|
|
55
49
|
static std::shared_ptr<AnyMap> make() {
|
|
56
|
-
return std::
|
|
50
|
+
return std::shared_ptr<AnyMap>(new AnyMap());
|
|
57
51
|
}
|
|
58
52
|
/**
|
|
59
53
|
* Create a new `shared_ptr` instance of AnyMap with the given amount of spaces pre-allocated.
|
|
60
54
|
*/
|
|
61
55
|
static std::shared_ptr<AnyMap> make(size_t size) {
|
|
62
|
-
return std::
|
|
56
|
+
return std::shared_ptr<AnyMap>(new AnyMap(size));
|
|
63
57
|
}
|
|
64
58
|
|
|
65
59
|
public:
|
|
@@ -154,6 +148,12 @@ public:
|
|
|
154
148
|
*/
|
|
155
149
|
AnyObject getObject(const std::string& key) const;
|
|
156
150
|
|
|
151
|
+
/**
|
|
152
|
+
* Get the value at the given `key` as it's boxed `AnyValue`.
|
|
153
|
+
* If no object value exists at the given `key`, this method will throw.
|
|
154
|
+
*/
|
|
155
|
+
AnyValue getAny(const std::string& key) const;
|
|
156
|
+
|
|
157
157
|
public:
|
|
158
158
|
/**
|
|
159
159
|
* Set the value at the given key to `null`.
|
|
@@ -202,6 +202,12 @@ public:
|
|
|
202
202
|
*/
|
|
203
203
|
const std::unordered_map<std::string, AnyValue>& getMap() const;
|
|
204
204
|
|
|
205
|
+
public:
|
|
206
|
+
/**
|
|
207
|
+
* Merge all keys and values from given `other` `AnyMap` into this `AnyMap`.
|
|
208
|
+
*/
|
|
209
|
+
void merge(const std::shared_ptr<AnyMap>& other);
|
|
210
|
+
|
|
205
211
|
private:
|
|
206
212
|
std::unordered_map<std::string, AnyValue> _map;
|
|
207
213
|
} SWIFT_NONCOPYABLE;
|
package/ios/core/AnyMap.swift
CHANGED
|
@@ -28,7 +28,7 @@ public indirect enum AnyValue {
|
|
|
28
28
|
} else if margelo.nitro.AnyMapUtils.is_AnyValue_bigint(value) {
|
|
29
29
|
return .bigint(margelo.nitro.AnyMapUtils.get_AnyValue_bigint(value))
|
|
30
30
|
} else if margelo.nitro.AnyMapUtils.is_AnyValue_string(value) {
|
|
31
|
-
return .string(margelo.nitro.AnyMapUtils.get_AnyValue_string(value)
|
|
31
|
+
return .string(String(margelo.nitro.AnyMapUtils.get_AnyValue_string(value)))
|
|
32
32
|
} else if margelo.nitro.AnyMapUtils.is_AnyValue_AnyArray(value) {
|
|
33
33
|
return .array(margelo.nitro.AnyMapUtils.get_AnyValue_AnyArray(value).toSwift())
|
|
34
34
|
} else if margelo.nitro.AnyMapUtils.is_AnyValue_AnyObject(value) {
|
|
@@ -151,6 +151,17 @@ public final class AnyMap: @unchecked Sendable {
|
|
|
151
151
|
return value.toSwift()
|
|
152
152
|
}
|
|
153
153
|
|
|
154
|
+
/**
|
|
155
|
+
* Gets the value value at the given key.
|
|
156
|
+
* If no value exists at the given key,
|
|
157
|
+
* this function throws.
|
|
158
|
+
*/
|
|
159
|
+
public func getAny(key: String) -> Any? {
|
|
160
|
+
let value = cppPart.pointee.getAny(std.string(key))
|
|
161
|
+
let any = AnyValue.create(value)
|
|
162
|
+
return any.toAny()
|
|
163
|
+
}
|
|
164
|
+
|
|
154
165
|
// pragma MARK: Setters
|
|
155
166
|
|
|
156
167
|
/**
|
|
@@ -202,6 +213,15 @@ public final class AnyMap: @unchecked Sendable {
|
|
|
202
213
|
cppPart.pointee.setObject(std.string(key), margelo.nitro.AnyObject.create(value))
|
|
203
214
|
}
|
|
204
215
|
|
|
216
|
+
/**
|
|
217
|
+
* Set the given key to the given any value.
|
|
218
|
+
*/
|
|
219
|
+
public func setAny(key: String, value: Any?) throws {
|
|
220
|
+
let swiftAny = try AnyValue.fromAny(value)
|
|
221
|
+
let cppAny = margelo.nitro.AnyValue.create(swiftAny)
|
|
222
|
+
cppPart.pointee.setAny(std.string(key), cppAny)
|
|
223
|
+
}
|
|
224
|
+
|
|
205
225
|
// pragma MARK: Is Getters
|
|
206
226
|
|
|
207
227
|
/**
|
|
@@ -252,6 +272,13 @@ public final class AnyMap: @unchecked Sendable {
|
|
|
252
272
|
public func isObject(key: String) -> Bool {
|
|
253
273
|
return cppPart.pointee.isObject(std.string(key))
|
|
254
274
|
}
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* Merges all keys and values from the `other` map into this map.
|
|
278
|
+
*/
|
|
279
|
+
public func merge(other: AnyMap) {
|
|
280
|
+
cppPart.pointee.merge(other.cppPart)
|
|
281
|
+
}
|
|
255
282
|
}
|
|
256
283
|
|
|
257
284
|
// pragma MARK: margelo.nitro.AnyValue extension
|
|
@@ -298,14 +325,6 @@ extension margelo.nitro.AnyValue {
|
|
|
298
325
|
}
|
|
299
326
|
}
|
|
300
327
|
|
|
301
|
-
// pragma MARK: std.string extension
|
|
302
|
-
|
|
303
|
-
extension std.string {
|
|
304
|
-
func toSwift() -> String {
|
|
305
|
-
return String(self)
|
|
306
|
-
}
|
|
307
|
-
}
|
|
308
|
-
|
|
309
328
|
// pragma MARK: margelo.nitro.AnyArray extension
|
|
310
329
|
|
|
311
330
|
extension margelo.nitro.AnyArray {
|
|
@@ -351,5 +370,103 @@ extension margelo.nitro.AnyObject {
|
|
|
351
370
|
}
|
|
352
371
|
}
|
|
353
372
|
|
|
373
|
+
// pragma MARK: AnyValue <-> Any extension
|
|
374
|
+
|
|
375
|
+
extension AnyValue {
|
|
376
|
+
public static func fromAny(_ any: Any?) throws -> AnyValue {
|
|
377
|
+
switch any {
|
|
378
|
+
case nil:
|
|
379
|
+
return AnyValue.null
|
|
380
|
+
case let value as String:
|
|
381
|
+
return AnyValue.string(value)
|
|
382
|
+
case let value as Double:
|
|
383
|
+
return AnyValue.number(value)
|
|
384
|
+
case let value as Int:
|
|
385
|
+
return AnyValue.number(Double(value))
|
|
386
|
+
case let value as Float:
|
|
387
|
+
return AnyValue.number(Double(value))
|
|
388
|
+
case let value as Int64:
|
|
389
|
+
return AnyValue.bigint(value)
|
|
390
|
+
case let value as Bool:
|
|
391
|
+
return AnyValue.bool(value)
|
|
392
|
+
case let value as [Any]:
|
|
393
|
+
let array = try value.map { try AnyValue.fromAny($0) }
|
|
394
|
+
return AnyValue.array(array)
|
|
395
|
+
case let value as [String: Any]:
|
|
396
|
+
let map = try value.mapValues { try AnyValue.fromAny($0) }
|
|
397
|
+
return AnyValue.object(map)
|
|
398
|
+
case is AnyValue, is AnyMap, is margelo.nitro.AnyValue, is margelo.nitro.AnyArray,
|
|
399
|
+
is margelo.nitro.AnyObject:
|
|
400
|
+
throw RuntimeError.error(
|
|
401
|
+
withMessage: "Cannot box AnyValue (\(String(describing: any))) twice!")
|
|
402
|
+
default:
|
|
403
|
+
throw RuntimeError.error(
|
|
404
|
+
withMessage: "Value \(String(describing: any)) cannot be represented as AnyValue!")
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
public func toAny() -> Any? {
|
|
409
|
+
switch self {
|
|
410
|
+
case .null:
|
|
411
|
+
return nil
|
|
412
|
+
case .bigint(let int):
|
|
413
|
+
return int
|
|
414
|
+
case .bool(let bool):
|
|
415
|
+
return bool
|
|
416
|
+
case .number(let double):
|
|
417
|
+
return double
|
|
418
|
+
case .array(let array):
|
|
419
|
+
return array.map { $0.toAny() }
|
|
420
|
+
case .object(let object):
|
|
421
|
+
return object.mapValues { $0.toAny() }
|
|
422
|
+
case .string(let string):
|
|
423
|
+
return string
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
// pragma MARK: AnyMap <-> Any extension
|
|
429
|
+
|
|
430
|
+
extension AnyMap {
|
|
431
|
+
/**
|
|
432
|
+
* Convert the given `Dictionary<String, Any?>` to an `AnyMap`
|
|
433
|
+
* by copying all keys and values into the C++ container.
|
|
434
|
+
* If a key cannot be wrapped as any, this will throw.
|
|
435
|
+
*/
|
|
436
|
+
public static func fromDictionary(_ dictionary: [String: Any?]) throws -> AnyMap {
|
|
437
|
+
let map = AnyMap(withPreallocatedSize: dictionary.count)
|
|
438
|
+
for (key, value) in dictionary {
|
|
439
|
+
try map.setAny(key: key, value: value)
|
|
440
|
+
}
|
|
441
|
+
return map
|
|
442
|
+
}
|
|
443
|
+
/**
|
|
444
|
+
* Convert the given `Dictionary<String, Any?>` to an `AnyMap`
|
|
445
|
+
* by copying all keys and values into the C++ container.
|
|
446
|
+
* If a key cannot be wrapped as any, it will simply be ignored and omitted.
|
|
447
|
+
*/
|
|
448
|
+
public static func fromDictionaryIgnoreIncompatible(_ dictionary: [String: Any?]) -> AnyMap {
|
|
449
|
+
let map = AnyMap(withPreallocatedSize: dictionary.count)
|
|
450
|
+
for (key, value) in dictionary {
|
|
451
|
+
try? map.setAny(key: key, value: value)
|
|
452
|
+
}
|
|
453
|
+
return map
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
/**
|
|
457
|
+
* Convert this `AnyMap` instance to a `Dictionary<String, Any?>`
|
|
458
|
+
* by copying all keys and values into a wrapped Swift container.
|
|
459
|
+
*/
|
|
460
|
+
public func toDictionary() -> [String: Any?] {
|
|
461
|
+
var dictionary: [String: Any?] = [:]
|
|
462
|
+
let keys = self.getAllKeys()
|
|
463
|
+
dictionary.reserveCapacity(keys.count)
|
|
464
|
+
for key in keys {
|
|
465
|
+
dictionary[key] = self.getAny(key: key)
|
|
466
|
+
}
|
|
467
|
+
return dictionary
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
|
|
354
471
|
@available(*, deprecated, renamed: "AnyMap")
|
|
355
472
|
public typealias AnyMapHolder = AnyMap
|
package/ios/core/Promise.swift
CHANGED
|
@@ -108,6 +108,7 @@ extension Promise {
|
|
|
108
108
|
/**
|
|
109
109
|
* Create a new `Promise<T>` that runs the given `run` function on a parallel Thread/`DispatchQueue`.
|
|
110
110
|
*/
|
|
111
|
+
@preconcurrency
|
|
111
112
|
public static func parallel(
|
|
112
113
|
_ queue: DispatchQueue = .global(),
|
|
113
114
|
_ run: @escaping @Sendable () throws -> T
|
|
@@ -125,6 +126,22 @@ extension Promise {
|
|
|
125
126
|
}
|
|
126
127
|
}
|
|
127
128
|
|
|
129
|
+
/// Void overloads to avoid typing out `()`
|
|
130
|
+
extension Promise where T == Void {
|
|
131
|
+
/**
|
|
132
|
+
* Resolves this `Promise<Void>`.
|
|
133
|
+
*/
|
|
134
|
+
public func resolve() {
|
|
135
|
+
return self.resolve(withResult: ())
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Create an already resolved `Promise<Void>`.
|
|
139
|
+
*/
|
|
140
|
+
public static func resolved() -> Promise {
|
|
141
|
+
return Self.resolved(withResult: ())
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
128
145
|
/// Extensions to support then/catch syntax.
|
|
129
146
|
extension Promise {
|
|
130
147
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-native-nitro-modules",
|
|
3
|
-
"version": "0.31.
|
|
3
|
+
"version": "0.31.5",
|
|
4
4
|
"description": "Insanely fast native C++, Swift or Kotlin modules with a statically compiled binding layer to JSI.",
|
|
5
5
|
"main": "lib/commonjs/index",
|
|
6
6
|
"module": "lib/module/index",
|