@depup/react-native-worklets 0.12.1-depup.0 → 0.13.0-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 (311) hide show
  1. package/Common/cpp/worklets/AnimationFrameQueue/AnimationFrameBatchinator.cpp +1 -2
  2. package/Common/cpp/worklets/AnimationFrameQueue/AnimationFrameBatchinator.h +1 -1
  3. package/Common/cpp/worklets/Compat/StableApi.cpp +24 -8
  4. package/Common/cpp/worklets/Compat/StableApi.h +1 -1
  5. package/Common/cpp/worklets/NativeModules/JSIWorkletsModuleProxy.cpp +113 -50
  6. package/Common/cpp/worklets/NativeModules/JSIWorkletsModuleProxy.h +10 -1
  7. package/Common/cpp/worklets/NativeModules/WorkletsModuleProxy.cpp +95 -37
  8. package/Common/cpp/worklets/NativeModules/WorkletsModuleProxy.h +57 -7
  9. package/Common/cpp/worklets/NativeModules/WorkletsModuleProxyInitializer.cpp +94 -0
  10. package/Common/cpp/worklets/NativeModules/WorkletsModuleProxyInitializer.h +58 -0
  11. package/Common/cpp/worklets/Networking/Networking.cpp +283 -0
  12. package/Common/cpp/worklets/Networking/Networking.h +51 -0
  13. package/Common/cpp/worklets/Networking/NetworkingBackend.h +47 -0
  14. package/Common/cpp/worklets/Networking/NetworkingInstaller.cpp +127 -0
  15. package/Common/cpp/worklets/Networking/NetworkingInstaller.h +19 -0
  16. package/Common/cpp/worklets/Networking/NetworkingTypes.h +33 -0
  17. package/Common/cpp/worklets/Networking/TextDecoding.cpp +189 -0
  18. package/Common/cpp/worklets/Networking/TextDecoding.h +29 -0
  19. package/Common/cpp/worklets/RunLoop/AsyncQueue.h +28 -0
  20. package/Common/cpp/worklets/RunLoop/AsyncQueueImpl.cpp +9 -0
  21. package/Common/cpp/worklets/RunLoop/AsyncQueueImpl.h +6 -0
  22. package/Common/cpp/worklets/RunLoop/EventLoop.cpp +19 -4
  23. package/Common/cpp/worklets/RunLoop/EventLoop.h +3 -0
  24. package/Common/cpp/worklets/SharedItems/MemoryManager.cpp +1 -1
  25. package/Common/cpp/worklets/SharedItems/MemoryManager.h +2 -1
  26. package/Common/cpp/worklets/SharedItems/Serializable/CustomSerializable.cpp +37 -0
  27. package/Common/cpp/worklets/SharedItems/Serializable/CustomSerializable.h +33 -0
  28. package/Common/cpp/worklets/SharedItems/Serializable/RetainingSerializable.h +112 -0
  29. package/Common/cpp/worklets/SharedItems/Serializable/Serializable.cpp +129 -0
  30. package/Common/cpp/worklets/SharedItems/Serializable/Serializable.h +121 -0
  31. package/Common/cpp/worklets/SharedItems/Serializable/SerializableArray.cpp +38 -0
  32. package/Common/cpp/worklets/SharedItems/Serializable/SerializableArray.h +39 -0
  33. package/Common/cpp/worklets/SharedItems/Serializable/SerializableArrayBuffer.cpp +35 -0
  34. package/Common/cpp/worklets/SharedItems/Serializable/SerializableArrayBuffer.h +43 -0
  35. package/Common/cpp/worklets/SharedItems/Serializable/SerializableBigInt.cpp +23 -0
  36. package/Common/cpp/worklets/SharedItems/Serializable/SerializableBigInt.h +35 -0
  37. package/Common/cpp/worklets/SharedItems/Serializable/SerializableError.cpp +38 -0
  38. package/Common/cpp/worklets/SharedItems/Serializable/SerializableError.h +30 -0
  39. package/Common/cpp/worklets/SharedItems/Serializable/SerializableHostFunction.cpp +24 -0
  40. package/Common/cpp/worklets/SharedItems/Serializable/SerializableHostFunction.h +32 -0
  41. package/Common/cpp/worklets/SharedItems/Serializable/SerializableHostObject.cpp +19 -0
  42. package/Common/cpp/worklets/SharedItems/Serializable/SerializableHostObject.h +25 -0
  43. package/Common/cpp/worklets/SharedItems/Serializable/SerializableImport.cpp +29 -0
  44. package/Common/cpp/worklets/SharedItems/Serializable/SerializableImport.h +25 -0
  45. package/Common/cpp/worklets/SharedItems/Serializable/SerializableMap.cpp +44 -0
  46. package/Common/cpp/worklets/SharedItems/Serializable/SerializableMap.h +25 -0
  47. package/Common/cpp/worklets/SharedItems/Serializable/SerializableObject.cpp +80 -0
  48. package/Common/cpp/worklets/SharedItems/Serializable/SerializableObject.h +35 -0
  49. package/Common/cpp/worklets/SharedItems/Serializable/SerializableRegExp.cpp +22 -0
  50. package/Common/cpp/worklets/SharedItems/Serializable/SerializableRegExp.h +25 -0
  51. package/Common/cpp/worklets/SharedItems/{SerializableRemoteFunction.cpp → Serializable/SerializableRemoteFunction.cpp} +30 -2
  52. package/Common/cpp/worklets/SharedItems/{SerializableRemoteFunction.h → Serializable/SerializableRemoteFunction.h} +24 -10
  53. package/Common/cpp/worklets/SharedItems/Serializable/SerializableScalar.cpp +46 -0
  54. package/Common/cpp/worklets/SharedItems/Serializable/SerializableScalar.h +41 -0
  55. package/Common/cpp/worklets/SharedItems/Serializable/SerializableSet.cpp +36 -0
  56. package/Common/cpp/worklets/SharedItems/Serializable/SerializableSet.h +23 -0
  57. package/Common/cpp/worklets/SharedItems/Serializable/SerializableString.cpp +19 -0
  58. package/Common/cpp/worklets/SharedItems/Serializable/SerializableString.h +22 -0
  59. package/Common/cpp/worklets/SharedItems/Serializable/SerializableTurboModuleLike.cpp +26 -0
  60. package/Common/cpp/worklets/SharedItems/Serializable/SerializableTurboModuleLike.h +33 -0
  61. package/Common/cpp/worklets/SharedItems/Serializable/SerializableWorklet.cpp +31 -0
  62. package/Common/cpp/worklets/SharedItems/Serializable/SerializableWorklet.h +23 -0
  63. package/Common/cpp/worklets/SharedItems/Serializable.h +21 -409
  64. package/Common/cpp/worklets/SharedItems/Shareable.cpp +1 -1
  65. package/Common/cpp/worklets/SharedItems/Shareable.h +1 -1
  66. package/Common/cpp/worklets/SharedItems/Synchronizable.h +43 -20
  67. package/Common/cpp/worklets/SharedItems/SynchronizableAccess.cpp +24 -26
  68. package/Common/cpp/worklets/SharedItems/SynchronizableAccess.h +5 -7
  69. package/Common/cpp/worklets/SharedItems/SynchronizableDynamic.cpp +36 -0
  70. package/Common/cpp/worklets/SharedItems/SynchronizableDynamic.h +27 -0
  71. package/Common/cpp/worklets/SharedItems/SynchronizableFixed.cpp +76 -0
  72. package/Common/cpp/worklets/SharedItems/SynchronizableFixed.h +48 -0
  73. package/Common/cpp/worklets/Tools/ScriptBuffer.h +0 -5
  74. package/Common/cpp/worklets/Tools/WorkletsJSIUtils.h +16 -30
  75. package/Common/cpp/worklets/WorkletRuntime/RuntimeBindings.h +27 -30
  76. package/Common/cpp/worklets/WorkletRuntime/RuntimeManager.cpp +4 -3
  77. package/Common/cpp/worklets/WorkletRuntime/RuntimeManager.h +2 -1
  78. package/Common/cpp/worklets/WorkletRuntime/WorkletRuntime.cpp +79 -29
  79. package/Common/cpp/worklets/WorkletRuntime/WorkletRuntime.h +34 -19
  80. package/Common/cpp/worklets/WorkletRuntime/WorkletRuntimeDecorator.cpp +13 -86
  81. package/Common/cpp/worklets/WorkletRuntime/WorkletRuntimeDecorator.h +1 -7
  82. package/Package.swift +147 -0
  83. package/README.md +7 -7
  84. package/RNWorklets.podspec +1 -2
  85. package/android/CMakeLists.txt +7 -26
  86. package/android/build.gradle.kts +5 -31
  87. package/android/src/main/cpp/worklets/android/WorkletsModule.cpp +46 -129
  88. package/android/src/main/cpp/worklets/android/WorkletsModule.h +21 -11
  89. package/android/src/main/cpp/worklets/android/WorkletsOnLoad.cpp +2 -4
  90. package/android/src/main/cpp/worklets/android/networking/AndroidNetworkingBackend.cpp +82 -0
  91. package/android/src/main/cpp/worklets/android/networking/AndroidNetworkingBackend.h +32 -0
  92. package/android/src/main/cpp/worklets/android/networking/JNetworkRequestListener.cpp +73 -0
  93. package/android/src/main/cpp/worklets/android/networking/JNetworkRequestListener.h +38 -0
  94. package/android/src/{no-networking → main/java}/com/swmansion/worklets/WorkletsModule.kt +40 -28
  95. package/android/src/main/java/com/swmansion/worklets/networking/NetworkRequestListener.kt +37 -0
  96. package/android/src/main/java/com/swmansion/worklets/networking/Networking.kt +244 -0
  97. package/android/src/main/java/com/swmansion/worklets/runloop/AnimationFrameQueue.kt +40 -5
  98. package/apple/worklets/apple/AnimationFrameQueue.mm +9 -1
  99. package/apple/worklets/apple/AssertJavaScriptQueue.h +3 -2
  100. package/apple/worklets/apple/AssertTurboModuleManagerQueue.h +4 -0
  101. package/apple/worklets/apple/IOSUIScheduler.mm +3 -0
  102. package/apple/worklets/apple/Networking/AppleNetworkingBackend.h +29 -0
  103. package/apple/worklets/apple/Networking/AppleNetworkingBackend.mm +93 -0
  104. package/apple/worklets/apple/Networking/WorkletsURLSessionDelegate.h +37 -0
  105. package/apple/worklets/apple/Networking/WorkletsURLSessionDelegate.mm +447 -0
  106. package/apple/worklets/apple/WorkletsModule.h +3 -2
  107. package/apple/worklets/apple/WorkletsModule.mm +82 -74
  108. package/bundleMode/index.js +10 -47
  109. package/bundleMode/polyfills/prepareBundleMode.js +16 -0
  110. package/changes.json +6 -6
  111. package/compatibility.json +4 -1
  112. package/lib/module/WorkletsModule/NativeWorklets.native.js +12 -9
  113. package/lib/module/WorkletsModule/NativeWorklets.native.js.map +1 -1
  114. package/lib/module/debug/jsVersion.js +1 -1
  115. package/lib/module/featureFlags/staticFlags.json +0 -1
  116. package/lib/module/featureFlags/types.js +0 -1
  117. package/lib/module/featureFlags/types.js.map +1 -1
  118. package/lib/module/index.js +1 -1
  119. package/lib/module/index.js.map +1 -1
  120. package/lib/module/initializers/initializers.native.js +5 -12
  121. package/lib/module/initializers/initializers.native.js.map +1 -1
  122. package/lib/module/memory/bundleUnpacker.native.js +2 -10
  123. package/lib/module/memory/bundleUnpacker.native.js.map +1 -1
  124. package/lib/module/memory/serializable.native.js +3 -31
  125. package/lib/module/memory/serializable.native.js.map +1 -1
  126. package/lib/module/memory/synchronizable.js +1 -1
  127. package/lib/module/memory/synchronizable.js.map +1 -1
  128. package/lib/module/memory/synchronizable.native.js +7 -3
  129. package/lib/module/memory/synchronizable.native.js.map +1 -1
  130. package/lib/module/memory/synchronizableUnpacker.native.js +22 -9
  131. package/lib/module/memory/synchronizableUnpacker.native.js.map +1 -1
  132. package/lib/module/memory/valueUnpacker.native.js +0 -8
  133. package/lib/module/memory/valueUnpacker.native.js.map +1 -1
  134. package/lib/module/mock.js +22 -1
  135. package/lib/module/mock.js.map +1 -1
  136. package/lib/module/networking/AbortController.js +43 -0
  137. package/lib/module/networking/AbortController.js.map +1 -0
  138. package/lib/module/networking/Blob.js +60 -0
  139. package/lib/module/networking/Blob.js.map +1 -0
  140. package/lib/module/networking/DOMException.js +35 -0
  141. package/lib/module/networking/DOMException.js.map +1 -0
  142. package/lib/module/networking/FileReader.js +83 -0
  143. package/lib/module/networking/FileReader.js.map +1 -0
  144. package/lib/module/networking/FormData.js +105 -0
  145. package/lib/module/networking/FormData.js.map +1 -0
  146. package/lib/module/networking/XMLHttpRequest.js +450 -0
  147. package/lib/module/networking/XMLHttpRequest.js.map +1 -0
  148. package/lib/module/networking/bytes.js +6 -0
  149. package/lib/module/networking/bytes.js.map +1 -0
  150. package/lib/module/networking/events.js +44 -0
  151. package/lib/module/networking/events.js.map +1 -0
  152. package/lib/module/networking/http.js +100 -0
  153. package/lib/module/networking/http.js.map +1 -0
  154. package/lib/module/networking/install.js +37 -0
  155. package/lib/module/networking/install.js.map +1 -0
  156. package/lib/module/networking/utf8.js +99 -0
  157. package/lib/module/networking/utf8.js.map +1 -0
  158. package/lib/module/privateGlobals.d.js +0 -1
  159. package/lib/module/privateGlobals.d.js.map +1 -1
  160. package/lib/module/runLoop/common/queueMicrotaskPolyfill.js +9 -0
  161. package/lib/module/runLoop/common/queueMicrotaskPolyfill.js.map +1 -0
  162. package/lib/module/runLoop/uiRuntime/requestAnimationFrame.js +7 -3
  163. package/lib/module/runLoop/uiRuntime/requestAnimationFrame.js.map +1 -1
  164. package/lib/module/runLoop/workletRuntime/index.js +1 -1
  165. package/lib/module/runLoop/workletRuntime/index.js.map +1 -1
  166. package/lib/module/runLoop/workletRuntime/requestAnimationFramePolyfill.js +1 -1
  167. package/lib/module/runLoop/workletRuntime/requestAnimationFramePolyfill.js.map +1 -1
  168. package/lib/module/runLoop/workletRuntime/taskQueue.js +0 -13
  169. package/lib/module/runLoop/workletRuntime/taskQueue.js.map +1 -1
  170. package/lib/module/runtimes.native.js +8 -1
  171. package/lib/module/runtimes.native.js.map +1 -1
  172. package/lib/module/specs/NativeWorkletsModule.js.map +1 -1
  173. package/lib/module/threads.js +11 -0
  174. package/lib/module/threads.js.map +1 -1
  175. package/lib/module/threads.native.js +35 -42
  176. package/lib/module/threads.native.js.map +1 -1
  177. package/lib/typescript/WorkletsModule/NativeWorklets.native.d.ts.map +1 -1
  178. package/lib/typescript/WorkletsModule/workletsModuleProxy.d.ts +6 -5
  179. package/lib/typescript/WorkletsModule/workletsModuleProxy.d.ts.map +1 -1
  180. package/lib/typescript/debug/jsVersion.d.ts +1 -1
  181. package/lib/typescript/featureFlags/types.d.ts +0 -1
  182. package/lib/typescript/featureFlags/types.d.ts.map +1 -1
  183. package/lib/typescript/index.d.ts +2 -2
  184. package/lib/typescript/index.d.ts.map +1 -1
  185. package/lib/typescript/memory/bundleUnpacker.native.d.ts.map +1 -1
  186. package/lib/typescript/memory/serializable.native.d.ts.map +1 -1
  187. package/lib/typescript/memory/synchronizable.d.ts +6 -2
  188. package/lib/typescript/memory/synchronizable.d.ts.map +1 -1
  189. package/lib/typescript/memory/synchronizable.native.d.ts +6 -2
  190. package/lib/typescript/memory/synchronizable.native.d.ts.map +1 -1
  191. package/lib/typescript/memory/synchronizableUnpacker.native.d.ts +1 -1
  192. package/lib/typescript/memory/synchronizableUnpacker.native.d.ts.map +1 -1
  193. package/lib/typescript/memory/types.d.ts +6 -0
  194. package/lib/typescript/memory/types.d.ts.map +1 -1
  195. package/lib/typescript/memory/valueUnpacker.native.d.ts.map +1 -1
  196. package/lib/typescript/networking/AbortController.d.ts +16 -0
  197. package/lib/typescript/networking/AbortController.d.ts.map +1 -0
  198. package/lib/typescript/networking/Blob.d.ts +16 -0
  199. package/lib/typescript/networking/Blob.d.ts.map +1 -0
  200. package/lib/typescript/networking/DOMException.d.ts +5 -0
  201. package/lib/typescript/networking/DOMException.d.ts.map +1 -0
  202. package/lib/typescript/networking/FileReader.d.ts +19 -0
  203. package/lib/typescript/networking/FileReader.d.ts.map +1 -0
  204. package/lib/typescript/networking/FormData.d.ts +30 -0
  205. package/lib/typescript/networking/FormData.d.ts.map +1 -0
  206. package/lib/typescript/networking/XMLHttpRequest.d.ts +77 -0
  207. package/lib/typescript/networking/XMLHttpRequest.d.ts.map +1 -0
  208. package/lib/typescript/networking/bytes.d.ts +2 -0
  209. package/lib/typescript/networking/bytes.d.ts.map +1 -0
  210. package/lib/typescript/networking/events.d.ts +18 -0
  211. package/lib/typescript/networking/events.d.ts.map +1 -0
  212. package/lib/typescript/networking/http.d.ts +14 -0
  213. package/lib/typescript/networking/http.d.ts.map +1 -0
  214. package/lib/typescript/networking/install.d.ts +2 -0
  215. package/lib/typescript/networking/install.d.ts.map +1 -0
  216. package/lib/typescript/networking/utf8.d.ts +4 -0
  217. package/lib/typescript/networking/utf8.d.ts.map +1 -0
  218. package/lib/typescript/runLoop/{workletRuntime/queueMicrotask.d.ts → common/queueMicrotaskPolyfill.d.ts} +1 -1
  219. package/lib/typescript/runLoop/common/queueMicrotaskPolyfill.d.ts.map +1 -0
  220. package/lib/typescript/runLoop/uiRuntime/requestAnimationFrame.d.ts.map +1 -1
  221. package/lib/typescript/runLoop/workletRuntime/taskQueue.d.ts +0 -2
  222. package/lib/typescript/runLoop/workletRuntime/taskQueue.d.ts.map +1 -1
  223. package/lib/typescript/runtimes.native.d.ts.map +1 -1
  224. package/lib/typescript/specs/NativeWorkletsModule.d.ts +1 -0
  225. package/lib/typescript/specs/NativeWorkletsModule.d.ts.map +1 -1
  226. package/lib/typescript/threads.d.ts +8 -0
  227. package/lib/typescript/threads.d.ts.map +1 -1
  228. package/lib/typescript/threads.native.d.ts +8 -1
  229. package/lib/typescript/threads.native.d.ts.map +1 -1
  230. package/lib/typescript/types.d.ts +9 -5
  231. package/lib/typescript/types.d.ts.map +1 -1
  232. package/package.json +36 -22
  233. package/plugin/index.js +63 -93
  234. package/plugin-oxc/babel.d.ts +7 -0
  235. package/plugin-oxc/babel.js +200 -0
  236. package/plugin-oxc/index.d.ts +33 -0
  237. package/plugin-oxc/index.js +36 -0
  238. package/plugin-oxc/worklets-oxc-plugin.darwin-arm64.node +0 -0
  239. package/plugin-oxc/worklets-oxc-plugin.darwin-x64.node +0 -0
  240. package/plugin-oxc/worklets-oxc-plugin.linux-arm64.node +0 -0
  241. package/plugin-oxc/worklets-oxc-plugin.linux-x64.node +0 -0
  242. package/plugin-oxc/worklets-oxc-plugin.win32-arm64.node +0 -0
  243. package/plugin-oxc/worklets-oxc-plugin.win32-x64.node +0 -0
  244. package/react-native.config.js +5 -0
  245. package/scripts/worklets_utils.rb +0 -10
  246. package/src/WorkletsModule/NativeWorklets.native.ts +26 -9
  247. package/src/WorkletsModule/workletsModuleProxy.ts +15 -5
  248. package/src/debug/jsVersion.ts +1 -1
  249. package/src/featureFlags/staticFlags.json +0 -1
  250. package/src/featureFlags/types.ts +0 -1
  251. package/src/index.ts +3 -0
  252. package/src/initializers/initializers.native.ts +5 -13
  253. package/src/memory/bundleUnpacker.native.ts +6 -13
  254. package/src/memory/serializable.native.ts +6 -46
  255. package/src/memory/synchronizable.native.ts +38 -4
  256. package/src/memory/synchronizable.ts +22 -2
  257. package/src/memory/synchronizableUnpacker.native.ts +38 -13
  258. package/src/memory/types.ts +9 -0
  259. package/src/memory/valueUnpacker.native.ts +0 -8
  260. package/src/mock.ts +28 -1
  261. package/src/networking/AbortController.ts +57 -0
  262. package/src/networking/Blob.ts +74 -0
  263. package/src/networking/DOMException.ts +37 -0
  264. package/src/networking/FileReader.ts +99 -0
  265. package/src/networking/FormData.ts +135 -0
  266. package/src/networking/XMLHttpRequest.ts +557 -0
  267. package/src/networking/bytes.ts +8 -0
  268. package/src/networking/events.ts +61 -0
  269. package/src/networking/http.ts +173 -0
  270. package/src/networking/install.ts +38 -0
  271. package/src/networking/utf8.ts +122 -0
  272. package/src/privateGlobals.d.ts +20 -13
  273. package/src/runLoop/common/queueMicrotaskPolyfill.ts +12 -0
  274. package/src/runLoop/uiRuntime/requestAnimationFrame.ts +7 -4
  275. package/src/runLoop/workletRuntime/index.ts +1 -1
  276. package/src/runLoop/workletRuntime/requestAnimationFramePolyfill.ts +1 -1
  277. package/src/runLoop/workletRuntime/taskQueue.ts +0 -15
  278. package/src/runtimes.native.ts +10 -1
  279. package/src/specs/NativeWorkletsModule.ts +1 -0
  280. package/src/threads.native.ts +49 -54
  281. package/src/threads.ts +11 -0
  282. package/src/types.ts +9 -5
  283. package/Common/cpp/worklets/SharedItems/Serializable.cpp +0 -373
  284. package/Common/cpp/worklets/SharedItems/SerializableFactory.cpp +0 -172
  285. package/Common/cpp/worklets/SharedItems/SerializableFactory.h +0 -85
  286. package/Common/cpp/worklets/SharedItems/Synchronizable.cpp +0 -59
  287. package/android/src/main/cpp/worklets/android/JWorkletRuntimeWrapper.cpp +0 -51
  288. package/android/src/main/cpp/worklets/android/JWorkletRuntimeWrapper.h +0 -43
  289. package/android/src/networking/com/swmansion/worklets/WorkletRuntimeWrapper.kt +0 -27
  290. package/android/src/networking/com/swmansion/worklets/WorkletsHeaderUtil.kt +0 -30
  291. package/android/src/networking/com/swmansion/worklets/WorkletsModule.kt +0 -203
  292. package/android/src/networking/com/swmansion/worklets/WorkletsNetworkEventUtil.kt +0 -272
  293. package/android/src/networking/com/swmansion/worklets/WorkletsNetworking.kt +0 -1124
  294. package/android/src/networking/com/swmansion/worklets/WorkletsOkHttpCallUtil.kt +0 -40
  295. package/android/src/networking/com/swmansion/worklets/WorkletsProgressListener.kt +0 -13
  296. package/android/src/networking/com/swmansion/worklets/WorkletsProgressRequestBody.kt +0 -98
  297. package/android/src/networking/com/swmansion/worklets/WorkletsProgressResponseBody.kt +0 -60
  298. package/android/src/networking/com/swmansion/worklets/WorkletsProgressiveStringDecoder.kt +0 -87
  299. package/android/src/networking/com/swmansion/worklets/WorkletsRequestBodyUtil.kt +0 -181
  300. package/apple/worklets/apple/Networking/WorkletsNetworking.h +0 -25
  301. package/apple/worklets/apple/Networking/WorkletsNetworking.mm +0 -707
  302. package/bundleMode/shims/turboModuleRegistryShim.js +0 -47
  303. package/lib/module/bundleMode/network.native.js +0 -44
  304. package/lib/module/bundleMode/network.native.js.map +0 -1
  305. package/lib/module/runLoop/workletRuntime/queueMicrotask.js +0 -11
  306. package/lib/module/runLoop/workletRuntime/queueMicrotask.js.map +0 -1
  307. package/lib/typescript/bundleMode/network.native.d.ts +0 -7
  308. package/lib/typescript/bundleMode/network.native.d.ts.map +0 -1
  309. package/lib/typescript/runLoop/workletRuntime/queueMicrotask.d.ts.map +0 -1
  310. package/src/bundleMode/network.native.ts +0 -63
  311. package/src/runLoop/workletRuntime/queueMicrotask.ts +0 -13
