@effectionx/worker 0.1.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.
Files changed (226) hide show
  1. package/README.md +149 -0
  2. package/esm/_dnt.shims.d.ts +2 -0
  3. package/esm/_dnt.shims.d.ts.map +1 -0
  4. package/esm/_dnt.shims.js +57 -0
  5. package/esm/deps/jsr.io/@std/assert/1.0.12/almost_equals.d.ts +29 -0
  6. package/esm/deps/jsr.io/@std/assert/1.0.12/almost_equals.d.ts.map +1 -0
  7. package/esm/deps/jsr.io/@std/assert/1.0.12/almost_equals.js +46 -0
  8. package/esm/deps/jsr.io/@std/assert/1.0.12/array_includes.d.ts +24 -0
  9. package/esm/deps/jsr.io/@std/assert/1.0.12/array_includes.d.ts.map +1 -0
  10. package/esm/deps/jsr.io/@std/assert/1.0.12/array_includes.js +46 -0
  11. package/esm/deps/jsr.io/@std/assert/1.0.12/assert.d.ts +16 -0
  12. package/esm/deps/jsr.io/@std/assert/1.0.12/assert.d.ts.map +1 -0
  13. package/esm/deps/jsr.io/@std/assert/1.0.12/assert.js +22 -0
  14. package/esm/deps/jsr.io/@std/assert/1.0.12/assertion_error.d.ts +26 -0
  15. package/esm/deps/jsr.io/@std/assert/1.0.12/assertion_error.d.ts.map +1 -0
  16. package/esm/deps/jsr.io/@std/assert/1.0.12/assertion_error.js +30 -0
  17. package/esm/deps/jsr.io/@std/assert/1.0.12/equal.d.ts +17 -0
  18. package/esm/deps/jsr.io/@std/assert/1.0.12/equal.d.ts.map +1 -0
  19. package/esm/deps/jsr.io/@std/assert/1.0.12/equal.js +183 -0
  20. package/esm/deps/jsr.io/@std/assert/1.0.12/equals.d.ts +35 -0
  21. package/esm/deps/jsr.io/@std/assert/1.0.12/equals.d.ts.map +1 -0
  22. package/esm/deps/jsr.io/@std/assert/1.0.12/equals.js +58 -0
  23. package/esm/deps/jsr.io/@std/assert/1.0.12/exists.d.ts +18 -0
  24. package/esm/deps/jsr.io/@std/assert/1.0.12/exists.d.ts.map +1 -0
  25. package/esm/deps/jsr.io/@std/assert/1.0.12/exists.js +27 -0
  26. package/esm/deps/jsr.io/@std/assert/1.0.12/fail.d.ts +15 -0
  27. package/esm/deps/jsr.io/@std/assert/1.0.12/fail.d.ts.map +1 -0
  28. package/esm/deps/jsr.io/@std/assert/1.0.12/fail.js +20 -0
  29. package/esm/deps/jsr.io/@std/assert/1.0.12/false.d.ts +18 -0
  30. package/esm/deps/jsr.io/@std/assert/1.0.12/false.d.ts.map +1 -0
  31. package/esm/deps/jsr.io/@std/assert/1.0.12/false.js +22 -0
  32. package/esm/deps/jsr.io/@std/assert/1.0.12/greater.d.ts +20 -0
  33. package/esm/deps/jsr.io/@std/assert/1.0.12/greater.d.ts.map +1 -0
  34. package/esm/deps/jsr.io/@std/assert/1.0.12/greater.js +29 -0
  35. package/esm/deps/jsr.io/@std/assert/1.0.12/greater_or_equal.d.ts +20 -0
  36. package/esm/deps/jsr.io/@std/assert/1.0.12/greater_or_equal.d.ts.map +1 -0
  37. package/esm/deps/jsr.io/@std/assert/1.0.12/greater_or_equal.js +29 -0
  38. package/esm/deps/jsr.io/@std/assert/1.0.12/instance_of.d.ts +23 -0
  39. package/esm/deps/jsr.io/@std/assert/1.0.12/instance_of.d.ts.map +1 -0
  40. package/esm/deps/jsr.io/@std/assert/1.0.12/instance_of.js +52 -0
  41. package/esm/deps/jsr.io/@std/assert/1.0.12/is_error.d.ts +25 -0
  42. package/esm/deps/jsr.io/@std/assert/1.0.12/is_error.d.ts.map +1 -0
  43. package/esm/deps/jsr.io/@std/assert/1.0.12/is_error.js +53 -0
  44. package/esm/deps/jsr.io/@std/assert/1.0.12/less.d.ts +19 -0
  45. package/esm/deps/jsr.io/@std/assert/1.0.12/less.d.ts.map +1 -0
  46. package/esm/deps/jsr.io/@std/assert/1.0.12/less.js +28 -0
  47. package/esm/deps/jsr.io/@std/assert/1.0.12/less_or_equal.d.ts +20 -0
  48. package/esm/deps/jsr.io/@std/assert/1.0.12/less_or_equal.d.ts.map +1 -0
  49. package/esm/deps/jsr.io/@std/assert/1.0.12/less_or_equal.js +29 -0
  50. package/esm/deps/jsr.io/@std/assert/1.0.12/match.d.ts +18 -0
  51. package/esm/deps/jsr.io/@std/assert/1.0.12/match.d.ts.map +1 -0
  52. package/esm/deps/jsr.io/@std/assert/1.0.12/match.js +26 -0
  53. package/esm/deps/jsr.io/@std/assert/1.0.12/mod.d.ts +44 -0
  54. package/esm/deps/jsr.io/@std/assert/1.0.12/mod.d.ts.map +1 -0
  55. package/esm/deps/jsr.io/@std/assert/1.0.12/mod.js +45 -0
  56. package/esm/deps/jsr.io/@std/assert/1.0.12/not_equals.d.ts +21 -0
  57. package/esm/deps/jsr.io/@std/assert/1.0.12/not_equals.d.ts.map +1 -0
  58. package/esm/deps/jsr.io/@std/assert/1.0.12/not_equals.js +33 -0
  59. package/esm/deps/jsr.io/@std/assert/1.0.12/not_instance_of.d.ts +20 -0
  60. package/esm/deps/jsr.io/@std/assert/1.0.12/not_instance_of.d.ts.map +1 -0
  61. package/esm/deps/jsr.io/@std/assert/1.0.12/not_instance_of.js +29 -0
  62. package/esm/deps/jsr.io/@std/assert/1.0.12/not_match.d.ts +18 -0
  63. package/esm/deps/jsr.io/@std/assert/1.0.12/not_match.d.ts.map +1 -0
  64. package/esm/deps/jsr.io/@std/assert/1.0.12/not_match.js +26 -0
  65. package/esm/deps/jsr.io/@std/assert/1.0.12/not_strict_equals.d.ts +23 -0
  66. package/esm/deps/jsr.io/@std/assert/1.0.12/not_strict_equals.d.ts.map +1 -0
  67. package/esm/deps/jsr.io/@std/assert/1.0.12/not_strict_equals.js +32 -0
  68. package/esm/deps/jsr.io/@std/assert/1.0.12/object_match.d.ts +29 -0
  69. package/esm/deps/jsr.io/@std/assert/1.0.12/object_match.d.ts.map +1 -0
  70. package/esm/deps/jsr.io/@std/assert/1.0.12/object_match.js +167 -0
  71. package/esm/deps/jsr.io/@std/assert/1.0.12/rejects.d.ts +42 -0
  72. package/esm/deps/jsr.io/@std/assert/1.0.12/rejects.d.ts.map +1 -0
  73. package/esm/deps/jsr.io/@std/assert/1.0.12/rejects.js +53 -0
  74. package/esm/deps/jsr.io/@std/assert/1.0.12/strict_equals.d.ts +24 -0
  75. package/esm/deps/jsr.io/@std/assert/1.0.12/strict_equals.d.ts.map +1 -0
  76. package/esm/deps/jsr.io/@std/assert/1.0.12/strict_equals.js +57 -0
  77. package/esm/deps/jsr.io/@std/assert/1.0.12/string_includes.d.ts +18 -0
  78. package/esm/deps/jsr.io/@std/assert/1.0.12/string_includes.d.ts.map +1 -0
  79. package/esm/deps/jsr.io/@std/assert/1.0.12/string_includes.js +26 -0
  80. package/esm/deps/jsr.io/@std/assert/1.0.12/throws.d.ts +45 -0
  81. package/esm/deps/jsr.io/@std/assert/1.0.12/throws.d.ts.map +1 -0
  82. package/esm/deps/jsr.io/@std/assert/1.0.12/throws.js +44 -0
  83. package/esm/deps/jsr.io/@std/assert/1.0.12/unimplemented.d.ts +15 -0
  84. package/esm/deps/jsr.io/@std/assert/1.0.12/unimplemented.d.ts.map +1 -0
  85. package/esm/deps/jsr.io/@std/assert/1.0.12/unimplemented.js +20 -0
  86. package/esm/deps/jsr.io/@std/assert/1.0.12/unreachable.d.ts +15 -0
  87. package/esm/deps/jsr.io/@std/assert/1.0.12/unreachable.d.ts.map +1 -0
  88. package/esm/deps/jsr.io/@std/assert/1.0.12/unreachable.js +20 -0
  89. package/esm/deps/jsr.io/@std/internal/1.0.6/build_message.d.ts +82 -0
  90. package/esm/deps/jsr.io/@std/internal/1.0.6/build_message.d.ts.map +1 -0
  91. package/esm/deps/jsr.io/@std/internal/1.0.6/build_message.js +110 -0
  92. package/esm/deps/jsr.io/@std/internal/1.0.6/diff.d.ts +140 -0
  93. package/esm/deps/jsr.io/@std/internal/1.0.6/diff.d.ts.map +1 -0
  94. package/esm/deps/jsr.io/@std/internal/1.0.6/diff.js +276 -0
  95. package/esm/deps/jsr.io/@std/internal/1.0.6/diff_str.d.ts +99 -0
  96. package/esm/deps/jsr.io/@std/internal/1.0.6/diff_str.d.ts.map +1 -0
  97. package/esm/deps/jsr.io/@std/internal/1.0.6/diff_str.js +182 -0
  98. package/esm/deps/jsr.io/@std/internal/1.0.6/format.d.ts +2 -0
  99. package/esm/deps/jsr.io/@std/internal/1.0.6/format.d.ts.map +1 -0
  100. package/esm/deps/jsr.io/@std/internal/1.0.6/format.js +37 -0
  101. package/esm/deps/jsr.io/@std/internal/1.0.6/styles.d.ts +159 -0
  102. package/esm/deps/jsr.io/@std/internal/1.0.6/styles.d.ts.map +1 -0
  103. package/esm/deps/jsr.io/@std/internal/1.0.6/styles.js +207 -0
  104. package/esm/deps/jsr.io/@std/internal/1.0.6/types.d.ts +16 -0
  105. package/esm/deps/jsr.io/@std/internal/1.0.6/types.d.ts.map +1 -0
  106. package/esm/deps/jsr.io/@std/internal/1.0.6/types.js +3 -0
  107. package/esm/mod.d.ts +2 -0
  108. package/esm/mod.d.ts.map +1 -0
  109. package/esm/mod.js +1 -0
  110. package/esm/package.json +3 -0
  111. package/esm/worker.d.ts +135 -0
  112. package/esm/worker.d.ts.map +1 -0
  113. package/esm/worker.js +237 -0
  114. package/package.json +29 -0
  115. package/script/_dnt.shims.d.ts +2 -0
  116. package/script/_dnt.shims.d.ts.map +1 -0
  117. package/script/_dnt.shims.js +60 -0
  118. package/script/deps/jsr.io/@std/assert/1.0.12/almost_equals.d.ts +29 -0
  119. package/script/deps/jsr.io/@std/assert/1.0.12/almost_equals.d.ts.map +1 -0
  120. package/script/deps/jsr.io/@std/assert/1.0.12/almost_equals.js +49 -0
  121. package/script/deps/jsr.io/@std/assert/1.0.12/array_includes.d.ts +24 -0
  122. package/script/deps/jsr.io/@std/assert/1.0.12/array_includes.d.ts.map +1 -0
  123. package/script/deps/jsr.io/@std/assert/1.0.12/array_includes.js +49 -0
  124. package/script/deps/jsr.io/@std/assert/1.0.12/assert.d.ts +16 -0
  125. package/script/deps/jsr.io/@std/assert/1.0.12/assert.d.ts.map +1 -0
  126. package/script/deps/jsr.io/@std/assert/1.0.12/assert.js +25 -0
  127. package/script/deps/jsr.io/@std/assert/1.0.12/assertion_error.d.ts +26 -0
  128. package/script/deps/jsr.io/@std/assert/1.0.12/assertion_error.d.ts.map +1 -0
  129. package/script/deps/jsr.io/@std/assert/1.0.12/assertion_error.js +34 -0
  130. package/script/deps/jsr.io/@std/assert/1.0.12/equal.d.ts +17 -0
  131. package/script/deps/jsr.io/@std/assert/1.0.12/equal.d.ts.map +1 -0
  132. package/script/deps/jsr.io/@std/assert/1.0.12/equal.js +209 -0
  133. package/script/deps/jsr.io/@std/assert/1.0.12/equals.d.ts +35 -0
  134. package/script/deps/jsr.io/@std/assert/1.0.12/equals.d.ts.map +1 -0
  135. package/script/deps/jsr.io/@std/assert/1.0.12/equals.js +61 -0
  136. package/script/deps/jsr.io/@std/assert/1.0.12/exists.d.ts +18 -0
  137. package/script/deps/jsr.io/@std/assert/1.0.12/exists.d.ts.map +1 -0
  138. package/script/deps/jsr.io/@std/assert/1.0.12/exists.js +30 -0
  139. package/script/deps/jsr.io/@std/assert/1.0.12/fail.d.ts +15 -0
  140. package/script/deps/jsr.io/@std/assert/1.0.12/fail.d.ts.map +1 -0
  141. package/script/deps/jsr.io/@std/assert/1.0.12/fail.js +23 -0
  142. package/script/deps/jsr.io/@std/assert/1.0.12/false.d.ts +18 -0
  143. package/script/deps/jsr.io/@std/assert/1.0.12/false.d.ts.map +1 -0
  144. package/script/deps/jsr.io/@std/assert/1.0.12/false.js +25 -0
  145. package/script/deps/jsr.io/@std/assert/1.0.12/greater.d.ts +20 -0
  146. package/script/deps/jsr.io/@std/assert/1.0.12/greater.d.ts.map +1 -0
  147. package/script/deps/jsr.io/@std/assert/1.0.12/greater.js +32 -0
  148. package/script/deps/jsr.io/@std/assert/1.0.12/greater_or_equal.d.ts +20 -0
  149. package/script/deps/jsr.io/@std/assert/1.0.12/greater_or_equal.d.ts.map +1 -0
  150. package/script/deps/jsr.io/@std/assert/1.0.12/greater_or_equal.js +32 -0
  151. package/script/deps/jsr.io/@std/assert/1.0.12/instance_of.d.ts +23 -0
  152. package/script/deps/jsr.io/@std/assert/1.0.12/instance_of.d.ts.map +1 -0
  153. package/script/deps/jsr.io/@std/assert/1.0.12/instance_of.js +55 -0
  154. package/script/deps/jsr.io/@std/assert/1.0.12/is_error.d.ts +25 -0
  155. package/script/deps/jsr.io/@std/assert/1.0.12/is_error.d.ts.map +1 -0
  156. package/script/deps/jsr.io/@std/assert/1.0.12/is_error.js +56 -0
  157. package/script/deps/jsr.io/@std/assert/1.0.12/less.d.ts +19 -0
  158. package/script/deps/jsr.io/@std/assert/1.0.12/less.d.ts.map +1 -0
  159. package/script/deps/jsr.io/@std/assert/1.0.12/less.js +31 -0
  160. package/script/deps/jsr.io/@std/assert/1.0.12/less_or_equal.d.ts +20 -0
  161. package/script/deps/jsr.io/@std/assert/1.0.12/less_or_equal.d.ts.map +1 -0
  162. package/script/deps/jsr.io/@std/assert/1.0.12/less_or_equal.js +32 -0
  163. package/script/deps/jsr.io/@std/assert/1.0.12/match.d.ts +18 -0
  164. package/script/deps/jsr.io/@std/assert/1.0.12/match.d.ts.map +1 -0
  165. package/script/deps/jsr.io/@std/assert/1.0.12/match.js +29 -0
  166. package/script/deps/jsr.io/@std/assert/1.0.12/mod.d.ts +44 -0
  167. package/script/deps/jsr.io/@std/assert/1.0.12/mod.d.ts.map +1 -0
  168. package/script/deps/jsr.io/@std/assert/1.0.12/mod.js +61 -0
  169. package/script/deps/jsr.io/@std/assert/1.0.12/not_equals.d.ts +21 -0
  170. package/script/deps/jsr.io/@std/assert/1.0.12/not_equals.d.ts.map +1 -0
  171. package/script/deps/jsr.io/@std/assert/1.0.12/not_equals.js +36 -0
  172. package/script/deps/jsr.io/@std/assert/1.0.12/not_instance_of.d.ts +20 -0
  173. package/script/deps/jsr.io/@std/assert/1.0.12/not_instance_of.d.ts.map +1 -0
  174. package/script/deps/jsr.io/@std/assert/1.0.12/not_instance_of.js +32 -0
  175. package/script/deps/jsr.io/@std/assert/1.0.12/not_match.d.ts +18 -0
  176. package/script/deps/jsr.io/@std/assert/1.0.12/not_match.d.ts.map +1 -0
  177. package/script/deps/jsr.io/@std/assert/1.0.12/not_match.js +29 -0
  178. package/script/deps/jsr.io/@std/assert/1.0.12/not_strict_equals.d.ts +23 -0
  179. package/script/deps/jsr.io/@std/assert/1.0.12/not_strict_equals.d.ts.map +1 -0
  180. package/script/deps/jsr.io/@std/assert/1.0.12/not_strict_equals.js +35 -0
  181. package/script/deps/jsr.io/@std/assert/1.0.12/object_match.d.ts +29 -0
  182. package/script/deps/jsr.io/@std/assert/1.0.12/object_match.d.ts.map +1 -0
  183. package/script/deps/jsr.io/@std/assert/1.0.12/object_match.js +170 -0
  184. package/script/deps/jsr.io/@std/assert/1.0.12/rejects.d.ts +42 -0
  185. package/script/deps/jsr.io/@std/assert/1.0.12/rejects.d.ts.map +1 -0
  186. package/script/deps/jsr.io/@std/assert/1.0.12/rejects.js +56 -0
  187. package/script/deps/jsr.io/@std/assert/1.0.12/strict_equals.d.ts +24 -0
  188. package/script/deps/jsr.io/@std/assert/1.0.12/strict_equals.d.ts.map +1 -0
  189. package/script/deps/jsr.io/@std/assert/1.0.12/strict_equals.js +60 -0
  190. package/script/deps/jsr.io/@std/assert/1.0.12/string_includes.d.ts +18 -0
  191. package/script/deps/jsr.io/@std/assert/1.0.12/string_includes.d.ts.map +1 -0
  192. package/script/deps/jsr.io/@std/assert/1.0.12/string_includes.js +29 -0
  193. package/script/deps/jsr.io/@std/assert/1.0.12/throws.d.ts +45 -0
  194. package/script/deps/jsr.io/@std/assert/1.0.12/throws.d.ts.map +1 -0
  195. package/script/deps/jsr.io/@std/assert/1.0.12/throws.js +47 -0
  196. package/script/deps/jsr.io/@std/assert/1.0.12/unimplemented.d.ts +15 -0
  197. package/script/deps/jsr.io/@std/assert/1.0.12/unimplemented.d.ts.map +1 -0
  198. package/script/deps/jsr.io/@std/assert/1.0.12/unimplemented.js +23 -0
  199. package/script/deps/jsr.io/@std/assert/1.0.12/unreachable.d.ts +15 -0
  200. package/script/deps/jsr.io/@std/assert/1.0.12/unreachable.d.ts.map +1 -0
  201. package/script/deps/jsr.io/@std/assert/1.0.12/unreachable.js +23 -0
  202. package/script/deps/jsr.io/@std/internal/1.0.6/build_message.d.ts +82 -0
  203. package/script/deps/jsr.io/@std/internal/1.0.6/build_message.d.ts.map +1 -0
  204. package/script/deps/jsr.io/@std/internal/1.0.6/build_message.js +115 -0
  205. package/script/deps/jsr.io/@std/internal/1.0.6/diff.d.ts +140 -0
  206. package/script/deps/jsr.io/@std/internal/1.0.6/diff.d.ts.map +1 -0
  207. package/script/deps/jsr.io/@std/internal/1.0.6/diff.js +283 -0
  208. package/script/deps/jsr.io/@std/internal/1.0.6/diff_str.d.ts +99 -0
  209. package/script/deps/jsr.io/@std/internal/1.0.6/diff_str.d.ts.map +1 -0
  210. package/script/deps/jsr.io/@std/internal/1.0.6/diff_str.js +188 -0
  211. package/script/deps/jsr.io/@std/internal/1.0.6/format.d.ts +2 -0
  212. package/script/deps/jsr.io/@std/internal/1.0.6/format.d.ts.map +1 -0
  213. package/script/deps/jsr.io/@std/internal/1.0.6/format.js +63 -0
  214. package/script/deps/jsr.io/@std/internal/1.0.6/styles.d.ts +159 -0
  215. package/script/deps/jsr.io/@std/internal/1.0.6/styles.d.ts.map +1 -0
  216. package/script/deps/jsr.io/@std/internal/1.0.6/styles.js +242 -0
  217. package/script/deps/jsr.io/@std/internal/1.0.6/types.d.ts +16 -0
  218. package/script/deps/jsr.io/@std/internal/1.0.6/types.d.ts.map +1 -0
  219. package/script/deps/jsr.io/@std/internal/1.0.6/types.js +4 -0
  220. package/script/mod.d.ts +2 -0
  221. package/script/mod.d.ts.map +1 -0
  222. package/script/mod.js +17 -0
  223. package/script/package.json +3 -0
  224. package/script/worker.d.ts +135 -0
  225. package/script/worker.d.ts.map +1 -0
  226. package/script/worker.js +241 -0
