react-native-unistyles 2.32.0 → 2.41.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.
- package/android/src/main/cxx/cpp-adapter.cpp +7 -1
- package/cxx/UnistylesModel.cpp +25 -21
- package/cxx/UnistylesModel.h +4 -6
- package/cxx/UnistylesRuntime.h +1 -2
- package/ios/UnistylesModule.h +1 -0
- package/ios/UnistylesModule.mm +10 -3
- package/ios/platform/Platform_iOS.h +1 -0
- package/ios/platform/Platform_iOS.mm +5 -1
- package/ios/platform/Platform_macOS.h +1 -0
- package/ios/platform/Platform_macOS.mm +4 -0
- package/ios/platform/Platform_tvOS.h +1 -0
- package/ios/platform/Platform_tvOS.mm +4 -0
- package/ios/platform/Platform_visionOS.h +1 -0
- package/ios/platform/Platform_visionOS.mm +4 -0
- package/package.json +1 -1
@@ -27,7 +27,13 @@ Java_com_unistyles_UnistylesModule_nativeInstall(JNIEnv *env, jobject thiz, jlon
|
|
27
27
|
return throwKotlinException(env, "Something went wrong while initializing UnistylesModule");
|
28
28
|
}
|
29
29
|
|
30
|
-
|
30
|
+
auto runOnJSThread = [callInvoker](std::function<void(jsi::Runtime&)>&& callback) {
|
31
|
+
callInvoker->invokeAsync([callback = std::move(callback)](jsi::Runtime &rt) {
|
32
|
+
callback(rt);
|
33
|
+
});
|
34
|
+
};
|
35
|
+
|
36
|
+
unistylesRuntime = std::make_shared<UnistylesRuntime>(runOnJSThread);
|
31
37
|
makeShared(env, unistylesModule, unistylesRuntime);
|
32
38
|
|
33
39
|
jsi::Object hostObject = jsi::Object::createFromHostObject(*runtime, unistylesRuntime);
|
package/cxx/UnistylesModel.cpp
CHANGED
@@ -110,29 +110,27 @@ std::vector<std::pair<std::string, double>> UnistylesModel::toSortedBreakpointPa
|
|
110
110
|
// ref: https://github.com/facebook/react-native/pull/43375
|
111
111
|
// ref: https://github.com/facebook/react-native/blob/b5fd041917d197f256433a41a126f0dff767c429/packages/react-native/ReactCommon/react/nativemodule/core/ReactCommon/TurboModule.cpp#L42
|
112
112
|
void UnistylesModel::emitDeviceEvent(const std::string eventType, EventPayload payload) {
|
113
|
-
this->
|
114
|
-
jsi::Value emitter =
|
113
|
+
this->runOnJSThread([this, &eventType, payload = std::move(payload)](jsi::Runtime& rt){
|
114
|
+
jsi::Value emitter = rt.global().getProperty(rt, "__rctDeviceEventEmitter");
|
115
115
|
|
116
116
|
if (emitter.isUndefined()) {
|
117
117
|
return;
|
118
118
|
}
|
119
119
|
|
120
|
-
jsi::Object emitterObject = emitter.asObject(
|
121
|
-
jsi::Function emitFunction = emitterObject.getPropertyAsFunction(
|
120
|
+
jsi::Object emitterObject = emitter.asObject(rt);
|
121
|
+
jsi::Function emitFunction = emitterObject.getPropertyAsFunction(rt, "emit");
|
122
122
|
|
123
123
|
std::vector<jsi::Value> arguments;
|
124
|
-
jsi::Object event = jsi::Object(
|
124
|
+
jsi::Object event = jsi::Object(rt);
|
125
|
+
jsi::Object eventPayload = this->parseEventPayload(rt, payload);
|
125
126
|
|
126
|
-
event.setProperty(
|
127
|
+
event.setProperty(rt, "type", jsi::String::createFromUtf8(rt, std::move(eventType)));
|
128
|
+
event.setProperty(rt, "payload", std::move(eventPayload));
|
127
129
|
|
128
|
-
jsi::
|
129
|
-
|
130
|
-
event.setProperty(this->runtime, "payload", eventPayload);
|
131
|
-
|
132
|
-
arguments.emplace_back(jsi::String::createFromAscii(runtime, "__unistylesOnChange"));
|
130
|
+
arguments.emplace_back(jsi::String::createFromAscii(rt, "__unistylesOnChange"));
|
133
131
|
arguments.emplace_back(std::move(event));
|
134
132
|
|
135
|
-
emitFunction.callWithThis(
|
133
|
+
emitFunction.callWithThis(rt, emitterObject, (const jsi::Value*)arguments.data(), arguments.size());
|
136
134
|
});
|
137
135
|
}
|
138
136
|
|
@@ -193,36 +191,42 @@ void UnistylesModel::onLayoutChange() {
|
|
193
191
|
this->emitDeviceEvent("layout", payload);
|
194
192
|
}
|
195
193
|
|
196
|
-
jsi::Object UnistylesModel::parseEventPayload(EventPayload payload) {
|
197
|
-
jsi::Object eventPayload = jsi::Object(
|
194
|
+
jsi::Object UnistylesModel::parseEventPayload(jsi::Runtime& rt, const EventPayload& payload) {
|
195
|
+
jsi::Object eventPayload = jsi::Object(rt);
|
198
196
|
|
199
197
|
for (const auto& [key, value] : payload) {
|
200
198
|
if (std::holds_alternative<std::string>(value)) {
|
201
|
-
eventPayload.setProperty(
|
199
|
+
eventPayload.setProperty(rt, key.c_str(), jsi::String::createFromUtf8(rt, std::get<std::string>(value)));
|
200
|
+
|
201
|
+
continue;
|
202
202
|
}
|
203
203
|
|
204
204
|
if (std::holds_alternative<int>(value)) {
|
205
|
-
eventPayload.setProperty(
|
205
|
+
eventPayload.setProperty(rt, key.c_str(), std::get<int>(value));
|
206
|
+
|
207
|
+
continue;
|
206
208
|
}
|
207
209
|
|
208
210
|
if (std::holds_alternative<EventNestedValue>(value)) {
|
209
|
-
eventPayload.setProperty(
|
211
|
+
eventPayload.setProperty(rt, key.c_str(), this->parseEventNestedPayload(rt, std::get<EventNestedValue>(value)));
|
212
|
+
|
213
|
+
continue;
|
210
214
|
}
|
211
215
|
}
|
212
216
|
|
213
217
|
return eventPayload;
|
214
218
|
}
|
215
219
|
|
216
|
-
jsi::Object UnistylesModel::parseEventNestedPayload(EventNestedValue payload) {
|
217
|
-
jsi::Object eventPayload = jsi::Object(
|
220
|
+
jsi::Object UnistylesModel::parseEventNestedPayload(jsi::Runtime& rt, const EventNestedValue& payload) {
|
221
|
+
jsi::Object eventPayload = jsi::Object(rt);
|
218
222
|
|
219
223
|
for (const auto& [key, value] : payload) {
|
220
224
|
if (std::holds_alternative<std::string>(value)) {
|
221
|
-
eventPayload.setProperty(
|
225
|
+
eventPayload.setProperty(rt, key.c_str(), jsi::String::createFromUtf8(rt, std::get<std::string>(value)));
|
222
226
|
}
|
223
227
|
|
224
228
|
if (std::holds_alternative<int>(value)) {
|
225
|
-
eventPayload.setProperty(
|
229
|
+
eventPayload.setProperty(rt, key.c_str(), std::get<int>(value));
|
226
230
|
}
|
227
231
|
}
|
228
232
|
|
package/cxx/UnistylesModel.h
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
#pragma once
|
2
2
|
|
3
3
|
#include <jsi/jsi.h>
|
4
|
-
#include <ReactCommon/CallInvoker.h>
|
5
4
|
#include <vector>
|
6
5
|
#include <map>
|
7
6
|
#include <optional>
|
@@ -51,8 +50,8 @@ struct UnistylesModel {
|
|
51
50
|
void onThemeChange(std::string themeName);
|
52
51
|
void onPluginChange();
|
53
52
|
void onLayoutChange();
|
54
|
-
jsi::Object parseEventPayload(EventPayload payload);
|
55
|
-
jsi::Object parseEventNestedPayload(EventNestedValue payload);
|
53
|
+
jsi::Object parseEventPayload(jsi::Runtime& rt, const EventPayload& payload);
|
54
|
+
jsi::Object parseEventNestedPayload(jsi::Runtime& rt, const EventNestedValue& payload);
|
56
55
|
|
57
56
|
std::function<Screen()> getScreenDimensions;
|
58
57
|
std::function<std::string()> getContentSizeCategory;
|
@@ -118,7 +117,7 @@ struct UnistylesModel {
|
|
118
117
|
std::string colorScheme = UnistylesUnspecifiedScheme;
|
119
118
|
std::string contentSizeCategory = UnistylesUnspecifiedScheme;
|
120
119
|
|
121
|
-
UnistylesModel(
|
120
|
+
UnistylesModel(std::function<void(std::function<void(jsi::Runtime&)>&&)> runOnJSThread): runOnJSThread(std::move(runOnJSThread)) {}
|
122
121
|
|
123
122
|
bool hasAdaptiveThemes;
|
124
123
|
bool supportsAutomaticColorScheme;
|
@@ -138,6 +137,5 @@ struct UnistylesModel {
|
|
138
137
|
std::vector<std::pair<std::string, double>> toSortedBreakpointPairs(jsi::Runtime&, jsi::Object&);
|
139
138
|
|
140
139
|
private:
|
141
|
-
jsi::Runtime&
|
142
|
-
std::shared_ptr<react::CallInvoker> callInvoker;
|
140
|
+
std::function<void(std::function<void(jsi::Runtime&)>&&)> runOnJSThread;
|
143
141
|
};
|
package/cxx/UnistylesRuntime.h
CHANGED
@@ -2,7 +2,6 @@
|
|
2
2
|
|
3
3
|
#include "UnistylesModel.h"
|
4
4
|
#include "Macros.h"
|
5
|
-
#include <ReactCommon/CallInvoker.h>
|
6
5
|
#include <jsi/jsi.h>
|
7
6
|
|
8
7
|
using namespace facebook;
|
@@ -11,7 +10,7 @@ using Getter = std::function<jsi::Value(jsi::Runtime& rt, std::string)>;
|
|
11
10
|
using Setter = std::function<std::optional<jsi::Value>(jsi::Runtime& rt, const jsi::Value&)>;
|
12
11
|
|
13
12
|
struct JSI_EXPORT UnistylesRuntime : public jsi::HostObject, UnistylesModel {
|
14
|
-
UnistylesRuntime(
|
13
|
+
UnistylesRuntime(std::function<void(std::function<void(jsi::Runtime&)>&&)> runOnJSThread) : UnistylesModel(runOnJSThread) {
|
15
14
|
this->getters = {
|
16
15
|
{"screenWidth", BIND_FN(getScreenWidth)},
|
17
16
|
{"screenHeight", BIND_FN(getScreenHeight)},
|
package/ios/UnistylesModule.h
CHANGED
package/ios/UnistylesModule.mm
CHANGED
@@ -1,6 +1,5 @@
|
|
1
1
|
#import "UnistylesModule.h"
|
2
2
|
#import "UnistylesRuntime.h"
|
3
|
-
|
4
3
|
#import <React/RCTBridge+Private.h>
|
5
4
|
#import <jsi/jsi.h>
|
6
5
|
|
@@ -16,10 +15,14 @@ RCT_EXPORT_MODULE(Unistyles)
|
|
16
15
|
if ((self = [super init])) {
|
17
16
|
self.platform = [[Platform alloc] init];
|
18
17
|
}
|
19
|
-
|
18
|
+
|
20
19
|
return self;
|
21
20
|
}
|
22
21
|
|
22
|
+
- (void)dealloc {
|
23
|
+
[self.platform clean];
|
24
|
+
}
|
25
|
+
|
23
26
|
+ (BOOL)requiresMainQueueSetup {
|
24
27
|
return YES;
|
25
28
|
}
|
@@ -62,8 +65,12 @@ RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(install) {
|
|
62
65
|
|
63
66
|
void registerUnistylesHostObject(RCTBridge* bridge, UnistylesModule* weakSelf) {
|
64
67
|
std::shared_ptr<react::CallInvoker> callInvoker = bridge.jsCallInvoker;
|
68
|
+
auto runOnJSThread = [callInvoker](std::function<void(jsi::Runtime& rt)> &&callback){
|
69
|
+
callInvoker->invokeAsync(std::move(callback));
|
70
|
+
};
|
71
|
+
|
65
72
|
jsi::Runtime* runtime = reinterpret_cast<jsi::Runtime*>(bridge.runtime);
|
66
|
-
auto unistylesRuntime = std::make_shared<UnistylesRuntime>(
|
73
|
+
auto unistylesRuntime = std::make_shared<UnistylesRuntime>(runOnJSThread);
|
67
74
|
|
68
75
|
[weakSelf.platform makeShared:unistylesRuntime.get()];
|
69
76
|
|
@@ -14,6 +14,10 @@
|
|
14
14
|
}
|
15
15
|
|
16
16
|
- (void)dealloc {
|
17
|
+
[self clean];
|
18
|
+
}
|
19
|
+
|
20
|
+
- (void)clean {
|
17
21
|
if (self.unistylesRuntime != nullptr) {
|
18
22
|
self.unistylesRuntime = nullptr;
|
19
23
|
}
|
@@ -116,7 +120,7 @@
|
|
116
120
|
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
|
117
121
|
UIApplicationState appState = [UIApplication sharedApplication].applicationState;
|
118
122
|
|
119
|
-
if (appState
|
123
|
+
if (appState != UIApplicationStateActive) {
|
120
124
|
return;
|
121
125
|
}
|
122
126
|
|