react-native-worklets 0.12.0 → 0.12.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.
@@ -22,6 +22,10 @@ void scheduleOnUI(const std::shared_ptr<UIScheduler> &uiScheduler, const std::fu
22
22
  uiScheduler->scheduleOnUI(job);
23
23
  }
24
24
 
25
+ bool isOnUIThread(const std::shared_ptr<UIScheduler> &uiScheduler) {
26
+ return uiScheduler->isOnUIThread();
27
+ }
28
+
25
29
  facebook::jsi::Runtime &getJSIRuntimeFromWorkletRuntime(const std::shared_ptr<WorkletRuntime> &workletRuntime) {
26
30
  return workletRuntime->getJSIRuntime();
27
31
  }
@@ -5,7 +5,7 @@
5
5
  * It can be used by libraries to verify they use a compatible version
6
6
  * of `react-native-worklets` installed when integrating in C++.
7
7
  */
8
- #define WORKLETS_STABLE_API_VERSION "0.9.0"
8
+ #define WORKLETS_STABLE_API_VERSION "0.12.1"
9
9
 
10
10
  #include <cstdint>
11
11
  #include <functional>
@@ -83,6 +83,8 @@ extern std::weak_ptr<WorkletRuntime> getWeakRuntimeFromJSIRuntime(facebook::jsi:
83
83
 
84
84
  extern void scheduleOnUI(const std::shared_ptr<UIScheduler> &uiScheduler, const std::function<void()> &job);
85
85
 
86
+ extern bool isOnUIThread(const std::shared_ptr<UIScheduler> &uiScheduler);
87
+
86
88
  extern std::string JSIValueToStdString(facebook::jsi::Runtime &rt, const facebook::jsi::Value &value);
87
89
 
88
90
  extern std::shared_ptr<Serializable>
@@ -1,9 +1,12 @@
1
1
  #include <worklets/Tools/UIScheduler.h>
2
2
 
3
+ #include <optional>
3
4
  #include <utility>
4
5
 
5
6
  namespace worklets {
6
7
 
8
+ static thread_local std::optional<bool> tls_isOnUIThread;
9
+
7
10
  void UIScheduler::scheduleOnUI(std::function<void()> job) {
8
11
  uiJobs_.push(std::move(job));
9
12
  }
@@ -16,4 +19,11 @@ void UIScheduler::triggerUI() {
16
19
  }
17
20
  }
18
21
 
22
+ bool UIScheduler::isOnUIThread() const {
23
+ if (!tls_isOnUIThread.has_value()) {
24
+ tls_isOnUIThread = queryIsOnUIThread();
25
+ }
26
+ return *tls_isOnUIThread;
27
+ }
28
+
19
29
  } // namespace worklets
@@ -12,9 +12,12 @@ class UIScheduler {
12
12
  public:
13
13
  virtual void scheduleOnUI(std::function<void()> job);
14
14
  virtual void triggerUI();
15
+ bool isOnUIThread() const;
15
16
  virtual ~UIScheduler() = default;
16
17
 
17
18
  protected:
19
+ virtual bool queryIsOnUIThread() const = 0;
20
+
18
21
  std::atomic<bool> scheduledOnUI_{false};
19
22
  ThreadSafeQueue<std::function<void()>> uiJobs_;
20
23
  };
@@ -7,18 +7,25 @@ namespace worklets {
7
7
  using namespace facebook;
8
8
  using namespace react;
9
9
 
10
- static thread_local bool tls_isOnUIThread = false;
11
-
12
10
  class UISchedulerWrapper : public UIScheduler {
13
11
  private:
14
12
  jni::global_ref<AndroidUIScheduler::javaobject> androidUiScheduler_;
15
13
 
14
+ bool queryIsOnUIThread() const override {
15
+ static const auto method = androidUiScheduler_->getClass()->getMethod<jboolean()>("isOnUIThread");
16
+ return method(androidUiScheduler_);
17
+ }
18
+
16
19
  public:
17
20
  explicit UISchedulerWrapper(jni::global_ref<AndroidUIScheduler::javaobject> androidUiScheduler)
18
21
  : androidUiScheduler_(std::move(androidUiScheduler)) {}
19
22
 
23
+ void triggerUI() override {
24
+ UIScheduler::triggerUI();
25
+ }
26
+
20
27
  void scheduleOnUI(std::function<void()> job) override {
21
- if (tls_isOnUIThread) {
28
+ if (isOnUIThread()) {
22
29
  job();
23
30
  return;
24
31
  }
@@ -29,11 +36,6 @@ class UISchedulerWrapper : public UIScheduler {
29
36
  method(androidUiScheduler_);
30
37
  }
31
38
  }
32
-
33
- void triggerUI() override {
34
- tls_isOnUIThread = true;
35
- UIScheduler::triggerUI();
36
- }
37
39
  };
38
40
 
39
41
  AndroidUIScheduler::AndroidUIScheduler(const jni::alias_ref<AndroidUIScheduler::jhybridobject> &jThis)
@@ -31,6 +31,9 @@ class AndroidUIScheduler(
31
31
 
32
32
  private external fun initHybrid(): HybridData
33
33
 
34
+ @DoNotStrip
35
+ private fun isOnUIThread(): Boolean = UiThreadUtil.isOnUiThread()
36
+
34
37
  external fun triggerUI()
35
38
 
36
39
  external fun invalidate()
@@ -9,6 +9,9 @@ using namespace worklets;
9
9
  class IOSUIScheduler : public UIScheduler, public std::enable_shared_from_this<IOSUIScheduler> {
10
10
  public:
11
11
  void scheduleOnUI(std::function<void()> job) override;
12
+
13
+ protected:
14
+ bool queryIsOnUIThread() const override;
12
15
  };
13
16
 
14
17
  } // namespace worklets
@@ -5,9 +5,14 @@ namespace worklets {
5
5
  using namespace facebook;
6
6
  using namespace react;
7
7
 
8
+ bool IOSUIScheduler::queryIsOnUIThread() const
9
+ {
10
+ return [NSThread isMainThread];
11
+ }
12
+
8
13
  void IOSUIScheduler::scheduleOnUI(std::function<void()> job)
9
14
  {
10
- if ([NSThread isMainThread]) {
15
+ if (isOnUIThread()) {
11
16
  job();
12
17
  return;
13
18
  }
@@ -5,5 +5,5 @@
5
5
  * version used to build the native part of the library in runtime. Remember to
6
6
  * keep this in sync with the version declared in `package.json`
7
7
  */
8
- export const jsVersion = '0.12.0';
8
+ export const jsVersion = '0.12.1';
9
9
  //# sourceMappingURL=jsVersion.js.map
@@ -3,5 +3,5 @@
3
3
  * version used to build the native part of the library in runtime. Remember to
4
4
  * keep this in sync with the version declared in `package.json`
5
5
  */
6
- export declare const jsVersion = "0.12.0";
6
+ export declare const jsVersion = "0.12.1";
7
7
  //# sourceMappingURL=jsVersion.d.ts.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-worklets",
3
- "version": "0.12.0",
3
+ "version": "0.12.1",
4
4
  "description": "The React Native multithreading library",
5
5
  "keywords": [
6
6
  "react-native",
@@ -5,4 +5,4 @@
5
5
  * version used to build the native part of the library in runtime. Remember to
6
6
  * keep this in sync with the version declared in `package.json`
7
7
  */
8
- export const jsVersion = '0.12.0';
8
+ export const jsVersion = '0.12.1';