@depup/react-native-worklets 0.11.4-depup.0 → 0.12.2-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 (82) 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/EventLoop.cpp +16 -7
  6. package/Common/cpp/worklets/RunLoop/EventLoop.h +4 -2
  7. package/Common/cpp/worklets/SharedItems/MemoryManager.cpp +2 -2
  8. package/Common/cpp/worklets/SharedItems/Serializable.cpp +17 -1
  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 +7 -7
  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/main/java/com/swmansion/worklets/runloop/AnimationFrameQueue.kt +37 -5
  25. package/android/src/networking/com/swmansion/worklets/WorkletsModule.kt +2 -3
  26. package/android/src/no-networking/com/swmansion/worklets/WorkletsModule.kt +2 -3
  27. package/apple/worklets/apple/IOSUIScheduler.h +3 -0
  28. package/apple/worklets/apple/IOSUIScheduler.mm +6 -1
  29. package/apple/worklets/apple/ScriptLoader.h +11 -0
  30. package/apple/worklets/apple/ScriptLoader.mm +124 -0
  31. package/apple/worklets/apple/WorkletsModule.h +1 -16
  32. package/apple/worklets/apple/WorkletsModule.mm +4 -16
  33. package/changes.json +6 -6
  34. package/compatibility.json +4 -1
  35. package/lib/module/WorkletsModule/NativeWorklets.native.js +2 -2
  36. package/lib/module/WorkletsModule/NativeWorklets.native.js.map +1 -1
  37. package/lib/module/debug/jsVersion.js +1 -1
  38. package/lib/module/memory/serializable.native.js +61 -23
  39. package/lib/module/memory/serializable.native.js.map +1 -1
  40. package/lib/module/memory/synchronizableUnpacker.native.js +16 -8
  41. package/lib/module/memory/synchronizableUnpacker.native.js.map +1 -1
  42. package/lib/module/mock.js +58 -17
  43. package/lib/module/mock.js.map +1 -1
  44. package/lib/module/runLoop/uiRuntime/requestAnimationFrame.js +1 -2
  45. package/lib/module/runLoop/uiRuntime/requestAnimationFrame.js.map +1 -1
  46. package/lib/module/runLoop/workletRuntime/taskQueue.js +0 -1
  47. package/lib/module/runLoop/workletRuntime/taskQueue.js.map +1 -1
  48. package/lib/module/runtimes.native.js +7 -8
  49. package/lib/module/runtimes.native.js.map +1 -1
  50. package/lib/module/threads.js +35 -24
  51. package/lib/module/threads.js.map +1 -1
  52. package/lib/typescript/WorkletsModule/NativeWorklets.native.d.ts.map +1 -1
  53. package/lib/typescript/WorkletsModule/workletsModuleProxy.d.ts +1 -1
  54. package/lib/typescript/WorkletsModule/workletsModuleProxy.d.ts.map +1 -1
  55. package/lib/typescript/debug/jsVersion.d.ts +1 -1
  56. package/lib/typescript/memory/serializable.native.d.ts.map +1 -1
  57. package/lib/typescript/memory/synchronizableUnpacker.native.d.ts.map +1 -1
  58. package/lib/typescript/runLoop/uiRuntime/requestAnimationFrame.d.ts.map +1 -1
  59. package/lib/typescript/runLoop/workletRuntime/taskQueue.d.ts.map +1 -1
  60. package/lib/typescript/runtimes.native.d.ts.map +1 -1
  61. package/lib/typescript/specs/NativeWorkletsModule.d.ts +1 -1
  62. package/lib/typescript/threads.d.ts.map +1 -1
  63. package/lib/typescript/types.d.ts +38 -8
  64. package/lib/typescript/types.d.ts.map +1 -1
  65. package/package.json +24 -25
  66. package/plugin/index.js +1625 -1572
  67. package/src/WorkletsModule/NativeWorklets.native.ts +4 -2
  68. package/src/WorkletsModule/workletsModuleProxy.ts +2 -1
  69. package/src/debug/jsVersion.ts +1 -1
  70. package/src/memory/serializable.native.ts +108 -38
  71. package/src/memory/synchronizableUnpacker.native.ts +16 -10
  72. package/src/mock.ts +85 -23
  73. package/src/runLoop/uiRuntime/requestAnimationFrame.ts +1 -2
  74. package/src/runLoop/workletRuntime/taskQueue.ts +0 -1
  75. package/src/runtimes.native.ts +12 -8
  76. package/src/threads.ts +38 -26
  77. package/src/types.ts +42 -8
  78. package/Common/cpp/worklets/Registries/EventHandlerRegistry.cpp +0 -95
  79. package/Common/cpp/worklets/Registries/EventHandlerRegistry.h +0 -44
  80. package/Common/cpp/worklets/Tools/WorkletEventHandler.cpp +0 -34
  81. package/Common/cpp/worklets/Tools/WorkletEventHandler.h +0 -44
  82. package/jest/pluginTestUtils.ts +0 -16
