@depup/react-native-worklets 0.11.3-depup.0 → 0.12.1-depup.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.
Files changed (77) hide show
  1. package/Common/cpp/worklets/AnimationFrameQueue/AnimationFrameBatchinator.cpp +6 -3
  2. package/Common/cpp/worklets/Compat/StableApi.cpp +4 -0
  3. package/Common/cpp/worklets/Compat/StableApi.h +3 -1
  4. package/Common/cpp/worklets/NativeModules/JSIWorkletsModuleProxy.cpp +26 -43
  5. package/Common/cpp/worklets/RunLoop/AsyncQueueImpl.cpp +32 -0
  6. package/Common/cpp/worklets/RunLoop/EventLoop.cpp +16 -7
  7. package/Common/cpp/worklets/RunLoop/EventLoop.h +4 -2
  8. package/Common/cpp/worklets/SharedItems/MemoryManager.cpp +2 -2
  9. package/Common/cpp/worklets/SharedItems/Serializable.h +14 -16
  10. package/Common/cpp/worklets/Tools/UIScheduler.cpp +10 -0
  11. package/Common/cpp/worklets/Tools/UIScheduler.h +3 -0
  12. package/Common/cpp/worklets/Tools/WorkletsJSIUtils.h +23 -0
  13. package/Common/cpp/worklets/WorkletRuntime/RuntimeManager.cpp +7 -4
  14. package/Common/cpp/worklets/WorkletRuntime/RuntimeManager.h +2 -1
  15. package/Common/cpp/worklets/WorkletRuntime/WorkletRuntime.cpp +60 -88
  16. package/Common/cpp/worklets/WorkletRuntime/WorkletRuntime.h +335 -122
  17. package/README.md +2 -2
  18. package/android/build.gradle.kts +20 -6
  19. package/android/src/main/cpp/worklets/android/AndroidUIScheduler.cpp +10 -8
  20. package/android/src/main/cpp/worklets/android/JScriptBufferWrapper.cpp +2 -11
  21. package/android/src/main/cpp/worklets/android/JScriptBufferWrapper.h +1 -4
  22. package/android/src/main/java/com/swmansion/worklets/AndroidUIScheduler.kt +3 -0
  23. package/android/src/main/java/com/swmansion/worklets/ScriptBufferWrapper.kt +87 -43
  24. package/android/src/networking/com/swmansion/worklets/WorkletsModule.kt +25 -9
  25. package/android/src/no-networking/com/swmansion/worklets/WorkletsModule.kt +25 -9
  26. package/apple/worklets/apple/IOSUIScheduler.h +3 -0
  27. package/apple/worklets/apple/IOSUIScheduler.mm +6 -1
  28. package/apple/worklets/apple/ScriptLoader.h +11 -0
  29. package/apple/worklets/apple/ScriptLoader.mm +124 -0
  30. package/apple/worklets/apple/WorkletsModule.h +1 -16
  31. package/apple/worklets/apple/WorkletsModule.mm +4 -16
  32. package/changes.json +1 -1
  33. package/compatibility.json +4 -1
  34. package/lib/module/WorkletsModule/NativeWorklets.native.js +2 -2
  35. package/lib/module/WorkletsModule/NativeWorklets.native.js.map +1 -1
  36. package/lib/module/debug/jsVersion.js +1 -1
  37. package/lib/module/memory/serializable.native.js +59 -21
  38. package/lib/module/memory/serializable.native.js.map +1 -1
  39. package/lib/module/mock.js +58 -17
  40. package/lib/module/mock.js.map +1 -1
  41. package/lib/module/runLoop/uiRuntime/requestAnimationFrame.js +1 -2
  42. package/lib/module/runLoop/uiRuntime/requestAnimationFrame.js.map +1 -1
  43. package/lib/module/runLoop/workletRuntime/taskQueue.js +0 -1
  44. package/lib/module/runLoop/workletRuntime/taskQueue.js.map +1 -1
  45. package/lib/module/runtimes.native.js +7 -8
  46. package/lib/module/runtimes.native.js.map +1 -1
  47. package/lib/module/threads.js +35 -24
  48. package/lib/module/threads.js.map +1 -1
  49. package/lib/typescript/WorkletsModule/NativeWorklets.native.d.ts.map +1 -1
  50. package/lib/typescript/WorkletsModule/workletsModuleProxy.d.ts +1 -1
  51. package/lib/typescript/WorkletsModule/workletsModuleProxy.d.ts.map +1 -1
  52. package/lib/typescript/debug/jsVersion.d.ts +1 -1
  53. package/lib/typescript/memory/serializable.native.d.ts.map +1 -1
  54. package/lib/typescript/runLoop/uiRuntime/requestAnimationFrame.d.ts.map +1 -1
  55. package/lib/typescript/runLoop/workletRuntime/taskQueue.d.ts.map +1 -1
  56. package/lib/typescript/runtimes.native.d.ts.map +1 -1
  57. package/lib/typescript/specs/NativeWorkletsModule.d.ts +1 -1
  58. package/lib/typescript/threads.d.ts.map +1 -1
  59. package/lib/typescript/types.d.ts +38 -8
  60. package/lib/typescript/types.d.ts.map +1 -1
  61. package/package.json +14 -15
  62. package/plugin/index.js +1625 -1572
  63. package/src/WorkletsModule/NativeWorklets.native.ts +4 -2
  64. package/src/WorkletsModule/workletsModuleProxy.ts +2 -1
  65. package/src/debug/jsVersion.ts +1 -1
  66. package/src/memory/serializable.native.ts +103 -36
  67. package/src/mock.ts +85 -23
  68. package/src/runLoop/uiRuntime/requestAnimationFrame.ts +1 -2
  69. package/src/runLoop/workletRuntime/taskQueue.ts +0 -1
  70. package/src/runtimes.native.ts +12 -8
  71. package/src/threads.ts +38 -26
  72. package/src/types.ts +42 -8
  73. package/Common/cpp/worklets/Registries/EventHandlerRegistry.cpp +0 -95
  74. package/Common/cpp/worklets/Registries/EventHandlerRegistry.h +0 -44
  75. package/Common/cpp/worklets/Tools/WorkletEventHandler.cpp +0 -34
  76. package/Common/cpp/worklets/Tools/WorkletEventHandler.h +0 -44
  77. package/jest/pluginTestUtils.ts +0 -16