package/esm/worker.js ADDED
@@ -0,0 +1,237 @@
1
+ import { assert } from "./deps/jsr.io/@std/assert/1.0.12/mod.js";
2
+ import { createSignal, each, Err, main, Ok, on, once, resource, scoped, spawn, withResolvers, } from "effection";
3
+ /**
4
+ * Entrypoint used in the worker that estaliblishes communication
5
+ * with the main thread. It can be used to return a value,
6
+ * respond to messages or both.
7
+ *
8
+ * @example Returning a value
9
+ * ```ts
10
+ * import { workerMain } from "../worker.ts";
11
+ *
12
+ * await workerMain(function* ({ data }) {
13
+ * return data;
14
+ * });
15
+ * ```
16
+ *
17
+ * @example Responding to messages
18
+ * ```ts
19
+ * import { workerMain } from "../worker.ts";
20
+ *
21
+ * await workerMain(function* ({ messages }) {
22
+ * yield* messages.forEach(function* (message) {
23
+ * return message;
24
+ * });
25
+ * });
26
+ * ```
27
+ *
28
+ * @example Responding to messages and return a value
29
+ * ```ts
30
+ * import { workerMain } from "../worker.ts";
31
+ *
32
+ * await workerMain<number, number, number, number>(
33
+ * function* ({ messages, data: initial }) {
34
+ * let counter = initial;
35
+ *
36
+ * yield* messages.forEach(function* (message) {
37
+ * counter += message;
38
+ * return counter; // returns a value after each message
39
+ * });
40
+ *
41
+ * return counter; // returns the final value
42
+ * },
43
+ * );
44
+ * ```
45
+ *
46
+ * @template TSend - value main thread will send to the worker
47
+ * @template TRecv - value main thread will receive from the worker
48
+ * @template TReturn - worker operation return value
49
+ * @template TData - data passed from the main thread to the worker during initialization
50
+ * @param {(options: WorkerMainOptions<TSend, TRecv, TData>) => Operation<TReturn>} body
51
+ * @returns {Promise<void>}
52
+ */
53
+ export async function workerMain(body) {
54
+ await main(function* () {
55
+ let sent = createSignal();
56
+ let controls = yield* on(self, "message");
57
+ let outcome = withResolvers();
58
+ self.postMessage({ type: "open" });
59
+ let result = yield* scoped(function* () {
60
+ yield* spawn(function* () {
61
+ let next = yield* controls.next();
62
+ while (true) {
63
+ let control = next.value.data;
64
+ if (control.type === "init") {
65
+ yield* spawn(function* () {
66
+ try {
67
+ let value = yield* body({
68
+ data: control.data,
69
+ messages: {
70
+ *forEach(fn) {
71
+ for (let { value, response } of yield* each(sent)) {
72
+ yield* spawn(function* () {
73
+ try {
74
+ let result = yield* fn(value);
75
+ response.postMessage(Ok(result));
76
+ }
77
+ catch (error) {
78
+ response.postMessage(Err(error));
79
+ }
80
+ });
81
+ yield* each.next();
82
+ }
83
+ },
84
+ },
85
+ });
86
+ outcome.resolve(Ok(value));
87
+ }
88
+ catch (error) {
89
+ outcome.resolve(Err(error));
90
+ }
91
+ });
92
+ }
93
+ else if (control.type === "send") {
94
+ let { value, response } = control;
95
+ sent.send({ value, response });
96
+ }
97
+ else if (control.type === "close") {
98
+ outcome.resolve(Err(new Error(`worker terminated`)));
99
+ }
100
+ next = yield* controls.next();
101
+ }
102
+ });
103
+ return yield* outcome.operation;
104
+ });
105
+ self.postMessage({ type: "close", result });
106
+ });
107
+ }
108
+ /**
109
+ * Use on the main thread to create and exeecute a well behaved web worker.
110
+ *
111
+ * @example Compute a single value
112
+ * ```ts
113
+ * import { run } from "effection";
114
+ * import { useWorker } from "@effectionx/worker"
115
+ *
116
+ * await run(function*() {
117
+ * const worker = yield* useWorker("script.ts", { type: "module" });
118
+ *
119
+ * try {
120
+ * const result = yield* worker;
121
+ * } catch (e) {
122
+ * console.error(e);
123
+ * }
124
+ * });
125
+ * ```
126
+ *
127
+ * @example Compute multipe values
128
+ * ```ts
129
+ * import { run } from "effection";
130
+ * import { useWorker } from "@effectionx/worker"
131
+ *
132
+ * await run(function*() {
133
+ * const worker = yield* useWorker("script.ts", { type: "module" });
134
+ *
135
+ * try {
136
+ * const result1 = yield* worker.send("Tom");
137
+ * const result2 = yield* worker.send("Dick");
138
+ * const result2 = yield* worker.send("Harry");
139
+ *
140
+ * // get the last result
141
+ * const finalResult = yield* worker;
142
+ * } catch (e) {
143
+ * console.error(e);
144
+ * }
145
+ * });
146
+ * ```
147
+ *
148
+ * @param url URL or string of script
149
+ * @param options WorkerOptions
150
+ * @template TSend - value main thread will send to the worker
151
+ * @template TRecv - value main thread will receive from the worker
152
+ * @template TReturn - worker operation return value
153
+ * @template TData - data passed from the main thread to the worker during initialization
154
+ * @returns {Operation<WorkerResource<TSend, TRecv>>}
155
+ */
156
+ export function useWorker(url, options) {
157
+ return resource(function* (provide) {
158
+ let outcome = withResolvers();
159
+ let worker = new Worker(url, options);
160
+ let subscription = yield* on(worker, "message");
161
+ let onclose = (event) => {
162
+ if (event.data.type === "close") {
163
+ let { result } = event.data;
164
+ if (result.ok) {
165
+ outcome.resolve(result.value);
166
+ }
167
+ else {
168
+ outcome.reject(result.error);
169
+ }
170
+ }
171
+ };
172
+ worker.addEventListener("message", onclose);
173
+ let first = yield* subscription.next();
174
+ assert(first.value.data.type === "open", `expected first message to arrive from worker to be of type "open", but was: ${first.value.data.type}`);
175
+ yield* spawn(function* () {
176
+ let event = yield* once(worker, "error");
177
+ event.preventDefault();
178
+ throw event.error;
179
+ });
180
+ try {
181
+ worker.postMessage({
182
+ type: "init",
183
+ data: options?.data,
184
+ });
185
+ yield* provide({
186
+ *send(value) {
187
+ let channel = yield* useMessageChannel();
188
+ worker.postMessage({
189
+ type: "send",
190
+ value,
191
+ response: channel.port2,
192
+ }, [channel.port2]);
193
+ channel.port1.start();
194
+ let event = yield* once(channel.port1, "message");
195
+ let result = event.data;
196
+ if (result.ok) {
197
+ return result.value;
198
+ }
199
+ else {
200
+ throw result.error;
201
+ }
202
+ },
203
+ [Symbol.iterator]: outcome.operation[Symbol.iterator],
204
+ });
205
+ }
206
+ finally {
207
+ worker.postMessage({ type: "close" });
208
+ yield* settled(outcome.operation);
209
+ worker.removeEventListener("message", onclose);
210
+ }
211
+ });
212
+ }
213
+ function useMessageChannel() {
214
+ return resource(function* (provide) {
215
+ let channel = new MessageChannel();
216
+ try {
217
+ yield* provide(channel);
218
+ }
219
+ finally {
220
+ channel.port1.close();
221
+ channel.port2.close();
222
+ }
223
+ });
224
+ }
225
+ function settled(operation) {
226
+ return {
227
+ *[Symbol.iterator]() {
228
+ try {
229
+ yield* operation;
230
+ return Ok();
231
+ }
232
+ catch (error) {
233
+ return Err(error);
234
+ }
235
+ },
236
+ };
237
+ }
package/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "@effectionx/worker",
3
+ "version": "0.1.1",
4
+ "author": "engineering@frontside.com",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "git+https://github.com/thefrontside/effectionx.git"
8
+ },
9
+ "license": "MIT",
10
+ "bugs": {
11
+ "url": "https://github.com/thefrontside/effectionx/issues"
12
+ },
13
+ "main": "./script/mod.js",
14
+ "module": "./esm/mod.js",
15
+ "exports": {
16
+ ".": {
17
+ "import": "./esm/mod.js",
18
+ "require": "./script/mod.js"
19
+ }
20
+ },
21
+ "engines": {
22
+ "node": ">= 16"
23
+ },
24
+ "sideEffects": false,
25
+ "dependencies": {
26
+ "effection": "4.0.0-alpha.4"
27
+ },
28
+ "_generatedBy": "dnt@dev"
29
+ }
@@ -0,0 +1,2 @@
1
+ export declare const dntGlobalThis: Omit<typeof globalThis, never>;
2
+ //# sourceMappingURL=_dnt.shims.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_dnt.shims.d.ts","sourceRoot":"","sources":["../src/_dnt.shims.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,aAAa,gCAA2C,CAAC"}
@@ -0,0 +1,60 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.dntGlobalThis = void 0;
4
+ const dntGlobals = {};
5
+ exports.dntGlobalThis = createMergeProxy(globalThis, dntGlobals);
6
+ function createMergeProxy(baseObj, extObj) {
7
+ return new Proxy(baseObj, {
8
+ get(_target, prop, _receiver) {
9
+ if (prop in extObj) {
10
+ return extObj[prop];
11
+ }
12
+ else {
13
+ return baseObj[prop];
14
+ }
15
+ },
16
+ set(_target, prop, value) {
17
+ if (prop in extObj) {
18
+ delete extObj[prop];
19
+ }
20
+ baseObj[prop] = value;
21
+ return true;
22
+ },
23
+ deleteProperty(_target, prop) {
24
+ let success = false;
25
+ if (prop in extObj) {
26
+ delete extObj[prop];
27
+ success = true;
28
+ }
29
+ if (prop in baseObj) {
30
+ delete baseObj[prop];
31
+ success = true;
32
+ }
33
+ return success;
34
+ },
35
+ ownKeys(_target) {
36
+ const baseKeys = Reflect.ownKeys(baseObj);
37
+ const extKeys = Reflect.ownKeys(extObj);
38
+ const extKeysSet = new Set(extKeys);
39
+ return [...baseKeys.filter((k) => !extKeysSet.has(k)), ...extKeys];
40
+ },
41
+ defineProperty(_target, prop, desc) {
42
+ if (prop in extObj) {
43
+ delete extObj[prop];
44
+ }
45
+ Reflect.defineProperty(baseObj, prop, desc);
46
+ return true;
47
+ },
48
+ getOwnPropertyDescriptor(_target, prop) {
49
+ if (prop in extObj) {
50
+ return Reflect.getOwnPropertyDescriptor(extObj, prop);
51
+ }
52
+ else {
53
+ return Reflect.getOwnPropertyDescriptor(baseObj, prop);
54
+ }
55
+ },
56
+ has(_target, prop) {
57
+ return prop in extObj || prop in baseObj;
58
+ },
59
+ });
60
+ }
@@ -0,0 +1,29 @@
1
+ /**
2
+ * Make an assertion that `actual` and `expected` are almost equal numbers
3
+ * through a given tolerance. It can be used to take into account IEEE-754
4
+ * double-precision floating-point representation limitations. If the values
5
+ * are not almost equal then throw.
6
+ *
7
+ * The default tolerance is one hundred thousandth of a percent of the
8
+ * expected value.
9
+ *
10
+ * @example Usage
11
+ * ```ts ignore
12
+ * import { assertAlmostEquals } from "@std/assert";
13
+ *
14
+ * assertAlmostEquals(0.01, 0.02); // Throws
15
+ * assertAlmostEquals(1e-8, 1e-9); // Throws
16
+ * assertAlmostEquals(1.000000001e-8, 1.000000002e-8); // Doesn't throw
17
+ * assertAlmostEquals(0.01, 0.02, 0.1); // Doesn't throw
18
+ * assertAlmostEquals(0.1 + 0.2, 0.3, 1e-16); // Doesn't throw
19
+ * assertAlmostEquals(0.1 + 0.2, 0.3, 1e-17); // Throws
20
+ * ```
21
+ *
22
+ * @param actual The actual value to compare.
23
+ * @param expected The expected value to compare.
24
+ * @param tolerance The tolerance to consider the values almost equal. The
25
+ * default is one hundred thousandth of a percent of the expected value.
26
+ * @param msg The optional message to include in the error.
27
+ */
28
+ export declare function assertAlmostEquals(actual: number, expected: number, tolerance?: number, msg?: string): void;
29
+ //# sourceMappingURL=almost_equals.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"almost_equals.d.ts","sourceRoot":"","sources":["../../../../../../src/deps/jsr.io/@std/assert/1.0.12/almost_equals.ts"],"names":[],"mappings":"AAIA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,wBAAgB,kBAAkB,CAChC,MAAM,EAAE,MAAM,EACd,QAAQ,EAAE,MAAM,EAChB,SAAS,CAAC,EAAE,MAAM,EAClB,GAAG,CAAC,EAAE,MAAM,QAmBb"}
@@ -0,0 +1,49 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.assertAlmostEquals = assertAlmostEquals;
4
+ // Copyright 2018-2025 the Deno authors. MIT license.
5
+ // This module is browser compatible.
6
+ const assertion_error_js_1 = require("./assertion_error.js");
7
+ /**
8
+ * Make an assertion that `actual` and `expected` are almost equal numbers
9
+ * through a given tolerance. It can be used to take into account IEEE-754
10
+ * double-precision floating-point representation limitations. If the values
11
+ * are not almost equal then throw.
12
+ *
13
+ * The default tolerance is one hundred thousandth of a percent of the
14
+ * expected value.
15
+ *
16
+ * @example Usage
17
+ * ```ts ignore
18
+ * import { assertAlmostEquals } from "@std/assert";
19
+ *
20
+ * assertAlmostEquals(0.01, 0.02); // Throws
21
+ * assertAlmostEquals(1e-8, 1e-9); // Throws
22
+ * assertAlmostEquals(1.000000001e-8, 1.000000002e-8); // Doesn't throw
23
+ * assertAlmostEquals(0.01, 0.02, 0.1); // Doesn't throw
24
+ * assertAlmostEquals(0.1 + 0.2, 0.3, 1e-16); // Doesn't throw
25
+ * assertAlmostEquals(0.1 + 0.2, 0.3, 1e-17); // Throws
26
+ * ```
27
+ *
28
+ * @param actual The actual value to compare.
29
+ * @param expected The expected value to compare.
30
+ * @param tolerance The tolerance to consider the values almost equal. The
31
+ * default is one hundred thousandth of a percent of the expected value.
32
+ * @param msg The optional message to include in the error.
33
+ */
34
+ function assertAlmostEquals(actual, expected, tolerance, msg) {
35
+ if (Object.is(actual, expected)) {
36
+ return;
37
+ }
38
+ const delta = Math.abs(expected - actual);
39
+ if (tolerance === undefined) {
40
+ tolerance = isFinite(expected) ? Math.abs(expected * 1e-7) : 1e-7;
41
+ }
42
+ if (delta <= tolerance) {
43
+ return;
44
+ }
45
+ const msgSuffix = msg ? `: ${msg}` : ".";
46
+ const f = (n) => Number.isInteger(n) ? n : n.toExponential();
47
+ throw new assertion_error_js_1.AssertionError(`Expected actual: "${f(actual)}" to be close to "${f(expected)}": \
48
+ delta "${f(delta)}" is greater than "${f(tolerance)}"${msgSuffix}`);
49
+ }
@@ -0,0 +1,24 @@
1
+ /** An array-like object (`Array`, `Uint8Array`, `NodeList`, etc.) that is not a string */
2
+ export type ArrayLikeArg<T> = ArrayLike<T> & object;
3
+ /**
4
+ * Make an assertion that `actual` includes the `expected` values. If not then
5
+ * an error will be thrown.
6
+ *
7
+ * Type parameter can be specified to ensure values under comparison have the
8
+ * same type.
9
+ *
10
+ * @example Usage
11
+ * ```ts ignore
12
+ * import { assertArrayIncludes } from "@std/assert";
13
+ *
14
+ * assertArrayIncludes([1, 2], [2]); // Doesn't throw
15
+ * assertArrayIncludes([1, 2], [3]); // Throws
16
+ * ```
17
+ *
18
+ * @typeParam T The type of the elements in the array to compare.
19
+ * @param actual The array-like object to check for.
20
+ * @param expected The array-like object to check for.
21
+ * @param msg The optional message to display if the assertion fails.
22
+ */
23
+ export declare function assertArrayIncludes<T>(actual: ArrayLikeArg<T>, expected: ArrayLikeArg<T>, msg?: string): void;
24
+ //# sourceMappingURL=array_includes.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"array_includes.d.ts","sourceRoot":"","sources":["../../../../../../src/deps/jsr.io/@std/assert/1.0.12/array_includes.ts"],"names":[],"mappings":"AAMA,0FAA0F;AAC1F,MAAM,MAAM,YAAY,CAAC,CAAC,IAAI,SAAS,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC;AAEpD;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,mBAAmB,CAAC,CAAC,EACnC,MAAM,EAAE,YAAY,CAAC,CAAC,CAAC,EACvB,QAAQ,EAAE,YAAY,CAAC,CAAC,CAAC,EACzB,GAAG,CAAC,EAAE,MAAM,QAwBb"}
@@ -0,0 +1,49 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.assertArrayIncludes = assertArrayIncludes;
4
+ // Copyright 2018-2025 the Deno authors. MIT license.
5
+ // This module is browser compatible.
6
+ const equal_js_1 = require("./equal.js");
7
+ const format_js_1 = require("../../internal/1.0.6/format.js");
8
+ const assertion_error_js_1 = require("./assertion_error.js");
9
+ /**
10
+ * Make an assertion that `actual` includes the `expected` values. If not then
11
+ * an error will be thrown.
12
+ *
13
+ * Type parameter can be specified to ensure values under comparison have the
14
+ * same type.
15
+ *
16
+ * @example Usage
17
+ * ```ts ignore
18
+ * import { assertArrayIncludes } from "@std/assert";
19
+ *
20
+ * assertArrayIncludes([1, 2], [2]); // Doesn't throw
21
+ * assertArrayIncludes([1, 2], [3]); // Throws
22
+ * ```
23
+ *
24
+ * @typeParam T The type of the elements in the array to compare.
25
+ * @param actual The array-like object to check for.
26
+ * @param expected The array-like object to check for.
27
+ * @param msg The optional message to display if the assertion fails.
28
+ */
29
+ function assertArrayIncludes(actual, expected, msg) {
30
+ const missing = [];
31
+ for (let i = 0; i < expected.length; i++) {
32
+ let found = false;
33
+ for (let j = 0; j < actual.length; j++) {
34
+ if ((0, equal_js_1.equal)(expected[i], actual[j])) {
35
+ found = true;
36
+ break;
37
+ }
38
+ }
39
+ if (!found) {
40
+ missing.push(expected[i]);
41
+ }
42
+ }
43
+ if (missing.length === 0) {
44
+ return;
45
+ }
46
+ const msgSuffix = msg ? `: ${msg}` : ".";
47
+ msg = `Expected actual: "${(0, format_js_1.format)(actual)}" to include: "${(0, format_js_1.format)(expected)}"${msgSuffix}\nmissing: ${(0, format_js_1.format)(missing)}`;
48
+ throw new assertion_error_js_1.AssertionError(msg);
49
+ }
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Make an assertion, error will be thrown if `expr` does not have truthy value.
3
+ *
4
+ * @example Usage
5
+ * ```ts ignore
6
+ * import { assert } from "@std/assert";
7
+ *
8
+ * assert("hello".includes("ello")); // Doesn't throw
9
+ * assert("hello".includes("world")); // Throws
10
+ * ```
11
+ *
12
+ * @param expr The expression to test.
13
+ * @param msg The optional message to display if the assertion fails.
14
+ */
15
+ export declare function assert(expr: unknown, msg?: string): asserts expr;
16
+ //# sourceMappingURL=assert.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"assert.d.ts","sourceRoot":"","sources":["../../../../../../src/deps/jsr.io/@std/assert/1.0.12/assert.ts"],"names":[],"mappings":"AAIA;;;;;;;;;;;;;GAaG;AACH,wBAAgB,MAAM,CAAC,IAAI,EAAE,OAAO,EAAE,GAAG,SAAK,GAAG,OAAO,CAAC,IAAI,CAI5D"}
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.assert = assert;
4
+ // Copyright 2018-2025 the Deno authors. MIT license.
5
+ // This module is browser compatible.
6
+ const assertion_error_js_1 = require("./assertion_error.js");
7
+ /**
8
+ * Make an assertion, error will be thrown if `expr` does not have truthy value.
9
+ *
10
+ * @example Usage
11
+ * ```ts ignore
12
+ * import { assert } from "@std/assert";
13
+ *
14
+ * assert("hello".includes("ello")); // Doesn't throw
15
+ * assert("hello".includes("world")); // Throws
16
+ * ```
17
+ *
18
+ * @param expr The expression to test.
19
+ * @param msg The optional message to display if the assertion fails.
20
+ */
21
+ function assert(expr, msg = "") {
22
+ if (!expr) {
23
+ throw new assertion_error_js_1.AssertionError(msg);
24
+ }
25
+ }
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Error thrown when an assertion fails.
3
+ *
4
+ * @example Usage
5
+ * ```ts ignore
6
+ * import { AssertionError } from "@std/assert";
7
+ *
8
+ * try {
9
+ * throw new AssertionError("foo", { cause: "bar" });
10
+ * } catch (error) {
11
+ * if (error instanceof AssertionError) {
12
+ * error.message === "foo"; // true
13
+ * error.cause === "bar"; // true
14
+ * }
15
+ * }
16
+ * ```
17
+ */
18
+ export declare class AssertionError extends Error {
19
+ /** Constructs a new instance.
20
+ *
21
+ * @param message The error message.
22
+ * @param options Additional options. This argument is still unstable. It may change in the future release.
23
+ */
24
+ constructor(message: string, options?: ErrorOptions);
25
+ }
26
+ //# sourceMappingURL=assertion_error.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"assertion_error.d.ts","sourceRoot":"","sources":["../../../../../../src/deps/jsr.io/@std/assert/1.0.12/assertion_error.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;;;;;;GAgBG;AACH,qBAAa,cAAe,SAAQ,KAAK;IACvC;;;;OAIG;gBACS,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,YAAY;CAIpD"}
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ // Copyright 2018-2025 the Deno authors. MIT license.
3
+ // This module is browser compatible.
4
+ Object.defineProperty(exports, "__esModule", { value: true });
5
+ exports.AssertionError = void 0;
6
+ /**
7
+ * Error thrown when an assertion fails.
8
+ *
9
+ * @example Usage
10
+ * ```ts ignore
11
+ * import { AssertionError } from "@std/assert";
12
+ *
13
+ * try {
14
+ * throw new AssertionError("foo", { cause: "bar" });
15
+ * } catch (error) {
16
+ * if (error instanceof AssertionError) {
17
+ * error.message === "foo"; // true
18
+ * error.cause === "bar"; // true
19
+ * }
20
+ * }
21
+ * ```
22
+ */
23
+ class AssertionError extends Error {
24
+ /** Constructs a new instance.
25
+ *
26
+ * @param message The error message.
27
+ * @param options Additional options. This argument is still unstable. It may change in the future release.
28
+ */
29
+ constructor(message, options) {
30
+ super(message, options);
31
+ this.name = "AssertionError";
32
+ }
33
+ }
34
+ exports.AssertionError = AssertionError;
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Deep equality comparison used in assertions.
3
+ *
4
+ * @param a The actual value
5
+ * @param b The expected value
6
+ * @returns `true` if the values are deeply equal, `false` otherwise
7
+ *
8
+ * @example Usage
9
+ * ```ts
10
+ * import { equal } from "@std/assert/equal";
11
+ *
12
+ * equal({ foo: "bar" }, { foo: "bar" }); // Returns `true`
13
+ * equal({ foo: "bar" }, { foo: "baz" }); // Returns `false`
14
+ * ```
15
+ */
16
+ export declare function equal(a: unknown, b: unknown): boolean;
17
+ //# sourceMappingURL=equal.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"equal.d.ts","sourceRoot":"","sources":["../../../../../../src/deps/jsr.io/@std/assert/1.0.12/equal.ts"],"names":[],"mappings":"AA2FA;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,KAAK,CAAC,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,OAAO,GAAG,OAAO,CAuGrD"}