package/src/types.ts CHANGED
@@ -121,18 +121,52 @@ type WorkletRuntimeConfigBase = {
121
121
  * by requestAnimationFrame. If not specified, it defaults to 16 ms.
122
122
  */
123
123
  animationQueuePollingRate?: number;
124
- /**
125
- * Determines whether to enable the default Event Loop or not. The Event Loop
126
- * provides implementations for `setTimeout`, `setImmediate`, `setInterval`,
127
- * `requestAnimationFrame`, `queueMicrotask`, `clearTimeout`, `clearInterval`,
128
- * `clearImmediate`, and `cancelAnimationFrame` methods. If not specified, it
129
- * defaults to `true`.
130
- */
131
- enableEventLoop?: boolean;
132
124
  };
133
125
 
134
126
  /** Configuration object for creating a worklet runtime. */
135
127
  export type WorkletRuntimeConfig = WorkletRuntimeConfigBase &
128
+ (
129
+ | {
130
+ /**
131
+ * Determines whether to enable the default Event Loop, which provides
132
+ * `setTimeout`, `setInterval`, `requestAnimationFrame` and other
133
+ * scheduling APIs. If not specified, it defaults to `true`. Cannot be
134
+ * enabled when {@link enableLocking} is set to `false`.
135
+ */
136
+ enableEventLoop?: boolean;
137
+ /**
138
+ * Determines whether access to the underlying JS runtime is
139
+ * synchronized with a mutex around every JSI operation. If not
140
+ * specified, it defaults to `true`. Can be disabled only together
141
+ * with the Event Loop.
142
+ *
143
+ * Runtime without locking doesn't receive HMR updates
144
+ * and new Custom Serializable registrations.
145
+ *
146
+ * Disable only when you can guarantee thread safety of all access to the runtime.
147
+ */
148
+ enableLocking?: true;
149
+ }
150
+ | {
151
+ /**
152
+ * The Event Loop cannot be enabled on a runtime with
153
+ * {@link enableLocking} set to `false`.
154
+ */
155
+ enableEventLoop?: false;
156
+ /**
157
+ * Determines whether access to the underlying JS runtime is
158
+ * synchronized with a mutex around every JSI operation. If not
159
+ * specified, it defaults to `true`. Can be disabled only together
160
+ * with the Event Loop.
161
+ *
162
+ * Runtime without locking doesn't receive HMR updates
163
+ * and new Custom Serializable registrations.
164
+ *
165
+ * Disable only when you can guarantee thread safety of all access to the runtime.
166
+ */
167
+ enableLocking: false;
168
+ }
169
+ ) &
136
170
  (
137
171
  | {
138
172
  /**
@@ -1,95 +0,0 @@
1
- // EventHandlerRegistry was fully moved to Reanimated.
2
- // This file is kept only for backwards compatibility with older versions of Reanimated.
3
-
4
- #include <worklets/Registries/EventHandlerRegistry.h>
5
- #include <worklets/Tools/WorkletEventHandler.h>
6
-
7
- #include <memory>
8
- #include <string>
9
- #include <utility>
10
- #include <vector>
11
-
12
- namespace worklets {
13
-
14
- void EventHandlerRegistry::registerEventHandler(const std::shared_ptr<WorkletEventHandler> &eventHandler) {
15
- const std::lock_guard<std::mutex> lock(instanceMutex);
16
- const auto &eventName = eventHandler->getEventName();
17
- auto handlerId = eventHandler->getHandlerId();
18
-
19
- if (eventHandler->shouldIgnoreEmitterReactTag()) {
20
- eventMappingsWithoutTag[eventName][handlerId] = eventHandler;
21
- } else {
22
- const auto emitterReactTag = eventHandler->getEmitterReactTag();
23
- const auto eventHash = std::make_pair(emitterReactTag, eventName);
24
- eventMappingsWithTag[eventHash][handlerId] = eventHandler;
25
- }
26
- eventHandlers[handlerId] = eventHandler;
27
- }
28
-
29
- void EventHandlerRegistry::unregisterEventHandler(const uint64_t id) {
30
- const std::lock_guard<std::mutex> lock(instanceMutex);
31
- auto handlerIt = eventHandlers.find(id);
32
- if (handlerIt != eventHandlers.end()) {
33
- const auto &eventHandler = handlerIt->second;
34
- const auto &eventName = eventHandler->getEventName();
35
-
36
- if (eventHandler->shouldIgnoreEmitterReactTag()) {
37
- const auto eventMappingIt = eventMappingsWithoutTag.find(eventName);
38
- auto &handlersMap = eventMappingIt->second;
39
- handlersMap.erase(id);
40
- if (handlersMap.empty()) {
41
- eventMappingsWithoutTag.erase(eventMappingIt);
42
- }
43
- } else {
44
- const auto emitterReactTag = eventHandler->getEmitterReactTag();
45
- const auto eventHash = std::make_pair(emitterReactTag, eventName);
46
- const auto eventMappingIt = eventMappingsWithTag.find(eventHash);
47
- auto &handlersMap = eventMappingIt->second;
48
- handlersMap.erase(id);
49
- if (handlersMap.empty()) {
50
- eventMappingsWithTag.erase(eventMappingIt);
51
- }
52
- }
53
- eventHandlers.erase(handlerIt);
54
- }
55
- }
56
-
57
- void EventHandlerRegistry::processEvent(
58
- const std::shared_ptr<WorkletRuntime> &uiWorkletRuntime,
59
- const double eventTimestamp,
60
- const std::string &eventName,
61
- const int emitterReactTag,
62
- const jsi::Value &eventPayload) {
63
- std::vector<std::shared_ptr<WorkletEventHandler>> handlersForEvent;
64
- {
65
- const std::lock_guard<std::mutex> lock(instanceMutex);
66
- auto handlersIt = eventMappingsWithoutTag.find(eventName);
67
- if (handlersIt != eventMappingsWithoutTag.end()) {
68
- for (const auto &handler : handlersIt->second) {
69
- handlersForEvent.push_back(handler.second);
70
- }
71
- }
72
- const auto eventHash = std::make_pair(emitterReactTag, eventName);
73
- auto handlersWithTagIt = eventMappingsWithTag.find(eventHash);
74
- if (handlersWithTagIt != eventMappingsWithTag.end()) {
75
- for (const auto &handler : handlersWithTagIt->second) {
76
- handlersForEvent.push_back(handler.second);
77
- }
78
- }
79
- }
80
-
81
- jsi::Runtime &rt = uiWorkletRuntime->getJSIRuntime();
82
- eventPayload.asObject(rt).setProperty(rt, "eventName", jsi::String::createFromUtf8(rt, eventName));
83
- for (const auto &handler : handlersForEvent) {
84
- handler->process(uiWorkletRuntime, eventTimestamp, eventPayload);
85
- }
86
- }
87
-
88
- bool EventHandlerRegistry::isAnyHandlerWaitingForEvent(const std::string &eventName, const int emitterReactTag) {
89
- const std::lock_guard<std::mutex> lock(instanceMutex);
90
- const auto eventHash = std::make_pair(emitterReactTag, eventName);
91
- auto it = eventMappingsWithTag.find(eventHash);
92
- return it != eventMappingsWithTag.end() && !it->second.empty();
93
- }
94
-
95
- } // namespace worklets
@@ -1,44 +0,0 @@
1
- // EventHandlerRegistry was fully moved to Reanimated.
2
- // This file is kept only for backwards compatibility with older versions of Reanimated.
3
-
4
- #pragma once
5
-
6
- #include <worklets/WorkletRuntime/WorkletRuntime.h>
7
-
8
- #include <jsi/jsi.h>
9
-
10
- #include <map>
11
- #include <memory>
12
- #include <mutex>
13
- #include <string>
14
- #include <unordered_map>
15
- #include <utility>
16
-
17
- using namespace facebook;
18
-
19
- namespace worklets {
20
-
21
- class WorkletEventHandler;
22
-
23
- class EventHandlerRegistry {
24
- std::map<std::pair<int, std::string>, std::unordered_map<uint64_t, std::shared_ptr<WorkletEventHandler>>>
25
- eventMappingsWithTag;
26
- std::map<std::string, std::unordered_map<uint64_t, std::shared_ptr<WorkletEventHandler>>> eventMappingsWithoutTag;
27
- std::map<uint64_t, std::shared_ptr<WorkletEventHandler>> eventHandlers;
28
- std::mutex instanceMutex;
29
-
30
- public:
31
- void registerEventHandler(const std::shared_ptr<WorkletEventHandler> &eventHandler);
32
- void unregisterEventHandler(const uint64_t id);
33
-
34
- void processEvent(
35
- const std::shared_ptr<WorkletRuntime> &uiWorkletRuntime,
36
- const double eventTimestamp,
37
- const std::string &eventName,
38
- const int emitterReactTag,
39
- const jsi::Value &eventPayload);
40
-
41
- bool isAnyHandlerWaitingForEvent(const std::string &eventName, const int emitterReactTag);
42
- };
43
-
44
- } // namespace worklets
@@ -1,34 +0,0 @@
1
- // WorkletEventHandler was fully moved to Reanimated.
2
- // This file is kept only for backwards compatibility with older versions of Reanimated.
3
-
4
- #include <worklets/Tools/WorkletEventHandler.h>
5
-
6
- #include <memory>
7
- #include <string>
8
-
9
- namespace worklets {
10
-
11
- void WorkletEventHandler::process(
12
- const std::shared_ptr<WorkletRuntime> &workletRuntime,
13
- const double eventTimestamp,
14
- const jsi::Value &eventValue) const {
15
- workletRuntime->runSync(handlerFunction_, jsi::Value(eventTimestamp), eventValue);
16
- }
17
-
18
- uint64_t WorkletEventHandler::getHandlerId() const {
19
- return handlerId_;
20
- }
21
-
22
- const std::string &WorkletEventHandler::getEventName() const {
23
- return eventName_;
24
- }
25
-
26
- uint64_t WorkletEventHandler::getEmitterReactTag() const {
27
- return emitterReactTag_;
28
- }
29
-
30
- bool WorkletEventHandler::shouldIgnoreEmitterReactTag() const {
31
- return emitterReactTag_ == -1;
32
- }
33
-
34
- } // namespace worklets
@@ -1,44 +0,0 @@
1
- // WorkletEventHandler was fully moved to Reanimated.
2
- // This file is kept only for backwards compatibility with older versions of Reanimated.
3
-
4
- #pragma once
5
-
6
- #include <worklets/SharedItems/Serializable.h>
7
- #include <worklets/WorkletRuntime/WorkletRuntime.h>
8
-
9
- #include <jsi/jsi.h>
10
-
11
- #include <memory>
12
- #include <string>
13
-
14
- using namespace facebook;
15
-
16
- namespace worklets {
17
-
18
- class WorkletEventHandler {
19
- const uint64_t handlerId_;
20
- const uint64_t emitterReactTag_;
21
- const std::string eventName_;
22
- const std::shared_ptr<SerializableWorklet> handlerFunction_;
23
-
24
- public:
25
- WorkletEventHandler(
26
- const uint64_t handlerId,
27
- const std::string &eventName,
28
- const uint64_t emitterReactTag,
29
- const std::shared_ptr<SerializableWorklet> &handlerFunction)
30
- : handlerId_(handlerId),
31
- emitterReactTag_(emitterReactTag),
32
- eventName_(eventName),
33
- handlerFunction_(handlerFunction) {}
34
- void process(
35
- const std::shared_ptr<WorkletRuntime> &workletRuntime,
36
- double eventTimestamp,
37
- const jsi::Value &eventValue) const;
38
- uint64_t getHandlerId() const;
39
- const std::string &getEventName() const;
40
- uint64_t getEmitterReactTag() const;
41
- bool shouldIgnoreEmitterReactTag() const;
42
- };
43
-
44
- } // namespace worklets
@@ -1,16 +0,0 @@
1
- export function countOccurrences(haystack: string, needle: string): number {
2
- if (!needle) {
3
- return 0;
4
- }
5
- let count = 0;
6
- let from = 0;
7
- while (true) {
8
- const idx = haystack.indexOf(needle, from);
9
- if (idx === -1) {
10
- break;
11
- }
12
- count++;
13
- from = idx + needle.length;
14
- }
15
- return count;
16
- }