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.
@@ -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
- unistylesRuntime = std::make_shared<UnistylesRuntime>(*runtime, callInvoker);
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);
@@ -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->callInvoker->invokeAsync([eventType, payload, this](){
114
- jsi::Value emitter = this->runtime.global().getProperty(this->runtime, "__rctDeviceEventEmitter");
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(runtime);
121
- jsi::Function emitFunction = emitterObject.getPropertyAsFunction(runtime, "emit");
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(this->runtime);
124
+ jsi::Object event = jsi::Object(rt);
125
+ jsi::Object eventPayload = this->parseEventPayload(rt, payload);
125
126
 
126
- event.setProperty(this->runtime, "type", jsi::String::createFromUtf8(this->runtime, eventType));
127
+ event.setProperty(rt, "type", jsi::String::createFromUtf8(rt, std::move(eventType)));
128
+ event.setProperty(rt, "payload", std::move(eventPayload));
127
129
 
128
- jsi::Object eventPayload = this->parseEventPayload(payload);
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(runtime, emitterObject, (const jsi::Value*)arguments.data(), arguments.size());
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(this->runtime);
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(this->runtime, key.c_str(), jsi::String::createFromUtf8(this->runtime, std::get<std::string>(value)));
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(this->runtime, key.c_str(), std::get<int>(value));
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(this->runtime, key.c_str(), this->parseEventNestedPayload(std::get<EventNestedValue>(value)));
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(this->runtime);
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(this->runtime, key.c_str(), jsi::String::createFromUtf8(this->runtime, std::get<std::string>(value)));
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(this->runtime, key.c_str(), std::get<int>(value));
229
+ eventPayload.setProperty(rt, key.c_str(), std::get<int>(value));
226
230
  }
227
231
  }
228
232
 
@@ -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(jsi::Runtime& rt, std::shared_ptr<react::CallInvoker> callInvoker): runtime(rt), callInvoker(callInvoker) {}
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& runtime;
142
- std::shared_ptr<react::CallInvoker> callInvoker;
140
+ std::function<void(std::function<void(jsi::Runtime&)>&&)> runOnJSThread;
143
141
  };
@@ -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(jsi::Runtime& rt, std::shared_ptr<react::CallInvoker> callInvoker) : UnistylesModel(rt, callInvoker) {
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)},
@@ -1,5 +1,6 @@
1
1
  #import <React/RCTBridgeModule.h>
2
2
  #import <React/RCTEventEmitter.h>
3
+ #include <ReactCommon/CallInvoker.h>
3
4
 
4
5
  #import <string>
5
6
 
@@ -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>(*runtime, callInvoker);
73
+ auto unistylesRuntime = std::make_shared<UnistylesRuntime>(runOnJSThread);
67
74
 
68
75
  [weakSelf.platform makeShared:unistylesRuntime.get()];
69
76
 
@@ -9,6 +9,7 @@
9
9
 
10
10
  - (instancetype)init;
11
11
 
12
+ - (void)clean;
12
13
  - (void)setupListeners;
13
14
  - (void)makeShared:(void*)runtime;
14
15
  - (void)onWindowChange:(NSNotification *)notification;
@@ -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 == UIApplicationStateBackground) {
123
+ if (appState != UIApplicationStateActive) {
120
124
  return;
121
125
  }
122
126
 
@@ -8,6 +8,7 @@
8
8
 
9
9
  - (instancetype)init;
10
10
 
11
+ - (void)clean;
11
12
  - (void)setupListeners;
12
13
  - (void)makeShared:(void*)runtime;
13
14
  - (void)onWindowChange;
@@ -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
  }
@@ -8,6 +8,7 @@
8
8
 
9
9
  - (instancetype)init;
10
10
 
11
+ - (void)clean;
11
12
  - (void)makeShared:(void*)runtime;
12
13
  - (void)onAppearanceChange:(NSNotification *)notification;
13
14
  - (void)onContentSizeCategoryChange:(NSNotification *)notification;
@@ -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
  }
@@ -9,6 +9,7 @@
9
9
 
10
10
  - (instancetype)init;
11
11
 
12
+ - (void)clean;
12
13
  - (void)setupListeners;
13
14
  - (void)makeShared:(void*)runtime;
14
15
  - (UIWindow *)getMainWindow;
@@ -36,6 +36,10 @@
36
36
  }
37
37
 
38
38
  - (void)dealloc {
39
+ [self clean];
40
+ }
41
+
42
+ - (void)clean {
39
43
  if (self.unistylesRuntime != nullptr) {
40
44
  self.unistylesRuntime = nullptr;
41
45
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-unistyles",
3
- "version": "2.32.0",
3
+ "version": "2.41.0",
4
4
  "description": "Level up your React Native StyleSheet",
5
5
  "scripts": {
6
6
  "test": "jest",