@@ -2,7 +2,6 @@
2
2
  #include <jsi/jsi.h>
3
3
  #include <worklets/NativeModules/JSIWorkletsModuleProxy.h>
4
4
  #include <worklets/Tools/JSLogger.h>
5
- #include <worklets/Tools/WorkletsJSIUtils.h>
6
5
  #include <worklets/WorkletRuntime/RuntimeHolder.h>
7
6
  #include <worklets/WorkletRuntime/ScriptLoader.h>
8
7
  #include <worklets/WorkletRuntime/WorkletHermesRuntime.h>
@@ -46,9 +45,14 @@ class LockableRuntime : public jsi::WithRuntimeDecorator<AroundLock> {
46
45
  runtime_(std::move(runtime)) {}
47
46
  };
48
47
 
49
- static std::shared_ptr<jsi::Runtime> makeRuntime(const std::shared_ptr<std::recursive_mutex> &runtimeMutex) {
50
- auto hermesRuntime = facebook::hermes::makeHermesRuntime();
48
+ static std::shared_ptr<jsi::Runtime>
49
+ makeRuntime(const std::shared_ptr<std::recursive_mutex> &runtimeMutex, bool enableLocking, bool enableMicrotaskQueue) {
50
+ auto config = ::hermes::vm::RuntimeConfig::Builder().withMicrotaskQueue(enableMicrotaskQueue).build();
51
+ auto hermesRuntime = facebook::hermes::makeHermesRuntime(config);
51
52
  std::shared_ptr<jsi::Runtime> jsiRuntime = std::make_shared<WorkletHermesRuntime>(std::move(hermesRuntime));
53
+ if (!enableLocking) {
54
+ return jsiRuntime;
55
+ }
52
56
  return std::make_shared<LockableRuntime>(jsiRuntime, runtimeMutex);
53
57
  }
54
58
 
@@ -57,17 +61,24 @@ WorkletRuntime::WorkletRuntime(
57
61
  const RuntimeData::RuntimeKind runtimeKind,
58
62
  const std::string &name,
59
63
  const std::shared_ptr<AsyncQueue> &queue,
60
- bool enableEventLoop)
64
+ bool enableEventLoop,
65
+ bool enableLocking)
61
66
  : runtimeId_(runtimeId),
67
+ enableLocking_(enableLocking),
62
68
  runtimeMutex_(std::make_shared<std::recursive_mutex>()),
63
- runtime_(makeRuntime(runtimeMutex_)),
69
+ microtaskQueueEnabled_(enableEventLoop || runtimeKind == RuntimeData::RuntimeKind::UI),
70
+ runtime_(makeRuntime(runtimeMutex_, enableLocking_, microtaskQueueEnabled_)),
64
71
  runtimeKind_(runtimeKind),
65
72
  name_(name),