@@ -3,6 +3,7 @@
3
3
  #include <jsi/jsi.h>
4
4
  #include <worklets/AnimationFrameQueue/AnimationFrameBatchinator.h>
5
5
  #include <worklets/NativeModules/JSIWorkletsModuleProxy.h>
6
+ #include <worklets/Networking/Networking.h>
6
7
  #include <worklets/SharedItems/MemoryManager.h>
7
8
  #include <worklets/SharedItems/UnpackerLoader.h>
8
9
  #include <worklets/Tools/JSLogger.h>
@@ -16,20 +17,65 @@
16
17
  #include <worklets/WorkletRuntime/WorkletRuntime.h>
17
18
 
18
19
  #include <memory>
20
+ #include <optional>
19
21
 
20
22
  namespace worklets {
21
23
 
24
+ /**
25
+ * The initialization is a sequence of steps. The steps which do not need the RN Runtime run on background threads
26
+ * before the bundle asks for the module. The steps which need the RN Runtime run on the JS thread.
27
+ *
28
+ * Every mode starts the same way:
29
+ *
30
+ * 1. Native module creation: the platform creates the native module before the bundle runs. The module creates a
31
+ * `WorkletsModuleProxyInitializer` and dispatches `WorkletsModuleProxyInitializer::prepareProxy` to a
32
+ * background thread.
33
+ * 2. Background thread: `prepareProxy` calls the constructor of this class. The constructor creates the UI Worklet
34
+ * Runtime, but does not initialize it.
35
+ *
36
+ * Legacy Mode continues with:
37
+ *
38
+ * 3. JS thread: the bundle evaluates `NativeWorklets`, which calls `installTurboModule(false)`. The native module
39
+ * calls `WorkletsModuleProxyInitializer::finalize`. It waits for step 2 and calls `attachToRNRuntime` with a
40
+ * disabled Bundle Mode config. `attachToRNRuntime` creates the JSI proxy for the RN Runtime and installs it on
41
+ * the RN Runtime.
42
+ * 4. JS thread: `NativeWorklets` hands the unpacker code to the JSI proxy.
43
+ * 5. JS thread: `NativeWorklets` calls `start`. It initializes the UI Worklet Runtime and evaluates the unpacker
44
+ * code on it.
45
+ *
46
+ * Bundle Mode continues with:
47
+ *
48
+ * 3. JS thread: the `prepareBundleMode` polyfill runs before the first module of the bundle. The Worklets plugin
49
+ * enables the polyfill only when Bundle Mode is on. The polyfill calls `prepareBundleMode` on the native
50
+ * module. The module calls `WorkletsModuleProxyInitializer::beginBundleModeAOT` and dispatches
51
+ * `WorkletsModuleProxyInitializer::prepareBundleModeAOT` to a background thread.
52
+ * 4. Background thread: `prepareBundleModeAOT` waits for step 2, loads the bundle and calls
53
+ * `startUIRuntimeInBundleModeAOT`. It stores the Bundle Mode config, initializes the UI Worklet Runtime
54
+ * and evaluates the bundle on it. This runs in parallel with the JS thread, which evaluates the bundle on the RN
55
+ * Runtime.
56
+ * 5. JS thread: the bundle evaluates `NativeWorklets`, which calls `installTurboModule(true)`. The native module
57
+ * calls `WorkletsModuleProxyInitializer::finalize`. It waits for step 4 and calls `attachToRNRuntime` without
58
+ * a Bundle Mode config, because step 4 already stored it. `attachToRNRuntime` creates the JSI proxy for the RN
59
+ * Runtime and installs it on the RN Runtime.
60
+ * 6. JS thread: `NativeWorklets` calls `start`. It returns immediately, because step 4 already initialized the UI
61
+ * Worklet Runtime.
62
+ *
63
+ * When Bundle Mode is enabled but the polyfill did not run, steps 3 and 4 are skipped. Step 5 then loads the bundle
64
+ * and passes the Bundle Mode config to `attachToRNRuntime`, and step 6 initializes the UI Worklet Runtime by
65
+ * evaluating the bundle on it, both on the JS thread.
66
+ */
22
67
  class WorkletsModuleProxy : public std::enable_shared_from_this<WorkletsModuleProxy> {
23
68
  public:
69
+ void startUIRuntimeInBundleModeAOT(const BundleModeConfig &bundleModeConfig);
70
+
71
+ void attachToRNRuntime(jsi::Runtime &rnRuntime, const std::optional<BundleModeConfig> &bundleModeConfig);
72
+
24
73
  void start();
25
74
 
26
75
  explicit WorkletsModuleProxy(
27
- jsi::Runtime &rnRuntime,
28
- const std::shared_ptr<CallInvoker> &jsCallInvoker,
76
+ const std::shared_ptr<JSScheduler> &jsScheduler,
29
77
  const std::shared_ptr<UIScheduler> &uiScheduler,
30
- std::function<bool()> &&isJavaScriptQueue,
31
78
  const std::shared_ptr<RuntimeBindings> &runtimeBindings,
32
- const BundleModeConfig &bundleModeConfig,
33
79
  const std::shared_ptr<RNRuntimeStatus> &rnRuntimeStatus);
34
80
 
35
81
  ~WorkletsModuleProxy();
@@ -55,19 +101,23 @@ class WorkletsModuleProxy : public std::enable_shared_from_this<WorkletsModulePr
55
101
  }
56
102
 
57
103
  private:
58
- const bool isDevBundle_;
104
+ void startUIRuntime(const std::shared_ptr<JSIWorkletsModuleProxy> &uiRuntimeProxy);
105
+
106
+ bool isDevBundle_;
59
107
  const std::shared_ptr<JSScheduler> jsScheduler_;
60
108
  const std::shared_ptr<UIScheduler> uiScheduler_;
61
109
  const std::shared_ptr<JSLogger> jsLogger_;
62
110
  const std::shared_ptr<RuntimeBindings> runtimeBindings_;
63
- const BundleModeConfig bundleModeConfig_;
111
+ BundleModeConfig bundleModeConfig_;
64
112
  const std::shared_ptr<MemoryManager> memoryManager_;
65
113
  const std::shared_ptr<RuntimeManager> runtimeManager_;
66
114
  const std::shared_ptr<UnpackerLoader> unpackerLoader_;
67
115
  const std::shared_ptr<RNRuntimeStatus> rnRuntimeStatus_;
116
+ const std::shared_ptr<Networking> networking_;
68
117
  std::shared_ptr<WorkletRuntime> uiWorkletRuntime_;
69
- const std::shared_ptr<JSIWorkletsModuleProxy> rnRuntimeProxy_;
118
+ std::shared_ptr<JSIWorkletsModuleProxy> rnRuntimeProxy_;
70
119
  std::shared_ptr<AnimationFrameBatchinator> animationFrameBatchinator_;
120
+ bool uiRuntimeStarted_;
71
121
  #ifndef NDEBUG
72
122
  SingleInstanceChecker<WorkletsModuleProxy> singleInstanceChecker_;
73
123
  #endif // NDEBUG
@@ -0,0 +1,94 @@
1
+ #include <worklets/NativeModules/WorkletsModuleProxyInitializer.h>
2
+
3
+ #include <react/debug/react_native_assert.h>
4
+
5
+ #include <exception>
6
+ #include <future>
7
+ #include <optional>
8
+ #include <stdexcept>
9
+ #include <utility>
10
+
11
+ namespace worklets {
12
+
13
+ WorkletsModuleProxyInitializer::WorkletsModuleProxyInitializer(
14
+ const std::shared_ptr<JSScheduler> &jsScheduler,
15
+ const std::shared_ptr<UIScheduler> &uiScheduler,
16
+ const std::shared_ptr<RuntimeBindings> &runtimeBindings,
17
+ const std::shared_ptr<RNRuntimeStatus> &rnRuntimeStatus)
18
+ : jsScheduler_(jsScheduler),
19
+ uiScheduler_(uiScheduler),
20
+ runtimeBindings_(runtimeBindings),
21
+ rnRuntimeStatus_(rnRuntimeStatus),
22
+ preparedProxy_(preparedProxyPromise_.get_future().share()),
23
+ aotProxy_(aotProxyPromise_.get_future().share()) {}
24
+
25
+ void WorkletsModuleProxyInitializer::prepareProxy() {
26
+ try {
27
+ preparedProxyPromise_.set_value(
28
+ std::make_shared<WorkletsModuleProxy>(jsScheduler_, uiScheduler_, runtimeBindings_, rnRuntimeStatus_));
29
+ } catch (const std::future_error &) {
30
+ throw std::runtime_error("[Worklets] prepareProxy must be called exactly once.");
31
+ } catch (...) {
32
+ preparedProxyPromise_.set_exception(std::current_exception());
33
+ }
34
+ }
35
+
36
+ void WorkletsModuleProxyInitializer::beginBundleModeAOT() {
37
+ react_native_assert(jsScheduler_->canInvokeSyncOnJS() && "beginBundleModeAOT must be called on the JS thread");
38
+ std::lock_guard lock(mutex_);
39
+ if (stage_ != Stage::Created) [[unlikely]] {
40
+ throw std::runtime_error("[Worklets] beginBundleModeAOT must be called at most once and before finalize.");
41
+ }
42
+ stage_ = Stage::AOTBegun;
43
+ }
44
+
45
+ void WorkletsModuleProxyInitializer::prepareBundleModeAOT(const BundleModeConfigLoader &loadBundleModeConfig) {
46
+ try {
47
+ auto proxy = preparedProxy_.get();
48
+ proxy->startUIRuntimeInBundleModeAOT(loadBundleModeConfig());
49
+ aotProxyPromise_.set_value(std::move(proxy));
50
+ } catch (const std::future_error &) {
51
+ throw std::runtime_error("[Worklets] prepareBundleModeAOT must be called exactly once.");
52
+ } catch (...) {
53
+ aotProxyPromise_.set_exception(std::current_exception());
54
+ }
55
+ }
56
+
57
+ std::shared_ptr<WorkletsModuleProxy> WorkletsModuleProxyInitializer::finalize(
58
+ jsi::Runtime &rnRuntime,
59
+ const bool bundleModeEnabled,
60
+ const BundleModeConfigLoader &loadBundleModeConfig) {
61
+ react_native_assert(jsScheduler_->canInvokeSyncOnJS() && "finalize must be called on the JS thread");
62
+ bool startedAOT = false;
63
+ {
64
+ std::lock_guard lock(mutex_);
65
+ if (stage_ == Stage::Finalized || stage_ == Stage::Invalidated) [[unlikely]] {
66
+ throw std::runtime_error("[Worklets] finalize must be called at most once and before invalidate.");
67
+ }
68
+ startedAOT = stage_ == Stage::AOTBegun;
69
+ stage_ = Stage::Finalized;
70
+ }
71
+ auto proxy = (startedAOT ? aotProxy_ : preparedProxy_).get();
72
+ if (startedAOT) {
73
+ proxy->attachToRNRuntime(rnRuntime, std::nullopt);
74
+ } else {
75
+ proxy->attachToRNRuntime(
76
+ rnRuntime, bundleModeEnabled ? loadBundleModeConfig() : BundleModeConfig{.enabled = false});
77
+ }
78
+ return proxy;
79
+ }
80
+
81
+ void WorkletsModuleProxyInitializer::invalidate() {
82
+ bool startedAOT = false;
83
+ {
84
+ std::lock_guard lock(mutex_);
85
+ startedAOT = stage_ == Stage::AOTBegun;
86
+ stage_ = Stage::Invalidated;
87
+ }
88
+ if (startedAOT) {
89
+ aotProxy_.wait();
90
+ }
91
+ preparedProxy_.wait();
92
+ }
93
+
94
+ } // namespace worklets
@@ -0,0 +1,58 @@
1
+ #pragma once
2
+
3
+ #include <jsi/jsi.h>
4
+ #include <worklets/NativeModules/WorkletsModuleProxy.h>
5
+ #include <worklets/Tools/JSScheduler.h>
6
+ #include <worklets/Tools/RNRuntimeStatus.h>
7
+ #include <worklets/Tools/UIScheduler.h>
8
+ #include <worklets/WorkletRuntime/BundleModeConfig.h>
9
+ #include <worklets/WorkletRuntime/RuntimeBindings.h>
10
+
11
+ #include <functional>
12
+ #include <future>
13
+ #include <memory>
14
+ #include <mutex>
15
+
16
+ namespace worklets {
17
+
18
+ class WorkletsModuleProxyInitializer {
19
+ public:
20
+ using BundleModeConfigLoader = std::function<BundleModeConfig()>;
21
+
22
+ WorkletsModuleProxyInitializer(
23
+ const std::shared_ptr<JSScheduler> &jsScheduler,
24
+ const std::shared_ptr<UIScheduler> &uiScheduler,
25
+ const std::shared_ptr<RuntimeBindings> &runtimeBindings,
26
+ const std::shared_ptr<RNRuntimeStatus> &rnRuntimeStatus);
27
+
28
+ void prepareProxy();
29
+
30
+ void beginBundleModeAOT();
31
+
32
+ void prepareBundleModeAOT(const BundleModeConfigLoader &loadBundleModeConfig);
33
+
34
+ std::shared_ptr<WorkletsModuleProxy>
35
+ finalize(jsi::Runtime &rnRuntime, bool bundleModeEnabled, const BundleModeConfigLoader &loadBundleModeConfig);
36
+
37
+ void invalidate();
38
+
39
+ private:
40
+ enum class Stage { Created, AOTBegun, Finalized, Invalidated };
41
+
42
+ using ProxyPromise = std::promise<std::shared_ptr<WorkletsModuleProxy>>;
43
+ using ProxyFuture = std::shared_future<std::shared_ptr<WorkletsModuleProxy>>;
44
+
45
+ const std::shared_ptr<JSScheduler> jsScheduler_;
46
+ const std::shared_ptr<UIScheduler> uiScheduler_;
47
+ const std::shared_ptr<RuntimeBindings> runtimeBindings_;
48
+ const std::shared_ptr<RNRuntimeStatus> rnRuntimeStatus_;
49
+
50
+ std::mutex mutex_;
51
+ Stage stage_{Stage::Created};
52
+ ProxyPromise preparedProxyPromise_;
53
+ ProxyFuture preparedProxy_;
54
+ ProxyPromise aotProxyPromise_;
55
+ ProxyFuture aotProxy_;
56
+ };
57
+
58
+ } // namespace worklets
@@ -0,0 +1,283 @@
1
+ #include <worklets/Networking/Networking.h>
2
+ #include <worklets/Networking/TextDecoding.h>
3
+ #include <worklets/SharedItems/Serializable/SerializableRemoteFunction.h>
4
+ #include <worklets/WorkletRuntime/WorkletRuntime.h>
5
+
6
+ #include <functional>
7
+ #include <string>
8
+ #include <utility>
9
+ #include <vector>
10
+
11
+ namespace worklets {
12
+
13
+ namespace {
14
+
15
+ class VectorBuffer final : public jsi::MutableBuffer {
16
+ public:
17
+ explicit VectorBuffer(std::vector<uint8_t> &&data) : data_(std::move(data)) {}
18
+
19
+ size_t size() const override {
20
+ return data_.size();
21
+ }
22
+
23
+ uint8_t *data() override {
24
+ return data_.data();
25
+ }
26
+
27
+ private:
28
+ std::vector<uint8_t> data_;
29
+ };
30
+
31
+ const char *errorName(const RequestError error) {
32
+ switch (error) {
33
+ case RequestError::Timeout:
34
+ return "timeout";
35
+ case RequestError::Aborted:
36
+ return "aborted";
37
+ case RequestError::Network:
38
+ default:
39
+ return "network";
40
+ }
41
+ }
42
+
43
+ std::vector<std::pair<std::string, std::string>> parseHeaders(jsi::Runtime &rt, const jsi::Object &config) {
44
+ std::vector<std::pair<std::string, std::string>> headers;
45
+ auto headersArray = config.getProperty(rt, "headers").asObject(rt).asArray(rt);
46
+ const auto length = headersArray.size(rt);
47
+ headers.reserve(length);
48
+ for (size_t i = 0; i < length; i++) {
49
+ auto pair = headersArray.getValueAtIndex(rt, i).asObject(rt).asArray(rt);
50
+ headers.emplace_back(
51
+ pair.getValueAtIndex(rt, 0).asString(rt).utf8(rt), pair.getValueAtIndex(rt, 1).asString(rt).utf8(rt));
52
+ }
53
+ return headers;
54
+ }
55
+
56
+ std::optional<std::vector<uint8_t>> parseBody(jsi::Runtime &rt, const jsi::Object &config) {
57
+ const auto body = config.getProperty(rt, "body");
58
+ if (body.isString()) {
59
+ const auto utf8 = body.asString(rt).utf8(rt);
60
+ return std::vector<uint8_t>(utf8.begin(), utf8.end());
61
+ }
62
+ if (body.isObject()) {
63
+ const auto bodyObject = body.asObject(rt);
64
+ if (bodyObject.isArrayBuffer(rt)) {
65
+ auto arrayBuffer = bodyObject.getArrayBuffer(rt);
66
+ const auto *data = arrayBuffer.data(rt);
67
+ return std::vector<uint8_t>(data, data + arrayBuffer.size(rt));
68
+ }
69
+ }
70
+ return std::nullopt;
71
+ }
72
+
73
+ jsi::Value headersToJsi(jsi::Runtime &rt, const std::vector<std::pair<std::string, std::string>> &headers) {
74
+ auto result = jsi::Array(rt, headers.size());
75
+ for (size_t i = 0; i < headers.size(); i++) {
76
+ auto pair = jsi::Array(rt, 2);
77
+ pair.setValueAtIndex(rt, 0, jsi::String::createFromUtf8(rt, text::toValidUtf8(headers[i].first)));
78
+ pair.setValueAtIndex(rt, 1, jsi::String::createFromUtf8(rt, text::toValidUtf8(headers[i].second)));
79
+ result.setValueAtIndex(rt, i, std::move(pair));
80
+ }
81
+ return result;
82
+ }
83
+
84
+ void emitEvent(
85
+ const std::weak_ptr<WorkletRuntime> &weakRuntime,
86
+ const std::shared_ptr<SerializableRemoteFunction> &callback,
87
+ const char *type,
88
+ std::function<jsi::Value(jsi::Runtime &)> &&payloadFactory) {
89
+ const auto workletRuntime = weakRuntime.lock();
90
+ if (!workletRuntime) {
91
+ return;
92
+ }
93
+ workletRuntime->schedule([callback, type, payloadFactory = std::move(payloadFactory)](jsi::Runtime &rt) mutable {
94
+ callback->toJSValue(rt).asObject(rt).asFunction(rt).call(
95
+ rt, jsi::String::createFromAscii(rt, type), payloadFactory(rt));
96
+ });
97
+ }
98
+
99
+ } // namespace
100
+
101
+ class Networking::RequestListener final : public NetworkRequestListener {
102
+ public:
103
+ RequestListener(
104
+ const std::weak_ptr<Networking> &weakNetworking,
105
+ const std::shared_ptr<RequestRecord> &record,
106
+ const std::shared_ptr<SerializableRemoteFunction> &callback,
107
+ const uint64_t requestId)
108
+ : weakNetworking_(weakNetworking), record_(record), callback_(callback), requestId_(requestId) {}
109
+
110
+ ~RequestListener() override {
111
+ if (!callback_) {
112
+ return;
113
+ }
114
+ const auto workletRuntime = record_->weakRuntime.lock();
115
+ if (!workletRuntime) {
116
+ return;
117
+ }
118
+ workletRuntime->schedule([callback = std::move(callback_)](jsi::Runtime &) {});
119
+ }
120
+
121
+ void onResponse(ResponseInfo &&responseInfo) override {
122
+ emitEvent(record_->weakRuntime, callback_, "response", [responseInfo = std::move(responseInfo)](jsi::Runtime &rt) {
123
+ auto payload = jsi::Object(rt);
124
+ payload.setProperty(rt, "status", jsi::Value(responseInfo.status));
125
+ payload.setProperty(
126
+ rt, "statusText", jsi::String::createFromUtf8(rt, text::toValidUtf8(responseInfo.statusText)));
127
+ payload.setProperty(rt, "headers", headersToJsi(rt, responseInfo.headers));
128
+ payload.setProperty(rt, "url", jsi::String::createFromUtf8(rt, text::toValidUtf8(responseInfo.url)));
129
+ return jsi::Value(std::move(payload));
130
+ });
131
+ }
132
+
133
+ void onUploadProgress(const int64_t sent, const int64_t total) override {
134
+ emitProgress("uploadProgress", sent, total);
135
+ }
136
+
137
+ void onDownloadProgress(const int64_t received, const int64_t total) override {
138
+ emitProgress("downloadProgress", received, total);
139
+ }
140
+
141
+ void onComplete(std::vector<uint8_t> &&body) override {
142
+ if (settle()) {
143
+ return;
144
+ }
145
+ emitEvent(record_->weakRuntime, callback_, "done", [body = std::move(body)](jsi::Runtime &rt) mutable {
146
+ auto payload = jsi::Object(rt);
147
+ payload.setProperty(rt, "body", jsi::ArrayBuffer(rt, std::make_shared<VectorBuffer>(std::move(body))));
148
+ return jsi::Value(std::move(payload));
149
+ });
150
+ }
151
+
152
+ void onError(const RequestError error, std::string &&message) override {
153
+ if (settle()) {
154
+ return;
155
+ }
156
+ emitEvent(record_->weakRuntime, callback_, "done", [error, message = std::move(message)](jsi::Runtime &rt) {
157
+ auto payload = jsi::Object(rt);
158
+ payload.setProperty(rt, "error", jsi::String::createFromAscii(rt, errorName(error)));
159
+ payload.setProperty(rt, "message", jsi::String::createFromUtf8(rt, text::toValidUtf8(message)));
160
+ return jsi::Value(std::move(payload));
161
+ });
162
+ }
163
+
164
+ private:
165
+ void emitProgress(const char *type, const int64_t loaded, const int64_t total) {
166
+ if (record_->settled.load()) {
167
+ return;
168
+ }
169
+ emitEvent(record_->weakRuntime, callback_, type, [loaded, total](jsi::Runtime &rt) {
170
+ auto payload = jsi::Object(rt);
171
+ payload.setProperty(rt, "loaded", jsi::Value(static_cast<double>(loaded)));
172
+ payload.setProperty(rt, "total", jsi::Value(static_cast<double>(total)));
173
+ return jsi::Value(std::move(payload));
174
+ });
175
+ }
176
+
177
+ bool settle() {
178
+ if (record_->settled.exchange(true)) {
179
+ return true;
180
+ }
181
+ if (const auto networking = weakNetworking_.lock()) {
182
+ networking->unregisterRequest(requestId_);
183
+ }
184
+ return false;
185
+ }
186
+
187
+ const std::weak_ptr<Networking> weakNetworking_;
188
+ const std::shared_ptr<RequestRecord> record_;
189
+ std::shared_ptr<SerializableRemoteFunction> callback_;
190
+ const uint64_t requestId_;
191
+ };
192
+
193
+ Networking::Networking(const std::shared_ptr<NetworkingBackend> &backend) : backend_(backend) {}
194
+
195
+ uint64_t Networking::sendRequest(jsi::Runtime &rt, const jsi::Object &config, jsi::Function &&callback) {
196
+ const auto weakRuntime = WorkletRuntime::getWeakRuntimeFromJSIRuntime(rt);
197
+ const auto workletRuntime = weakRuntime.lock();
198
+ if (!workletRuntime) {
199
+ throw jsi::JSError(rt, "[Worklets] Cannot send a request from a dying Worklet Runtime.");
200
+ }
201
+
202
+ const auto serializableCallback = std::make_shared<SerializableRemoteFunction>(
203
+ rt, "workletsNetworkingCallback", std::move(callback), workletRuntime->getRuntimeId());
204
+
205
+ RequestConfig requestConfig{
206
+ .method = config.getProperty(rt, "method").asString(rt).utf8(rt),
207
+ .url = config.getProperty(rt, "url").asString(rt).utf8(rt),
208
+ .headers = parseHeaders(rt, config),
209
+ .body = parseBody(rt, config),
210
+ .timeoutMs = config.getProperty(rt, "timeoutMs").asNumber(),
211
+ .withCredentials = config.getProperty(rt, "withCredentials").asBool(),
212
+ };
213
+
214
+ const auto requestId = nextRequestId_.fetch_add(1);
215
+ const auto record = std::make_shared<RequestRecord>(weakRuntime, workletRuntime->getRuntimeId());
216
+ {
217
+ const std::lock_guard<std::mutex> lock(requestsMutex_);
218
+ requests_.emplace(requestId, record);
219
+ }
220
+
221
+ backend_->sendRequest(
222
+ requestId,
223
+ std::move(requestConfig),
224
+ std::make_shared<RequestListener>(weak_from_this(), record, serializableCallback, requestId));
225
+
226
+ return requestId;
227
+ }
228
+
229
+ void Networking::abortRequest(jsi::Runtime &rt, const uint64_t requestId) {
230
+ const auto workletRuntime = WorkletRuntime::getWeakRuntimeFromJSIRuntime(rt).lock();
231
+ if (!workletRuntime) {
232
+ return;
233
+ }
234
+ {
235
+ const std::lock_guard<std::mutex> lock(requestsMutex_);
236
+ const auto record = requests_.find(requestId);
237
+ if (record == requests_.end() || record->second->runtimeId != workletRuntime->getRuntimeId()) {
238
+ return;
239
+ }
240
+ }
241
+ backend_->abortRequest(requestId);
242
+ }
243
+
244
+ void Networking::abortAllForRuntime(const RuntimeData::RuntimeId runtimeId) {
245
+ std::vector<uint64_t> requestIds;
246
+ {
247
+ const std::lock_guard<std::mutex> lock(requestsMutex_);
248
+ for (const auto &[requestId, record] : requests_) {
249
+ if (record->runtimeId == runtimeId) {
250
+ requestIds.push_back(requestId);
251
+ }
252
+ }
253
+ }
254
+ for (const auto requestId : requestIds) {
255
+ backend_->abortRequest(requestId);
256
+ }
257
+ }
258
+
259
+ void Networking::abortAll() {
260
+ std::vector<uint64_t> requestIds;
261
+ {
262
+ const std::lock_guard<std::mutex> lock(requestsMutex_);
263
+ requestIds.reserve(requests_.size());
264
+ for (const auto &[requestId, record] : requests_) {
265
+ requestIds.push_back(requestId);
266
+ }
267
+ }
268
+ for (const auto requestId : requestIds) {
269
+ backend_->abortRequest(requestId);
270
+ }
271
+ }
272
+
273
+ std::optional<std::string>
274
+ Networking::decodeTextWithPlatform(const uint8_t *data, const size_t size, const std::string &label) {
275
+ return backend_->decodeText(data, size, label);
276
+ }
277
+
278
+ void Networking::unregisterRequest(const uint64_t requestId) {
279
+ const std::lock_guard<std::mutex> lock(requestsMutex_);
280
+ requests_.erase(requestId);
281
+ }
282
+
283
+ } // namespace worklets
@@ -0,0 +1,51 @@
1
+ #pragma once
2
+
3
+ #include <jsi/jsi.h>
4
+ #include <worklets/Networking/NetworkingBackend.h>
5
+ #include <worklets/WorkletRuntime/RuntimeData.h>
6
+
7
+ #include <atomic>
8
+ #include <cstdint>
9
+ #include <memory>
10
+ #include <mutex>
11
+ #include <optional>
12
+ #include <string>
13
+ #include <unordered_map>
14
+
15
+ using namespace facebook;
16
+
17
+ namespace worklets {
18
+
19
+ class WorkletRuntime;
20
+
21
+ class Networking : public std::enable_shared_from_this<Networking> {
22
+ public:
23
+ explicit Networking(const std::shared_ptr<NetworkingBackend> &backend);
24
+
25
+ uint64_t sendRequest(jsi::Runtime &rt, const jsi::Object &config, jsi::Function &&callback);
26
+ void abortRequest(jsi::Runtime &rt, uint64_t requestId);
27
+ void abortAllForRuntime(RuntimeData::RuntimeId runtimeId);
28
+ void abortAll();
29
+ std::optional<std::string> decodeTextWithPlatform(const uint8_t *data, size_t size, const std::string &label);
30
+
31
+ private:
32
+ struct RequestRecord {
33
+ RequestRecord(std::weak_ptr<WorkletRuntime> weakRuntime, RuntimeData::RuntimeId runtimeId)
34
+ : weakRuntime(std::move(weakRuntime)), runtimeId(runtimeId) {}
35
+
36
+ const std::weak_ptr<WorkletRuntime> weakRuntime;
37
+ const RuntimeData::RuntimeId runtimeId;
38
+ std::atomic<bool> settled{false};
39
+ };
40
+
41
+ class RequestListener;
42
+
43
+ void unregisterRequest(uint64_t requestId);
44
+
45
+ const std::shared_ptr<NetworkingBackend> backend_;
46
+ std::atomic<uint64_t> nextRequestId_{1};
47
+ std::mutex requestsMutex_;
48
+ std::unordered_map<uint64_t, std::shared_ptr<RequestRecord>> requests_;
49
+ };
50
+
51
+ } // namespace worklets
@@ -0,0 +1,47 @@
1
+ #pragma once
2
+
3
+ #include <worklets/Networking/NetworkingTypes.h>
4
+
5
+ #include <cstdint>
6
+ #include <memory>
7
+ #include <optional>
8
+ #include <string>
9
+ #include <vector>
10
+
11
+ namespace worklets {
12
+
13
+ /**
14
+ * Implemented by the networking core. Backends may invoke the methods from any
15
+ * thread. Exactly one of `onComplete` or `onError` must be invoked per request,
16
+ * as the last call for that request.
17
+ */
18
+ class NetworkRequestListener {
19
+ public:
20
+ virtual void onResponse(ResponseInfo &&responseInfo) = 0;
21
+ virtual void onUploadProgress(int64_t sent, int64_t total) = 0;
22
+ virtual void onDownloadProgress(int64_t received, int64_t total) = 0;
23
+ virtual void onComplete(std::vector<uint8_t> &&body) = 0;
24
+ virtual void onError(RequestError error, std::string &&message) = 0;
25
+ virtual ~NetworkRequestListener() = default;
26
+ };
27
+
28
+ class NetworkingBackend {
29
+ public:
30
+ virtual void
31
+ sendRequest(uint64_t requestId, RequestConfig &&config, const std::shared_ptr<NetworkRequestListener> &listener) = 0;
32
+ virtual void abortRequest(uint64_t requestId) = 0;
33
+
34
+ /**
35
+ * Decodes bytes with a legacy encoding the shared C++ decoders don't cover,
36
+ * using the platform's own tables. Returns `std::nullopt` when the label is
37
+ * unknown to the platform. The result must be valid UTF-8.
38
+ */
39
+ virtual std::optional<std::string>
40
+ decodeText(const uint8_t * /*data*/, size_t /*size*/, const std::string & /*label*/) {
41
+ return std::nullopt;
42
+ }
43
+
44
+ virtual ~NetworkingBackend() = default;
45
+ };
46
+
47
+ } // namespace worklets