66
73
  queue_(queue) {
74
+ react_native_assert(
75
+ (enableLocking || !enableEventLoop) &&
76
+ "[Worklets] The Event Loop cannot be enabled on a Worklet Runtime with "
77
+ "locking disabled.");
67
78
  jsi::Runtime &rt = *runtime_;
68
79
  WorkletRuntimeCollector::install(rt);
69
80
  if (enableEventLoop) {
70
- eventLoop_ = std::make_shared<EventLoop>(name_, runtime_, queue_);
81
+ eventLoop_ = std::make_shared<EventLoop>(name_, runtime_, queue_, runtimeMutex_);
71
82
  eventLoop_->run();
72
83
  }
73
84
  }
@@ -136,91 +147,75 @@ void WorkletRuntime::legacyModeInit(const std::shared_ptr<UnpackerLoader> &unpac
136
147
  /* #region schedule */
137
148
 
138
149
  void WorkletRuntime::schedule(jsi::Function &&function) const {
139
- react_native_assert(
140
- queue_ &&
141
- "[Worklets] Tried to invoke `schedule` on a Worklet Runtime but the "
142
- "async queue is not set. Recreate the runtime with a valid async queue.");
143
- queue_->push([function = std::make_shared<jsi::Function>(std::move(function)), weakThis = weak_from_this()]() {
144
- auto strongThis = weakThis.lock();
145
- if (!strongThis) {
146
- return;
147
- }
148
-
149
- strongThis->runSync(*function);
150
+ scheduleImpl([function = std::make_shared<jsi::Function>(std::move(function))](const WorkletRuntime &workletRuntime) {
151
+ workletRuntime.runSyncImpl<MicrotaskCheckpoint::Run>(*function);
150
152
  });
151
153
  }
152
154
 
153
155
  void WorkletRuntime::schedule(std::shared_ptr<SerializableWorklet> worklet) const {
154
- react_native_assert(
155
- queue_ &&
156
- "[Worklets] Tried to invoke `schedule` on a Worklet Runtime but the "
157
- "async queue is not set. Recreate the runtime with a valid async queue.");
158
-
159
- queue_->push([worklet = std::move(worklet), weakThis = weak_from_this()] {
160
- auto strongThis = weakThis.lock();
161
- if (!strongThis) {
162
- return;
163
- }
156
+ scheduleImpl([worklet = std::move(worklet)](const WorkletRuntime &workletRuntime) {
157
+ workletRuntime.runSyncImpl<MicrotaskCheckpoint::Run>(worklet);
158
+ });
159
+ }
164
160
 
165
- strongThis->runSync(worklet);
161
+ void WorkletRuntime::schedule(std::vector<std::shared_ptr<SerializableWorklet>> worklets) const {
162
+ scheduleImpl([worklets = std::move(worklets)](const WorkletRuntime &workletRuntime) {
163
+ workletRuntime.runSyncImpl<MicrotaskCheckpoint::Run>([&](jsi::Runtime &rt) {
164
+ const auto scope = jsi::Scope(rt);
165
+ for (const auto &worklet : worklets) {
166
+ workletRuntime.runSyncImpl(worklet);
167
+ }
168
+ });
166
169
  });
167
170
  }
168
171
 
169
172
  #ifndef NDEBUG
170
- void WorkletRuntime::schedule(std::shared_ptr<SerializableWorklet> worklet, std::optional<std::string> scheduleStack)
171
- const {
172
- react_native_assert(
173
- queue_ &&
174
- "[Worklets] Tried to invoke `schedule` on a Worklet Runtime but the "
175
- "async queue is not set. Recreate the runtime with a valid async queue.");
176
-
177
- queue_->push([worklet = std::move(worklet), scheduleStack = std::move(scheduleStack), weakThis = weak_from_this()] {
178
- auto strongThis = weakThis.lock();
179
- if (!strongThis) {
180
- return;
181
- }
182
-
183
- strongThis->runSyncWithStack(worklet, scheduleStack);
173
+ void WorkletRuntime::scheduleWithStack(
174
+ std::shared_ptr<SerializableWorklet> worklet,
175
+ std::optional<std::string> scheduleStack) const {
176
+ scheduleImpl([worklet = std::move(worklet),
177
+ scheduleStack = std::move(scheduleStack)](const WorkletRuntime &workletRuntime) {
178
+ workletRuntime.runSyncImpl<MicrotaskCheckpoint::Run, jsi::Value, ScheduleStack::Requested>(worklet, scheduleStack);
184
179
  });
185
180
  }
186
- #endif // NDEBUG
187
181
 
188
- void WorkletRuntime::callMicrotasks() const {
189
- runSync([](jsi::Runtime &rt) {
190
- auto callMicrotasks = rt.global().getProperty(rt, "__callMicrotasks");
191
- if (callMicrotasks.isObject()) {
192
- auto callMicrotasksObject = callMicrotasks.asObject(rt);
193
- if (callMicrotasksObject.isFunction(rt)) {
194
- callMicrotasksObject.asFunction(rt).call(rt);
182
+ void WorkletRuntime::scheduleWithStack(
183
+ std::vector<std::shared_ptr<SerializableWorklet>> worklets,
184
+ std::vector<std::optional<std::string>> scheduleStacks) const {
185
+ react_native_assert(worklets.size() == scheduleStacks.size());
186
+ scheduleImpl([worklets = std::move(worklets),
187
+ scheduleStacks = std::move(scheduleStacks)](const WorkletRuntime &workletRuntime) {
188
+ workletRuntime.runSyncImpl<MicrotaskCheckpoint::Run>([&](jsi::Runtime &rt) {
189
+ const auto scope = jsi::Scope(rt);
190
+ for (size_t i = 0; i < worklets.size(); i++) {
191
+ workletRuntime.runSyncImpl<MicrotaskCheckpoint::Skip, jsi::Value, ScheduleStack::Requested>(
192
+ worklets[i], scheduleStacks[i]);
195
193
  }
196
- }
194
+ });
197
195
  });
198
196
  }
197
+ #endif // NDEBUG
199
198
 
200
- void WorkletRuntime::schedule(std::function<void()> job) const {
201
- react_native_assert(
202
- queue_ &&
203
- "[Worklets] Tried to invoke `schedule` on a Worklet Runtime but the "
204
- "async queue is not set. Recreate the runtime with a valid async queue.");
205
-
206
- queue_->push(std::move(job));
199
+ void WorkletRuntime::schedule(std::function<void(jsi::Runtime &)> job) const {
200
+ scheduleImpl([job = std::move(job)](const WorkletRuntime &workletRuntime) {
201
+ workletRuntime.runSyncImpl<MicrotaskCheckpoint::Run>(job);
202
+ });
207
203
  }
208
204
 
209
- void WorkletRuntime::schedule(std::function<void(jsi::Runtime &)> job) const {
205
+ void WorkletRuntime::scheduleImpl(ScheduledJob job) const {
210
206
  react_native_assert(
211
207
  queue_ &&
212
208
  "[Worklets] Tried to invoke `schedule` on a Worklet Runtime but the "
213
209
  "async queue is not set. Recreate the runtime with a valid async queue.");
214
210
 
215
- queue_->push([job = std::move(job), weakThis = weak_from_this()]() {
216
- auto strongThis = weakThis.lock();
211
+ queue_->push([job = std::move(job), weakThis = weak_from_this()] {
212
+ const auto strongThis = weakThis.lock();
217
213
  if (!strongThis) {
218
214
  return;
219
215
  }
220
216
 
221
- auto lock = std::unique_lock<std::recursive_mutex>(*strongThis->runtimeMutex_);
222
- jsi::Runtime &runtime = strongThis->getJSIRuntime();
223
- job(runtime);
217
+ auto lock = strongThis->acquireRuntimeLock();
218
+ job(*strongThis);
224
219
  });
225
220
  }
226
221
 
@@ -286,7 +281,7 @@ void scheduleOnRuntime(
286
281
  rt,
287
282
  serializableWorkletValue,
288
283
  "[Worklets] Function passed to `_scheduleOnRuntime` is not a serializable worklet.");
289
- workletRuntime->schedule(serializableWorklet, scheduleStack);
284
+ workletRuntime->scheduleWithStack(serializableWorklet, scheduleStack);
290
285
  }
291
286
  #endif // NDEBUG
292
287
 
@@ -301,27 +296,4 @@ std::weak_ptr<WorkletRuntime> WorkletRuntime::getWeakRuntimeFromJSIRuntime(jsi::
301
296
  return weakHolder->weakRuntime;
302
297
  }
303
298
 
304
- /* #region deprecated */
305
-
306
- void WorkletRuntime::runAsyncGuarded(const std::shared_ptr<SerializableWorklet> &worklet) {
307
- schedule(worklet);
308
- }
309
-
310
- jsi::Value WorkletRuntime::executeSync(jsi::Runtime &caller, const jsi::Value &worklet) const {
311
- auto serializableWorklet = extractSerializableOrThrow<SerializableWorklet>(
312
- caller, worklet, "[Worklets] Only worklets can be executed synchronously on UI runtime.");
313
- auto result = runSyncSerialized(serializableWorklet);
314
- return result->toJSValue(caller);
315
- }
316
-
317
- jsi::Value WorkletRuntime::executeSync(std::function<jsi::Value(jsi::Runtime &)> &&job) const {
318
- return runSync(job);
319
- }
320
-
321
- jsi::Value WorkletRuntime::executeSync(const std::function<jsi::Value(jsi::Runtime &)> &job) const {
322
- return runSync(job);
323
- }
324
-
325
- /* #endregion */
326
-
327
299
  } // namespace worklets