@typed/fx 1.18.4 → 1.20.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 (423) hide show
  1. package/AsyncData/package.json +6 -0
  2. package/Emitter/package.json +6 -0
  3. package/Form/package.json +6 -0
  4. package/FormEntry/package.json +6 -0
  5. package/Fx/package.json +6 -0
  6. package/Guard/package.json +6 -0
  7. package/Idle/package.json +6 -0
  8. package/Match/package.json +6 -0
  9. package/Pull/package.json +6 -0
  10. package/Push/package.json +6 -0
  11. package/RefArray/package.json +6 -0
  12. package/RefChunk/package.json +6 -0
  13. package/RefHashMap/package.json +6 -0
  14. package/RefHashSet/package.json +6 -0
  15. package/RefSubject/package.json +6 -0
  16. package/Sink/package.json +6 -0
  17. package/Stream/package.json +6 -0
  18. package/Subject/package.json +6 -0
  19. package/TypeId/package.json +6 -0
  20. package/Typeclass/package.json +6 -0
  21. package/Versioned/package.json +6 -0
  22. package/dist/cjs/AsyncData.js +177 -0
  23. package/dist/cjs/AsyncData.js.map +1 -0
  24. package/dist/cjs/Emitter.js +24 -21
  25. package/dist/cjs/Emitter.js.map +1 -1
  26. package/dist/cjs/Form.js +78 -21
  27. package/dist/cjs/Form.js.map +1 -1
  28. package/dist/cjs/FormEntry.js +75 -48
  29. package/dist/cjs/FormEntry.js.map +1 -1
  30. package/dist/cjs/Fx.js +517 -872
  31. package/dist/cjs/Fx.js.map +1 -1
  32. package/dist/cjs/Guard.js +21 -13
  33. package/dist/cjs/Guard.js.map +1 -1
  34. package/dist/cjs/Idle.js +49 -37
  35. package/dist/cjs/Idle.js.map +1 -1
  36. package/dist/cjs/Match.js +99 -61
  37. package/dist/cjs/Match.js.map +1 -1
  38. package/dist/cjs/Pull.js.map +1 -1
  39. package/dist/cjs/Push.js +168 -0
  40. package/dist/cjs/Push.js.map +1 -0
  41. package/dist/cjs/RefArray.js +32 -30
  42. package/dist/cjs/RefArray.js.map +1 -1
  43. package/dist/cjs/RefChunk.js +26 -24
  44. package/dist/cjs/RefChunk.js.map +1 -1
  45. package/dist/cjs/RefHashMap.js +20 -20
  46. package/dist/cjs/RefHashMap.js.map +1 -1
  47. package/dist/cjs/RefHashSet.js +11 -8
  48. package/dist/cjs/RefHashSet.js.map +1 -1
  49. package/dist/cjs/RefSubject.js +899 -158
  50. package/dist/cjs/RefSubject.js.map +1 -1
  51. package/dist/cjs/Sink.js +588 -62
  52. package/dist/cjs/Sink.js.map +1 -1
  53. package/dist/cjs/Stream.js +15 -10
  54. package/dist/cjs/Stream.js.map +1 -1
  55. package/dist/cjs/Subject.js +153 -55
  56. package/dist/cjs/Subject.js.map +1 -1
  57. package/dist/cjs/TypeId.js +10 -5
  58. package/dist/cjs/TypeId.js.map +1 -1
  59. package/dist/cjs/Typeclass.js +28 -31
  60. package/dist/cjs/Typeclass.js.map +1 -1
  61. package/dist/cjs/Versioned.js +118 -57
  62. package/dist/cjs/Versioned.js.map +1 -1
  63. package/dist/cjs/index.js +44 -8
  64. package/dist/cjs/index.js.map +1 -1
  65. package/dist/cjs/internal/{deferred-ref.js → DeferredRef.js} +45 -13
  66. package/dist/cjs/internal/DeferredRef.js.map +1 -0
  67. package/dist/cjs/internal/UnionToTuple.js +6 -0
  68. package/dist/cjs/internal/UnionToTuple.js.map +1 -0
  69. package/dist/cjs/internal/core.js +1590 -779
  70. package/dist/cjs/internal/core.js.map +1 -1
  71. package/dist/cjs/internal/diff.js +114 -0
  72. package/dist/cjs/internal/diff.js.map +1 -0
  73. package/dist/cjs/internal/effect-loop-operator.js +288 -0
  74. package/dist/cjs/internal/effect-loop-operator.js.map +1 -0
  75. package/dist/cjs/internal/effect-operator.js +46 -37
  76. package/dist/cjs/internal/effect-operator.js.map +1 -1
  77. package/dist/cjs/internal/effect-producer.js +74 -0
  78. package/dist/cjs/internal/effect-producer.js.map +1 -0
  79. package/dist/cjs/internal/helpers.js +158 -128
  80. package/dist/cjs/internal/helpers.js.map +1 -1
  81. package/dist/cjs/internal/keyed.js +136 -173
  82. package/dist/cjs/internal/keyed.js.map +1 -1
  83. package/dist/cjs/internal/loop-operator.js +213 -0
  84. package/dist/cjs/internal/loop-operator.js.map +1 -0
  85. package/dist/cjs/internal/operator.js +79 -0
  86. package/dist/cjs/internal/operator.js.map +1 -0
  87. package/dist/cjs/internal/protos.js +19 -81
  88. package/dist/cjs/internal/protos.js.map +1 -1
  89. package/dist/cjs/internal/provide.js +47 -25
  90. package/dist/cjs/internal/provide.js.map +1 -1
  91. package/dist/cjs/internal/requestIdleCallback.js +5 -5
  92. package/dist/cjs/internal/requestIdleCallback.js.map +1 -1
  93. package/dist/cjs/internal/share.js +12 -15
  94. package/dist/cjs/internal/share.js.map +1 -1
  95. package/dist/cjs/internal/sync-operator.js +50 -22
  96. package/dist/cjs/internal/sync-operator.js.map +1 -1
  97. package/dist/cjs/internal/sync-producer.js +114 -0
  98. package/dist/cjs/internal/sync-producer.js.map +1 -0
  99. package/dist/cjs/internal/withKey.js +45 -51
  100. package/dist/cjs/internal/withKey.js.map +1 -1
  101. package/dist/dts/AsyncData.d.ts +185 -0
  102. package/dist/dts/AsyncData.d.ts.map +1 -0
  103. package/dist/dts/Emitter.d.ts +13 -20
  104. package/dist/dts/Emitter.d.ts.map +1 -1
  105. package/dist/dts/Form.d.ts +45 -28
  106. package/dist/dts/Form.d.ts.map +1 -1
  107. package/dist/dts/FormEntry.d.ts +25 -13
  108. package/dist/dts/FormEntry.d.ts.map +1 -1
  109. package/dist/dts/Fx.d.ts +983 -1387
  110. package/dist/dts/Fx.d.ts.map +1 -1
  111. package/dist/dts/Guard.d.ts +8 -1
  112. package/dist/dts/Guard.d.ts.map +1 -1
  113. package/dist/dts/Idle.d.ts +3 -3
  114. package/dist/dts/Idle.d.ts.map +1 -1
  115. package/dist/dts/Match.d.ts +8 -8
  116. package/dist/dts/Match.d.ts.map +1 -1
  117. package/dist/dts/Pull.d.ts +5 -5
  118. package/dist/dts/Pull.d.ts.map +1 -1
  119. package/dist/dts/Push.d.ts +170 -0
  120. package/dist/dts/Push.d.ts.map +1 -0
  121. package/dist/dts/RefArray.d.ts +21 -23
  122. package/dist/dts/RefArray.d.ts.map +1 -1
  123. package/dist/dts/RefChunk.d.ts +17 -19
  124. package/dist/dts/RefChunk.d.ts.map +1 -1
  125. package/dist/dts/RefHashMap.d.ts +19 -20
  126. package/dist/dts/RefHashMap.d.ts.map +1 -1
  127. package/dist/dts/RefHashSet.d.ts +12 -16
  128. package/dist/dts/RefHashSet.d.ts.map +1 -1
  129. package/dist/dts/RefSubject.d.ts +396 -187
  130. package/dist/dts/RefSubject.d.ts.map +1 -1
  131. package/dist/dts/Sink.d.ts +193 -93
  132. package/dist/dts/Sink.d.ts.map +1 -1
  133. package/dist/dts/Stream.d.ts +7 -1
  134. package/dist/dts/Stream.d.ts.map +1 -1
  135. package/dist/dts/Subject.d.ts +27 -36
  136. package/dist/dts/Subject.d.ts.map +1 -1
  137. package/dist/dts/TypeId.d.ts +10 -0
  138. package/dist/dts/TypeId.d.ts.map +1 -1
  139. package/dist/dts/Typeclass.d.ts +2 -2
  140. package/dist/dts/Typeclass.d.ts.map +1 -1
  141. package/dist/dts/Versioned.d.ts +50 -78
  142. package/dist/dts/Versioned.d.ts.map +1 -1
  143. package/dist/dts/index.d.ts +66 -6
  144. package/dist/dts/index.d.ts.map +1 -1
  145. package/dist/dts/internal/DeferredRef.d.ts +20 -0
  146. package/dist/dts/internal/DeferredRef.d.ts.map +1 -0
  147. package/dist/dts/internal/UnionToTuple.d.ts +6 -0
  148. package/dist/dts/internal/UnionToTuple.d.ts.map +1 -0
  149. package/dist/dts/internal/core.d.ts +218 -450
  150. package/dist/dts/internal/core.d.ts.map +1 -1
  151. package/dist/dts/internal/diff.d.ts +38 -0
  152. package/dist/dts/internal/diff.d.ts.map +1 -0
  153. package/dist/dts/internal/effect-loop-operator.d.ts +34 -0
  154. package/dist/dts/internal/effect-loop-operator.d.ts.map +1 -0
  155. package/dist/dts/internal/effect-operator.d.ts +8 -5
  156. package/dist/dts/internal/effect-operator.d.ts.map +1 -1
  157. package/dist/dts/internal/effect-producer.d.ts +25 -0
  158. package/dist/dts/internal/effect-producer.d.ts.map +1 -0
  159. package/dist/dts/internal/helpers.d.ts +33 -24
  160. package/dist/dts/internal/helpers.d.ts.map +1 -1
  161. package/dist/dts/internal/keyed.d.ts +3 -3
  162. package/dist/dts/internal/keyed.d.ts.map +1 -1
  163. package/dist/dts/internal/loop-operator.d.ts +40 -0
  164. package/dist/dts/internal/loop-operator.d.ts.map +1 -0
  165. package/dist/dts/internal/operator.d.ts +16 -0
  166. package/dist/dts/internal/operator.d.ts.map +1 -0
  167. package/dist/dts/internal/protos.d.ts +16 -43
  168. package/dist/dts/internal/protos.d.ts.map +1 -1
  169. package/dist/dts/internal/provide.d.ts +7 -1
  170. package/dist/dts/internal/provide.d.ts.map +1 -1
  171. package/dist/dts/internal/share.d.ts +12 -9
  172. package/dist/dts/internal/share.d.ts.map +1 -1
  173. package/dist/dts/internal/sync-operator.d.ts +7 -3
  174. package/dist/dts/internal/sync-operator.d.ts.map +1 -1
  175. package/dist/dts/internal/sync-producer.d.ts +36 -0
  176. package/dist/dts/internal/sync-producer.d.ts.map +1 -0
  177. package/dist/dts/internal/withKey.d.ts +3 -3
  178. package/dist/dts/internal/withKey.d.ts.map +1 -1
  179. package/dist/esm/AsyncData.js +151 -0
  180. package/dist/esm/AsyncData.js.map +1 -0
  181. package/dist/esm/Emitter.js +24 -21
  182. package/dist/esm/Emitter.js.map +1 -1
  183. package/dist/esm/Form.js +69 -14
  184. package/dist/esm/Form.js.map +1 -1
  185. package/dist/esm/FormEntry.js +77 -52
  186. package/dist/esm/FormEntry.js.map +1 -1
  187. package/dist/esm/Fx.js +509 -874
  188. package/dist/esm/Fx.js.map +1 -1
  189. package/dist/esm/Guard.js +5 -1
  190. package/dist/esm/Guard.js.map +1 -1
  191. package/dist/esm/Idle.js +49 -39
  192. package/dist/esm/Idle.js.map +1 -1
  193. package/dist/esm/Match.js +103 -63
  194. package/dist/esm/Match.js.map +1 -1
  195. package/dist/esm/Pull.js.map +1 -1
  196. package/dist/esm/Push.js +159 -0
  197. package/dist/esm/Push.js.map +1 -0
  198. package/dist/esm/RefArray.js +32 -30
  199. package/dist/esm/RefArray.js.map +1 -1
  200. package/dist/esm/RefChunk.js +26 -24
  201. package/dist/esm/RefChunk.js.map +1 -1
  202. package/dist/esm/RefHashMap.js +20 -20
  203. package/dist/esm/RefHashMap.js.map +1 -1
  204. package/dist/esm/RefHashSet.js +11 -8
  205. package/dist/esm/RefHashSet.js.map +1 -1
  206. package/dist/esm/RefSubject.js +873 -150
  207. package/dist/esm/RefSubject.js.map +1 -1
  208. package/dist/esm/Sink.js +608 -57
  209. package/dist/esm/Sink.js.map +1 -1
  210. package/dist/esm/Stream.js +13 -9
  211. package/dist/esm/Stream.js.map +1 -1
  212. package/dist/esm/Subject.js +151 -56
  213. package/dist/esm/Subject.js.map +1 -1
  214. package/dist/esm/TypeId.js +9 -4
  215. package/dist/esm/TypeId.js.map +1 -1
  216. package/dist/esm/Typeclass.js +29 -29
  217. package/dist/esm/Typeclass.js.map +1 -1
  218. package/dist/esm/Versioned.js +110 -53
  219. package/dist/esm/Versioned.js.map +1 -1
  220. package/dist/esm/index.js +66 -6
  221. package/dist/esm/index.js.map +1 -1
  222. package/dist/esm/internal/DeferredRef.js +54 -0
  223. package/dist/esm/internal/DeferredRef.js.map +1 -0
  224. package/dist/esm/internal/UnionToTuple.js +2 -0
  225. package/dist/esm/internal/UnionToTuple.js.map +1 -0
  226. package/dist/esm/internal/core.js +1439 -719
  227. package/dist/esm/internal/core.js.map +1 -1
  228. package/dist/esm/internal/diff.js +82 -0
  229. package/dist/esm/internal/diff.js.map +1 -0
  230. package/dist/esm/internal/effect-loop-operator.js +269 -0
  231. package/dist/esm/internal/effect-loop-operator.js.map +1 -0
  232. package/dist/esm/internal/effect-operator.js +44 -37
  233. package/dist/esm/internal/effect-operator.js.map +1 -1
  234. package/dist/esm/internal/effect-producer.js +47 -0
  235. package/dist/esm/internal/effect-producer.js.map +1 -0
  236. package/dist/esm/internal/helpers.js +157 -129
  237. package/dist/esm/internal/helpers.js.map +1 -1
  238. package/dist/esm/internal/keyed.js +128 -139
  239. package/dist/esm/internal/keyed.js.map +1 -1
  240. package/dist/esm/internal/loop-operator.js +186 -0
  241. package/dist/esm/internal/loop-operator.js.map +1 -0
  242. package/dist/esm/internal/operator.js +68 -0
  243. package/dist/esm/internal/operator.js.map +1 -0
  244. package/dist/esm/internal/protos.js +16 -75
  245. package/dist/esm/internal/protos.js.map +1 -1
  246. package/dist/esm/internal/provide.js +33 -25
  247. package/dist/esm/internal/provide.js.map +1 -1
  248. package/dist/esm/internal/requestIdleCallback.js +5 -5
  249. package/dist/esm/internal/requestIdleCallback.js.map +1 -1
  250. package/dist/esm/internal/share.js +12 -12
  251. package/dist/esm/internal/share.js.map +1 -1
  252. package/dist/esm/internal/sync-operator.js +49 -21
  253. package/dist/esm/internal/sync-operator.js.map +1 -1
  254. package/dist/esm/internal/sync-producer.js +88 -0
  255. package/dist/esm/internal/sync-producer.js.map +1 -0
  256. package/dist/esm/internal/withKey.js +46 -47
  257. package/dist/esm/internal/withKey.js.map +1 -1
  258. package/package.json +22 -62
  259. package/src/AsyncData.ts +447 -0
  260. package/src/Emitter.ts +47 -38
  261. package/src/Form.ts +185 -65
  262. package/src/FormEntry.ts +142 -109
  263. package/src/Fx.ts +1685 -1851
  264. package/src/Guard.ts +12 -1
  265. package/src/Idle.ts +76 -62
  266. package/src/Match.ts +149 -93
  267. package/src/Pull.ts +8 -8
  268. package/src/Push.ts +472 -0
  269. package/src/RefArray.ts +53 -51
  270. package/src/RefChunk.ts +44 -41
  271. package/src/RefHashMap.ts +57 -43
  272. package/src/RefHashSet.ts +21 -31
  273. package/src/RefSubject.ts +1984 -457
  274. package/src/Sink.ts +922 -171
  275. package/src/Stream.ts +32 -20
  276. package/src/Subject.ts +230 -87
  277. package/src/TypeId.ts +16 -4
  278. package/src/Typeclass.ts +31 -32
  279. package/src/Versioned.ts +220 -168
  280. package/src/index.ts +78 -6
  281. package/src/internal/DeferredRef.ts +62 -0
  282. package/src/internal/UnionToTuple.ts +11 -0
  283. package/src/internal/core.ts +2491 -1950
  284. package/src/internal/diff.ts +157 -0
  285. package/src/internal/effect-loop-operator.ts +466 -0
  286. package/src/internal/effect-operator.ts +89 -54
  287. package/src/internal/effect-producer.ts +125 -0
  288. package/src/internal/helpers.ts +275 -205
  289. package/src/internal/keyed.ts +192 -241
  290. package/src/internal/loop-operator.ts +266 -0
  291. package/src/internal/operator.ts +87 -0
  292. package/src/internal/protos.ts +29 -104
  293. package/src/internal/provide.ts +48 -40
  294. package/src/internal/requestIdleCallback.ts +5 -6
  295. package/src/internal/share.ts +26 -24
  296. package/src/internal/sync-operator.ts +68 -27
  297. package/src/internal/sync-producer.ts +146 -0
  298. package/src/internal/withKey.ts +64 -74
  299. package/dist/cjs/Computed.js +0 -115
  300. package/dist/cjs/Computed.js.map +0 -1
  301. package/dist/cjs/Filtered.js +0 -95
  302. package/dist/cjs/Filtered.js.map +0 -1
  303. package/dist/cjs/Model.js +0 -119
  304. package/dist/cjs/Model.js.map +0 -1
  305. package/dist/cjs/RefAsyncData.js +0 -187
  306. package/dist/cjs/RefAsyncData.js.map +0 -1
  307. package/dist/cjs/RefAsyncDataArray.js +0 -38
  308. package/dist/cjs/RefAsyncDataArray.js.map +0 -1
  309. package/dist/cjs/RefBoolean.js +0 -45
  310. package/dist/cjs/RefBoolean.js.map +0 -1
  311. package/dist/cjs/RefNumber.js +0 -49
  312. package/dist/cjs/RefNumber.js.map +0 -1
  313. package/dist/cjs/internal/core-ref-subject.js +0 -254
  314. package/dist/cjs/internal/core-ref-subject.js.map +0 -1
  315. package/dist/cjs/internal/core-subject.js +0 -108
  316. package/dist/cjs/internal/core-subject.js.map +0 -1
  317. package/dist/cjs/internal/deferred-ref.js.map +0 -1
  318. package/dist/cjs/internal/effect-primitive.js +0 -47
  319. package/dist/cjs/internal/effect-primitive.js.map +0 -1
  320. package/dist/cjs/internal/fx-effect-proto.js +0 -58
  321. package/dist/cjs/internal/fx-effect-proto.js.map +0 -1
  322. package/dist/cjs/internal/fx-primitive.js +0 -193
  323. package/dist/cjs/internal/fx-primitive.js.map +0 -1
  324. package/dist/cjs/internal/fx.js +0 -240
  325. package/dist/cjs/internal/fx.js.map +0 -1
  326. package/dist/cjs/internal/matchers.js +0 -34
  327. package/dist/cjs/internal/matchers.js.map +0 -1
  328. package/dist/cjs/internal/run.js +0 -54
  329. package/dist/cjs/internal/run.js.map +0 -1
  330. package/dist/cjs/internal/schema-ref-subject.js +0 -132
  331. package/dist/cjs/internal/schema-ref-subject.js.map +0 -1
  332. package/dist/cjs/internal/versioned-transform.js +0 -46
  333. package/dist/cjs/internal/versioned-transform.js.map +0 -1
  334. package/dist/dts/Computed.d.ts +0 -106
  335. package/dist/dts/Computed.d.ts.map +0 -1
  336. package/dist/dts/Filtered.d.ts +0 -97
  337. package/dist/dts/Filtered.d.ts.map +0 -1
  338. package/dist/dts/Model.d.ts +0 -200
  339. package/dist/dts/Model.d.ts.map +0 -1
  340. package/dist/dts/RefAsyncData.d.ts +0 -236
  341. package/dist/dts/RefAsyncData.d.ts.map +0 -1
  342. package/dist/dts/RefAsyncDataArray.d.ts +0 -56
  343. package/dist/dts/RefAsyncDataArray.d.ts.map +0 -1
  344. package/dist/dts/RefBoolean.d.ts +0 -47
  345. package/dist/dts/RefBoolean.d.ts.map +0 -1
  346. package/dist/dts/RefNumber.d.ts +0 -48
  347. package/dist/dts/RefNumber.d.ts.map +0 -1
  348. package/dist/dts/internal/core-ref-subject.d.ts +0 -100
  349. package/dist/dts/internal/core-ref-subject.d.ts.map +0 -1
  350. package/dist/dts/internal/core-subject.d.ts +0 -5
  351. package/dist/dts/internal/core-subject.d.ts.map +0 -1
  352. package/dist/dts/internal/deferred-ref.d.ts +0 -12
  353. package/dist/dts/internal/deferred-ref.d.ts.map +0 -1
  354. package/dist/dts/internal/effect-primitive.d.ts +0 -6
  355. package/dist/dts/internal/effect-primitive.d.ts.map +0 -1
  356. package/dist/dts/internal/fx-effect-proto.d.ts +0 -30
  357. package/dist/dts/internal/fx-effect-proto.d.ts.map +0 -1
  358. package/dist/dts/internal/fx-primitive.d.ts +0 -109
  359. package/dist/dts/internal/fx-primitive.d.ts.map +0 -1
  360. package/dist/dts/internal/fx.d.ts +0 -245
  361. package/dist/dts/internal/fx.d.ts.map +0 -1
  362. package/dist/dts/internal/matchers.d.ts +0 -33
  363. package/dist/dts/internal/matchers.d.ts.map +0 -1
  364. package/dist/dts/internal/run.d.ts +0 -8
  365. package/dist/dts/internal/run.d.ts.map +0 -1
  366. package/dist/dts/internal/schema-ref-subject.d.ts +0 -5
  367. package/dist/dts/internal/schema-ref-subject.d.ts.map +0 -1
  368. package/dist/dts/internal/versioned-transform.d.ts +0 -17
  369. package/dist/dts/internal/versioned-transform.d.ts.map +0 -1
  370. package/dist/esm/Computed.js +0 -103
  371. package/dist/esm/Computed.js.map +0 -1
  372. package/dist/esm/Filtered.js +0 -83
  373. package/dist/esm/Filtered.js.map +0 -1
  374. package/dist/esm/Model.js +0 -100
  375. package/dist/esm/Model.js.map +0 -1
  376. package/dist/esm/RefAsyncData.js +0 -163
  377. package/dist/esm/RefAsyncData.js.map +0 -1
  378. package/dist/esm/RefAsyncDataArray.js +0 -27
  379. package/dist/esm/RefAsyncDataArray.js.map +0 -1
  380. package/dist/esm/RefBoolean.js +0 -31
  381. package/dist/esm/RefBoolean.js.map +0 -1
  382. package/dist/esm/RefNumber.js +0 -35
  383. package/dist/esm/RefNumber.js.map +0 -1
  384. package/dist/esm/internal/core-ref-subject.js +0 -242
  385. package/dist/esm/internal/core-ref-subject.js.map +0 -1
  386. package/dist/esm/internal/core-subject.js +0 -90
  387. package/dist/esm/internal/core-subject.js.map +0 -1
  388. package/dist/esm/internal/deferred-ref.js +0 -23
  389. package/dist/esm/internal/deferred-ref.js.map +0 -1
  390. package/dist/esm/internal/effect-primitive.js +0 -41
  391. package/dist/esm/internal/effect-primitive.js.map +0 -1
  392. package/dist/esm/internal/fx-effect-proto.js +0 -43
  393. package/dist/esm/internal/fx-effect-proto.js.map +0 -1
  394. package/dist/esm/internal/fx-primitive.js +0 -175
  395. package/dist/esm/internal/fx-primitive.js.map +0 -1
  396. package/dist/esm/internal/fx.js +0 -216
  397. package/dist/esm/internal/fx.js.map +0 -1
  398. package/dist/esm/internal/matchers.js +0 -38
  399. package/dist/esm/internal/matchers.js.map +0 -1
  400. package/dist/esm/internal/run.js +0 -32
  401. package/dist/esm/internal/run.js.map +0 -1
  402. package/dist/esm/internal/schema-ref-subject.js +0 -121
  403. package/dist/esm/internal/schema-ref-subject.js.map +0 -1
  404. package/dist/esm/internal/versioned-transform.js +0 -37
  405. package/dist/esm/internal/versioned-transform.js.map +0 -1
  406. package/src/Computed.ts +0 -289
  407. package/src/Filtered.ts +0 -274
  408. package/src/Model.ts +0 -483
  409. package/src/RefAsyncData.ts +0 -547
  410. package/src/RefAsyncDataArray.ts +0 -135
  411. package/src/RefBoolean.ts +0 -71
  412. package/src/RefNumber.ts +0 -77
  413. package/src/internal/core-ref-subject.ts +0 -528
  414. package/src/internal/core-subject.ts +0 -143
  415. package/src/internal/deferred-ref.ts +0 -26
  416. package/src/internal/effect-primitive.ts +0 -230
  417. package/src/internal/fx-effect-proto.ts +0 -56
  418. package/src/internal/fx-primitive.ts +0 -223
  419. package/src/internal/fx.ts +0 -619
  420. package/src/internal/matchers.ts +0 -83
  421. package/src/internal/run.ts +0 -51
  422. package/src/internal/schema-ref-subject.ts +0 -163
  423. package/src/internal/versioned-transform.ts +0 -48
@@ -1,841 +1,1561 @@
1
- import { Cause, Chunk, Deferred, Effect, Effectable, Either, Equal, Fiber, Layer, Option, Ref, Scope, Stream, SynchronizedRef } from "effect";
2
- import { dual, identity } from "effect/Function";
3
- import { isContext } from "@typed/context";
4
- import * as Schedule from "effect/Schedule";
5
- import { compileEffectLoop, FilterEffect, FilterMapEffect, liftSyncOperator, MapEffect, TapEffect } from "./effect-operator.js";
6
- import * as helpers from "./helpers.js";
7
- import * as Provide from "./provide.js";
8
- import * as strategies from "./strategies.js";
9
- import { compileSyncReducer, Filter, FilterMap, Map } from "./sync-operator.js";
10
- import * as Emitter from "../Emitter.js";
1
+ import * as Context from "@typed/context";
2
+ import * as Boolean from "effect/Boolean";
3
+ import * as Cause from "effect/Cause";
4
+ import * as Clock from "effect/Clock";
5
+ import * as Deferred from "effect/Deferred";
6
+ import * as Effect from "effect/Effect";
7
+ import * as Either from "effect/Either";
8
+ import * as Equal from "effect/Equal";
9
+ import * as ExecutionStrategy from "effect/ExecutionStrategy";
10
+ import * as Exit from "effect/Exit";
11
+ import * as Fiber from "effect/Fiber";
12
+ import { constFalse, constTrue } from "effect/Function";
13
+ import * as Layer from "effect/Layer";
14
+ import * as Option from "effect/Option";
15
+ import * as Predicate from "effect/Predicate";
16
+ import * as Ref from "effect/Ref";
17
+ import * as Scope from "effect/Scope";
18
+ import * as Tracer from "effect/Tracer";
11
19
  import * as Sink from "../Sink.js";
12
- import { boundsFrom, mergeBounds } from "./bounds.js";
13
- import { matchEffectPrimitive } from "./effect-primitive.js";
14
- import { Empty, Fail, FromIterable, FromSink, Never, Succeed, Suspend, Sync, ToFx, Transformer, TransformerCause, TransformerEffect, WithEarlyExit, WithFlattenStrategy, WithScopedFork } from "./fx-primitive.js";
15
- import { adjustTime } from "./helpers.js";
16
- import { matchFxInput } from "./matchers.js";
17
- import { OnceEffect } from "./protos.js";
18
- import { run } from "./run.js";
19
- const constUnit = () => Effect.unit;
20
- class Merge extends ToFx {
20
+ import { boundsFrom, isInfiniteBounds, isNilBounds, mergeBounds } from "./bounds.js";
21
+ import * as EffectLoopOp from "./effect-loop-operator.js";
22
+ import * as EffectOp from "./effect-operator.js";
23
+ import * as EffectProducer from "./effect-producer.js";
24
+ import { adjustTime, matchEffectPrimitive, tupleSink, withBuffers, withFlattenStrategy, withScopedFork } from "./helpers.js";
25
+ import * as SyncLoopOp from "./loop-operator.js";
26
+ import * as Op from "./operator.js";
27
+ import { EffectBase, FxBase } from "./protos.js";
28
+ import * as Provide from "./provide.js";
29
+ import { Bounded, Exhaust, ExhaustLatest, Ordered, Switch, Unbounded, Unordered } from "./strategies.js";
30
+ import * as SyncOp from "./sync-operator.js";
31
+ import * as SyncProducer from "./sync-producer.js";
32
+ const DISCARD = { discard: true };
33
+ const UNBOUNDED = { concurrency: "unbounded" };
34
+ export function make(run) {
35
+ return new Make(run);
36
+ }
37
+ class Make extends FxBase {
38
+ _run;
39
+ constructor(_run) {
40
+ super();
41
+ this._run = _run;
42
+ }
43
+ run(sink) {
44
+ return Effect.contextWithEffect((ctx) => this._run(Sink.provide(sink, ctx)));
45
+ }
46
+ }
47
+ class Producer extends FxBase {
21
48
  i0;
22
- i1;
23
- constructor(i0, i1) {
24
- super(i0, i1);
49
+ constructor(i0) {
50
+ super();
25
51
  this.i0 = i0;
26
- this.i1 = i1;
27
52
  }
28
- static make(fx, strategy) {
29
- // TODO: Flatten nested Merges
30
- if (fx.length === 0)
31
- return empty;
32
- const nonEmptyFx = fx.filter((fx) => !(fx instanceof Empty));
33
- if (nonEmptyFx.length === 0)
34
- return empty;
35
- if (nonEmptyFx.length === 1)
36
- return nonEmptyFx[0];
37
- const neverIndex = nonEmptyFx.findIndex((fx) => fx instanceof Never);
38
- if (neverIndex === -1)
39
- return new Merge(nonEmptyFx, strategy);
40
- switch (strategy._tag) {
41
- case "Switch":
42
- case "Ordered":
43
- // Will only emit up to the first Never
44
- return new Merge(nonEmptyFx.slice(0, neverIndex + 1), strategy);
45
- // No use creating fibers for Fx that don't emit
46
- case "Unordered":
47
- return new Merge(nonEmptyFx, strategy);
48
- }
53
+ run(sink) {
54
+ return SyncProducer.runSink(this.i0, sink);
49
55
  }
50
- toFx() {
51
- const { i0, i1 } = this;
52
- switch (i1._tag) {
53
- case "Ordered": {
54
- const concurrency = i1.concurrency === Infinity ? "unbounded" : i1.concurrency;
55
- return fromSink((sink) => (Effect.flatMap(helpers.withBuffers(i0.length, sink), (buffers) => Effect.all(i0.map((fx, i) => Effect.flatMap(run(fx, Sink.WithContext((cause) => Cause.isInterruptedOnly(cause) ? Effect.unit : sink.onFailure(cause), (a) => buffers.onSuccess(i, a))), () => buffers.onEnd(i))), {
56
- concurrency
57
- }))));
58
- }
59
- case "Switch":
60
- return fromSink((sink) => Effect.all(i0.map((fx) => run(fx, sink)), { concurrency: 1 }));
61
- case "Unordered":
62
- return fromSink((sink) => Effect.all(i0.map((fx) => run(fx, sink)), {
63
- concurrency: i1.concurrency === Infinity ? "unbounded" : i1.concurrency
64
- }));
65
- }
56
+ }
57
+ /**
58
+ * @internal
59
+ */
60
+ export function isProducer(fx) {
61
+ return fx.constructor === Producer;
62
+ }
63
+ export const succeed = (value) => new Producer(SyncProducer.Success(value));
64
+ export const fromSync = (f) => new Producer(SyncProducer.FromSync(f));
65
+ export const fromArray = (array) => new Producer(SyncProducer.FromArray(array));
66
+ export const fromIterable = (iterable) => new Producer(SyncProducer.FromIterable(iterable));
67
+ class ProducerEffect extends FxBase {
68
+ i0;
69
+ constructor(i0) {
70
+ super();
71
+ this.i0 = i0;
72
+ }
73
+ run(sink) {
74
+ return EffectProducer.runSink(this.i0, sink);
66
75
  }
67
76
  }
68
- class Slice extends ToFx {
77
+ /**
78
+ * @internal
79
+ */
80
+ export function isProducerEffect(fx) {
81
+ return fx.constructor === ProducerEffect;
82
+ }
83
+ export const fromEffect = (effect) => matchEffectPrimitive(effect, {
84
+ // Match over Effect primitives and return Fx primitives to allow fusion to take place
85
+ Success: succeed,
86
+ Failure: failCause,
87
+ Sync: fromSync,
88
+ Left: fail,
89
+ Right: succeed,
90
+ Some: succeed,
91
+ None: fail,
92
+ Otherwise: (effect) => new ProducerEffect(EffectProducer.FromEffect(effect))
93
+ });
94
+ export const fromScheduled = (input, schedule) => new ProducerEffect(EffectProducer.FromScheduled(input, schedule));
95
+ export const schedule = (input, schedule) => new ProducerEffect(EffectProducer.Scheduled(input, schedule));
96
+ class FailCause extends FxBase {
69
97
  i0;
70
- i1;
71
- constructor(i0, i1) {
72
- super(i0, i1);
98
+ constructor(i0) {
99
+ super();
73
100
  this.i0 = i0;
74
- this.i1 = i1;
75
101
  }
76
- static make(fx, bounds) {
77
- if (fx instanceof Slice) {
78
- return new Slice(fx.i0, mergeBounds(fx.i1, bounds));
79
- }
80
- else if (fx instanceof Transformer && fx.i1._tag === "Map") {
81
- return new Transformer(Slice.make(fx.i0, bounds), fx.i1);
82
- }
83
- else {
84
- return new Slice(fx, bounds);
85
- }
102
+ run(sink) {
103
+ return sink.onFailure(this.i0);
86
104
  }
87
- toFx() {
88
- const fx = this.i0;
89
- const { max, min } = this.i1;
90
- return withEarlyExit(({ sink }) => Effect.suspend(() => {
91
- let toSkip = min;
92
- let toTake = max;
93
- return run(fx, Sink.WithContext(sink.onFailure, (a) => Effect.suspend(() => {
94
- if (toSkip > 0) {
95
- toSkip -= 1;
96
- return Effect.unit;
97
- }
98
- else if (toTake > 0) {
99
- toTake -= 1;
100
- return Effect.flatMap(sink.onSuccess(a), () => toTake <= 0 ? sink.earlyExit : Effect.unit);
101
- }
102
- else {
103
- return sink.earlyExit;
104
- }
105
- })));
106
- }));
107
- }
108
- }
109
- class Loop extends ToFx {
105
+ }
106
+ /**
107
+ * @internal
108
+ */
109
+ export function isFailCause(fx) {
110
+ return fx.constructor === FailCause;
111
+ }
112
+ export const failCause = (cause) => new FailCause(cause);
113
+ export const fail = (error) => failCause(Cause.fail(error));
114
+ export const die = (error) => failCause(Cause.die(error));
115
+ class Transformer extends FxBase {
110
116
  i0;
111
117
  i1;
112
- i2;
113
- constructor(i0, i1, i2) {
114
- super(i0, i1, i2);
118
+ constructor(i0, i1) {
119
+ super();
115
120
  this.i0 = i0;
116
121
  this.i1 = i1;
117
- this.i2 = i2;
118
122
  }
119
- static make(fx, b, f) {
120
- if (fx instanceof Transformer) {
121
- return FilterMapLoop.make(fx.i0, b, compileSyncReducer(fx.i1, f));
123
+ run(sink) {
124
+ return this.i0.run(Op.compileOperatorSink(this.i1, sink));
125
+ }
126
+ static make(fx, operator) {
127
+ if (isEmpty(fx) || isNever(fx))
128
+ return fx;
129
+ else if (isProducer(fx)) {
130
+ return new ProducerSyncTransformer(fx.i0, operator);
131
+ }
132
+ else if (isTransformer(fx)) {
133
+ return new Transformer(fx.i0, Op.fuseOperators(fx.i1, operator));
134
+ }
135
+ else if (isProducerSyncTransformer(fx)) {
136
+ return new ProducerSyncTransformer(fx.i0, Op.fuseOperators(fx.i1, operator));
137
+ }
138
+ else if (isProducerEffect(fx)) {
139
+ return new ProducerEffectTransformer(fx.i0, operator);
122
140
  }
123
- else if (fx instanceof TransformerEffect) {
124
- return new FilterMapLoopEffect(fx.i0, b, compileEffectLoop(fx.i1, (b, a) => Effect.sync(() => f(b, a))));
141
+ else if (isProducerEffectTransformer(fx)) {
142
+ return new ProducerEffectTransformer(fx.i0, Op.fuseOperators(fx.i1, operator));
143
+ }
144
+ else if (isSuspend(fx)) {
145
+ return new SuspendedTransformer(fx.i0, operator);
146
+ }
147
+ else if (isSuspendedTransformer(fx)) {
148
+ return new SuspendedTransformer(fx.i0, Op.fuseOperators(fx.i1, operator));
149
+ }
150
+ else if (isFailCause(fx)) {
151
+ return fx;
125
152
  }
126
153
  else {
127
- return new Loop(fx, f, b);
154
+ return new Transformer(fx, operator);
128
155
  }
129
156
  }
130
- toFx() {
131
- return fromSink((sink) => Effect.suspend(() => {
132
- let acc = this.i2;
133
- return run(this.i0, Sink.WithContext(sink.onFailure, (a) => Effect.suspend(() => {
134
- const [c, b] = this.i1(acc, a);
135
- acc = b;
136
- return sink.onSuccess(c);
137
- })));
138
- }));
139
- }
140
157
  }
141
- class FilterMapLoop extends ToFx {
158
+ /**
159
+ * @internal
160
+ */
161
+ export function isTransformer(fx) {
162
+ return fx.constructor === Transformer;
163
+ }
164
+ class ProducerSyncTransformer extends FxBase {
142
165
  i0;
143
166
  i1;
144
- i2;
145
- constructor(i0, i1, i2) {
146
- super(i0, i1, i2);
167
+ constructor(i0, i1) {
168
+ super();
147
169
  this.i0 = i0;
148
170
  this.i1 = i1;
149
- this.i2 = i2;
150
171
  }
151
- static make(fx, b, f) {
152
- return new FilterMapLoop(fx, b, f);
153
- }
154
- toFx() {
155
- return fromSink((sink) => Effect.suspend(() => {
156
- let acc = this.i1;
157
- return run(this.i0, Sink.WithContext(sink.onFailure, (a) => Effect.suspend(() => {
158
- const optionCB = this.i2(acc, a);
159
- if (Option.isNone(optionCB))
160
- return Effect.unit;
161
- const [c, b] = optionCB.value;
162
- acc = b;
163
- return sink.onSuccess(c);
164
- })));
165
- }));
172
+ run(sink) {
173
+ return SyncProducer.runSink(this.i0, Op.compileOperatorSink(this.i1, sink));
166
174
  }
167
175
  }
168
- class LoopEffect extends ToFx {
176
+ /**
177
+ * @internal
178
+ */
179
+ export function isProducerSyncTransformer(fx) {
180
+ return fx.constructor === ProducerSyncTransformer;
181
+ }
182
+ export const map = (fx, f) => Transformer.make(fx, SyncOp.Map(f));
183
+ export const filter = (fx, f) => Transformer.make(fx, SyncOp.Filter(f));
184
+ export const filterMap = (fx, f) => Transformer.make(fx, SyncOp.FilterMap(f));
185
+ export const mapEffect = (fx, f) => Transformer.make(fx, EffectOp.MapEffect(f));
186
+ export const filterMapEffect = (fx, f) => Transformer.make(fx, EffectOp.FilterMapEffect(f));
187
+ export const filterEffect = (fx, f) => Transformer.make(fx, EffectOp.FilterEffect(f));
188
+ export const tapEffect = (fx, f) => Transformer.make(fx, EffectOp.TapEffect(f));
189
+ export const loop = (fx, seed, f) => Transformer.make(fx, SyncLoopOp.LoopOperator(seed, f));
190
+ export const withPrevious = (fx) => loop(fx, Option.none(), (acc, a) => [[acc, a], Option.some(a)]);
191
+ export const filterMapLoop = (fx, seed, f) => Transformer.make(fx, SyncLoopOp.FilterMapLoopOperator(seed, f));
192
+ export const loopEffect = (fx, seed, f) => Transformer.make(fx, EffectLoopOp.LoopEffectOperator(seed, f));
193
+ export const filterMapLoopEffect = (fx, seed, f) => Transformer.make(fx, EffectLoopOp.FilterMapLoopEffectOperator(seed, f));
194
+ export const observe = (fx, f) => Observe.make(fx, f);
195
+ const constUnit = () => Effect.unit;
196
+ export const drain = (fx) => Observe.make(fx, constUnit);
197
+ class Observe extends EffectBase {
169
198
  i0;
170
199
  i1;
171
- i2;
172
- constructor(i0, i1, i2) {
173
- super(i0, i1, i2);
200
+ constructor(i0, i1) {
201
+ super();
174
202
  this.i0 = i0;
175
203
  this.i1 = i1;
176
- this.i2 = i2;
177
204
  }
178
- static make(fx, f, b) {
179
- if (fx instanceof TransformerEffect) {
180
- return new FilterMapLoopEffect(fx.i0, b, compileEffectLoop(fx.i1, f));
205
+ toEffect() {
206
+ return Effect.asyncEffect((resume) => {
207
+ const { i0: fx, i1: f } = this;
208
+ const onFailure = (cause) => Effect.sync(() => resume(Effect.failCause(cause)));
209
+ return Effect.zipRight(fx.run(Sink.make(onFailure, (a) => Effect.catchAllCause(f(a), onFailure))), Effect.sync(() => resume(Effect.unit)));
210
+ });
211
+ }
212
+ static make(fx, f) {
213
+ if (isEmpty(fx)) {
214
+ return Effect.unit;
215
+ }
216
+ else if (isNever(fx)) {
217
+ return Effect.never;
218
+ }
219
+ else if (isProducer(fx)) {
220
+ return SyncProducer.runEffect(fx.i0, f);
221
+ }
222
+ else if (isProducerSyncTransformer(fx)) {
223
+ return Op.matchOperator(fx.i1, {
224
+ SyncOperator: (op) => SyncOp.matchSyncOperator(op, {
225
+ Map: (op) => SyncProducer.effectOnce(() => SyncProducer.runEffect(fx.i0, (a) => f(op.f(a)))),
226
+ Filter: (op) => SyncProducer.effectOnce(() => SyncProducer.runEffect(fx.i0, Effect.unifiedFn((a) => op.f(a) ? f(a) : Effect.unit))),
227
+ FilterMap: (op) => SyncProducer.effectOnce(() => SyncProducer.runEffect(fx.i0, Effect.unifiedFn((a) => Option.match(op.f(a), { onNone: () => Effect.unit, onSome: f }))))
228
+ }),
229
+ EffectOperator: (op) => EffectOp.matchEffectOperator(op, {
230
+ MapEffect: (op) => SyncProducer.runEffect(fx.i0, (a) => Effect.flatMap(op.f(a), f)),
231
+ FilterMapEffect: (op) => SyncProducer.runEffect(fx.i0, (a) => Effect.flatMap(op.f(a), Effect.unifiedFn(Option.match({ onNone: () => Effect.unit, onSome: f })))),
232
+ FilterEffect: (op) => SyncProducer.runEffect(fx.i0, Effect.unifiedFn((a) => Effect.flatMap(op.f(a), Effect.unifiedFn((b) => b ? f(a) : Effect.unit)))),
233
+ TapEffect: (op) => SyncProducer.runEffect(fx.i0, (a) => Effect.flatMap(op.f(a), () => f(a)))
234
+ }),
235
+ SyncLoopOperator: (op) => SyncLoopOp.matchSyncLoopOperator(op, {
236
+ Loop: (op) => SyncProducer.effectOnce(() => SyncProducer.runReduceEffect(fx.i0, op.seed, (acc, a) => {
237
+ const [c, b] = op.f(acc, a);
238
+ return Effect.as(f(c), b);
239
+ })),
240
+ FilterMapLoop: (op) => SyncProducer.effectOnce(() => SyncProducer.runReduceEffect(fx.i0, op.seed, (acc, a) => {
241
+ const [c, b] = op.f(acc, a);
242
+ return Option.match(c, { onNone: () => Effect.succeed(acc), onSome: (c) => Effect.as(f(c), b) });
243
+ }))
244
+ }),
245
+ EffectLoopOperator: (op) => EffectLoopOp.matchEffectLoopOperator(op, {
246
+ LoopEffect: (op) => SyncProducer.runReduceEffect(fx.i0, op.seed, (acc, a) => Effect.flatMap(op.f(acc, a), ([c, b]) => Effect.as(f(c), b))),
247
+ FilterMapLoopEffect: (op) => SyncProducer.runReduceEffect(fx.i0, op.seed, (acc, a) => Effect.flatMap(op.f(acc, a), ([c, b]) => Option.match(c, {
248
+ onNone: () => Effect.succeed(b),
249
+ onSome: (c) => Effect.as(f(c), b)
250
+ })))
251
+ })
252
+ });
253
+ }
254
+ else if (isProducerEffect(fx)) {
255
+ return EffectProducer.runEffect(fx.i0, f);
256
+ }
257
+ else if (isProducerEffectTransformer(fx)) {
258
+ return Op.matchOperator(fx.i1, {
259
+ SyncOperator: (op) => SyncOp.matchSyncOperator(op, {
260
+ Map: (op) => SyncProducer.effectOnce(() => EffectProducer.runEffect(fx.i0, (a) => f(op.f(a)))),
261
+ Filter: (op) => SyncProducer.effectOnce(() => EffectProducer.runEffect(fx.i0, Effect.unifiedFn((a) => op.f(a) ? f(a) : Effect.unit))),
262
+ FilterMap: (op) => SyncProducer.effectOnce(() => EffectProducer.runEffect(fx.i0, Effect.unifiedFn((a) => Option.match(op.f(a), { onNone: () => Effect.unit, onSome: f }))))
263
+ }),
264
+ EffectOperator: (op) => EffectOp.matchEffectOperator(op, {
265
+ MapEffect: (op) => EffectProducer.runEffect(fx.i0, (a) => Effect.flatMap(op.f(a), f)),
266
+ FilterMapEffect: (op) => EffectProducer.runEffect(fx.i0, (a) => Effect.flatMap(op.f(a), Effect.unifiedFn(Option.match({ onNone: () => Effect.unit, onSome: f })))),
267
+ FilterEffect: (op) => EffectProducer.runEffect(fx.i0, Effect.unifiedFn((a) => Effect.flatMap(op.f(a), Effect.unifiedFn((b) => b ? f(a) : Effect.unit)))),
268
+ TapEffect: (op) => EffectProducer.runEffect(fx.i0, (a) => Effect.flatMap(op.f(a), () => f(a)))
269
+ }),
270
+ SyncLoopOperator: (op) => SyncLoopOp.matchSyncLoopOperator(op, {
271
+ Loop: (op) => SyncProducer.effectOnce(() => EffectProducer.runReduceEffect(fx.i0, op.seed, (acc, a) => {
272
+ const [c, b] = op.f(acc, a);
273
+ return Effect.as(f(c), b);
274
+ })),
275
+ FilterMapLoop: (op) => SyncProducer.effectOnce(() => EffectProducer.runReduceEffect(fx.i0, op.seed, (acc, a) => {
276
+ const [c, b] = op.f(acc, a);
277
+ return Option.match(c, { onNone: () => Effect.succeed(acc), onSome: (c) => Effect.as(f(c), b) });
278
+ }))
279
+ }),
280
+ EffectLoopOperator: (op) => EffectLoopOp.matchEffectLoopOperator(op, {
281
+ LoopEffect: (op) => EffectProducer.runReduceEffect(fx.i0, op.seed, (acc, a) => Effect.flatMap(op.f(acc, a), ([c, b]) => Effect.as(f(c), b))),
282
+ FilterMapLoopEffect: (op) => EffectProducer.runReduceEffect(fx.i0, op.seed, (acc, a) => Effect.flatMap(op.f(acc, a), ([c, b]) => Option.match(c, {
283
+ onNone: () => Effect.succeed(b),
284
+ onSome: (c) => Effect.as(f(c), b)
285
+ })))
286
+ })
287
+ });
181
288
  }
182
- else if (fx instanceof Transformer) {
183
- return new FilterMapLoopEffect(fx.i0, b, compileEffectLoop(liftSyncOperator(fx.i1), f));
289
+ else if (isFailCause(fx)) {
290
+ return Effect.failCause(fx.i0);
184
291
  }
185
292
  else {
186
- return new LoopEffect(fx, f, b);
293
+ return new Observe(fx, f);
187
294
  }
188
295
  }
189
- toFx() {
190
- const { i0, i1, i2 } = this;
191
- return fromSink((sink) => Effect.flatMap(SynchronizedRef.make(i2), (ref) => run(i0, Sink.WithContext(sink.onFailure, (a) => SynchronizedRef.updateEffect(ref, (b) => Effect.matchCauseEffect(i1(b, a), {
192
- onFailure: (cause) => Effect.as(sink.onFailure(cause), b),
193
- onSuccess: ([c, b2]) => Effect.as(sink.onSuccess(c), b2)
194
- }))))));
195
- }
196
296
  }
197
- class FilterMapLoopEffect extends ToFx {
297
+ export const reduce = (fx, seed, f) => Reduce.make(fx, seed, f);
298
+ class Reduce extends EffectBase {
198
299
  i0;
199
300
  i1;
200
301
  i2;
201
302
  constructor(i0, i1, i2) {
202
- super(i0, i1, i2);
303
+ super();
203
304
  this.i0 = i0;
204
305
  this.i1 = i1;
205
306
  this.i2 = i2;
206
307
  }
207
- toFx() {
208
- return fromSink((sink) => Effect.suspend(() => {
308
+ toEffect() {
309
+ return Effect.suspend(() => {
209
310
  let acc = this.i1;
210
- return run(this.i0, Sink.WithContext(sink.onFailure, (a) => this.i2(acc, a).pipe(Effect.matchCauseEffect({
211
- onFailure: sink.onFailure,
212
- onSuccess: (optionCB) => Effect.suspend(() => {
213
- if (Option.isNone(optionCB))
214
- return Effect.unit;
215
- const [c, b] = optionCB.value;
216
- acc = b;
217
- return sink.onSuccess(c);
311
+ return Effect.map(observe(this.i0, (a) => Effect.sync(() => acc = this.i2(acc, a))), () => acc);
312
+ });
313
+ }
314
+ static make(fx, seed, f) {
315
+ // TODO: optimize Effect trasformers
316
+ if (isEmpty(fx))
317
+ return Effect.succeed(seed);
318
+ else if (isProducer(fx)) {
319
+ return SyncProducer.runReduce(fx.i0, seed, f);
320
+ }
321
+ else if (isProducerSyncTransformer(fx)) {
322
+ return Op.matchOperator(fx.i1, {
323
+ SyncOperator: (op) => SyncProducer.syncOnce(() => SyncOp.runSyncReduce(fx.i0, op, seed, f)),
324
+ EffectOperator: (op) => EffectOp.runSyncReduce(fx.i0, op, seed, f),
325
+ SyncLoopOperator: (op) => SyncLoopOp.matchSyncLoopOperator(op, {
326
+ Loop: (op) => Effect.map(SyncProducer.runReduce(fx.i0, [op.seed, seed], ([opAcc, acc], a) => {
327
+ const [c, b] = op.f(opAcc, a);
328
+ const newAcc = f(acc, c);
329
+ return [b, newAcc];
330
+ }), (x) => x[1]),
331
+ FilterMapLoop: (op) => Effect.map(SyncProducer.runReduce(fx.i0, [op.seed, seed], ([opAcc, acc], a) => {
332
+ const [c, b] = op.f(opAcc, a);
333
+ const newAcc = Option.match(c, { onNone: () => acc, onSome: (c) => f(acc, c) });
334
+ return [b, newAcc];
335
+ }), (x) => x[1])
336
+ }),
337
+ EffectLoopOperator: (op) => EffectLoopOp.matchEffectLoopOperator(op, {
338
+ LoopEffect: (op) => Effect.map(SyncProducer.runReduceEffect(fx.i0, [op.seed, seed], ([opAcc, acc], a) => {
339
+ return Effect.flatMap(op.f(opAcc, a), ([c, b]) => {
340
+ const newAcc = f(acc, c);
341
+ return Effect.succeed([b, newAcc]);
342
+ });
343
+ }), (x) => x[1]),
344
+ FilterMapLoopEffect: (op) => Effect.map(SyncProducer.runReduceEffect(fx.i0, [op.seed, seed], ([opAcc, acc], a) => {
345
+ return Effect.map(op.f(opAcc, a), ([c, b]) => {
346
+ const newAcc = Option.match(c, { onNone: () => acc, onSome: () => f(acc, b) });
347
+ return [b, newAcc];
348
+ });
349
+ }), (x) => x[1])
218
350
  })
219
- }))));
220
- }));
351
+ });
352
+ }
353
+ else if (isProducerEffect(fx)) {
354
+ return EffectProducer.runReduceEffect(fx.i0, seed, (b, a) => Effect.succeed(f(b, a)));
355
+ }
356
+ else if (isFailCause(fx)) {
357
+ return Effect.failCause(fx.i0);
358
+ }
359
+ else {
360
+ return new Reduce(fx, seed, f);
361
+ }
221
362
  }
222
363
  }
223
- class FxProvide extends ToFx {
364
+ export const toReadonlyArray = (fx) => Effect.suspend(() => {
365
+ const init = [];
366
+ return Reduce.make(fx, init, (acc, a) => {
367
+ acc.push(a);
368
+ return acc;
369
+ });
370
+ });
371
+ export const slice = (fx, drop, take) => Slice.make(fx, boundsFrom(drop, take));
372
+ export const take = (fx, n) => slice(fx, 0, n);
373
+ export const drop = (fx, n) => slice(fx, n, Infinity);
374
+ class Slice extends FxBase {
224
375
  i0;
225
376
  i1;
226
377
  constructor(i0, i1) {
227
- super(i0, i1);
378
+ super();
228
379
  this.i0 = i0;
229
380
  this.i1 = i1;
230
381
  }
231
- static make(fx, provide) {
232
- if (fx instanceof FxProvide) {
233
- return new FxProvide(fx.i0, Provide.merge(fx.i1, provide));
382
+ run(sink) {
383
+ return Sink.slice(sink, this.i1, (s) => this.i0.run(s));
384
+ }
385
+ static make(fx, bounds) {
386
+ if (isNilBounds(bounds))
387
+ return empty;
388
+ if (isInfiniteBounds(bounds))
389
+ return fx;
390
+ if (isSlice(fx)) {
391
+ return Slice.make(fx.i0, mergeBounds(fx.i1, bounds));
392
+ }
393
+ else if (isTransformer(fx) && fx.i1._tag === "Map") {
394
+ // Commute map and slice
395
+ return map(Slice.make(fx.i0, bounds), fx.i1.f);
234
396
  }
235
397
  else {
236
- return new FxProvide(fx, provide);
398
+ return new Slice(fx, bounds);
237
399
  }
238
400
  }
239
- toFx() {
240
- return fromSink((sink) => Effect.catchAllCause(Provide.provideToEffect(run(this.i0, sink), this.i1), sink.onFailure));
241
- }
242
401
  }
243
- class Snapshot extends ToFx {
402
+ /**
403
+ * @internal
404
+ */
405
+ export function isSlice(fx) {
406
+ return fx.constructor === Slice;
407
+ }
408
+ export function skipRepeatsWith(fx, eq) {
409
+ return filterMapLoop(fx, Option.none(), (previous, a) => {
410
+ if (Option.isSome(previous) && eq(a, previous.value)) {
411
+ return [Option.none(), Option.some(a)];
412
+ }
413
+ else {
414
+ return [Option.some(a), Option.some(a)];
415
+ }
416
+ });
417
+ }
418
+ export function skipRepeats(fx) {
419
+ return skipRepeatsWith(fx, Equal.equals);
420
+ }
421
+ class ProducerEffectTransformer extends FxBase {
244
422
  i0;
245
423
  i1;
246
- i2;
247
- constructor(i0, i1, i2) {
248
- super(i0, i1, i2);
424
+ constructor(i0, i1) {
425
+ super();
249
426
  this.i0 = i0;
250
427
  this.i1 = i1;
251
- this.i2 = i2;
252
428
  }
253
- toFx() {
254
- return matchFxInput(this.i1, {
255
- RefSubject: (fx2) => this.runScoped(this.i0, fx2, this.i2),
256
- Fx: (fx2) => this.runScoped(this.i0, fx2, this.i2),
257
- Effect: (effect2) => mapEffect(this.i0, (a) => Effect.flatMap(effect2, (b) => this.i2(a, b))),
258
- Cause: (cause2) => matchCause(this.i0, {
259
- onFailure: (cause1) => Effect.failCause(Cause.sequential(cause1, cause2)),
260
- onSuccess: () => Effect.failCause(cause2)
261
- }),
262
- Iterable: (iterable) => withEarlyExit(({ sink }) => {
263
- const iterator = iterable[Symbol.iterator]();
264
- return run(this.i0, Sink.WithContext(sink.onFailure, (a) => {
265
- const result = iterator.next();
266
- if (result.done) {
267
- return sink.earlyExit;
268
- }
269
- else {
270
- return Effect.matchCauseEffect(this.i2(a, result.value), sink);
271
- }
272
- }));
273
- }),
274
- Otherwise: (b) => mapEffect(this.i0, (a) => this.i2(a, b))
275
- });
429
+ run(sink) {
430
+ return EffectProducer.runSink(this.i0, Op.compileOperatorSink(this.i1, sink));
431
+ }
432
+ }
433
+ /**
434
+ * @internal
435
+ */
436
+ export function isProducerEffectTransformer(fx) {
437
+ return fx.constructor === ProducerEffectTransformer;
438
+ }
439
+ class Empty extends FxBase {
440
+ run() {
441
+ return Effect.unit;
276
442
  }
277
- runScoped(fx, fx2, f) {
278
- return withScopedFork(({ fork, sink }) => Effect.flatMap(Ref.make(Option.none()), (ref) => Effect.flatMap(fork(run(fx2, Sink.WithContext(sink.onFailure, (b) => Ref.set(ref, Option.some(b))))), () => adjustTime(1).pipe(Effect.zipRight(run(fx, Sink.WithContext(sink.onFailure, (a) => Effect.flatten(Ref.get(ref)).pipe(Effect.flatMap((b) => Effect.matchCauseEffect(f(a, b), sink)), Effect.optionFromOptional, Effect.asUnit))))))));
443
+ }
444
+ /**
445
+ * @internal
446
+ */
447
+ export function isEmpty(fx) {
448
+ return fx.constructor === Empty;
449
+ }
450
+ export const empty = new Empty();
451
+ class Never extends FxBase {
452
+ run() {
453
+ return Effect.never;
279
454
  }
280
455
  }
281
- class Middleware extends ToFx {
456
+ /**
457
+ * @internal
458
+ */
459
+ export function isNever(fx) {
460
+ return fx.constructor === Never;
461
+ }
462
+ export const never = new Never();
463
+ export function padWith(fx, start, end) {
464
+ return new PadWith(fx, start, end);
465
+ }
466
+ export function prependAll(fx, start) {
467
+ return new PadWith(fx, start, []);
468
+ }
469
+ export function appendAll(fx, end) {
470
+ return new PadWith(fx, [], end);
471
+ }
472
+ export function prepend(fx, start) {
473
+ return new PadWith(fx, [start], []);
474
+ }
475
+ export function append(fx, end) {
476
+ return new PadWith(fx, [], [end]);
477
+ }
478
+ export function scan(fx, seed, f) {
479
+ return prepend(loop(fx, seed, (b, a) => {
480
+ const b2 = f(b, a);
481
+ return [b2, b2];
482
+ }), seed);
483
+ }
484
+ class PadWith extends FxBase {
282
485
  i0;
283
486
  i1;
284
487
  i2;
285
488
  constructor(i0, i1, i2) {
286
- super(i0, i1);
489
+ super();
287
490
  this.i0 = i0;
288
491
  this.i1 = i1;
289
492
  this.i2 = i2;
290
493
  }
291
- static make(fx, middleware, mapSink) {
292
- if (fx instanceof Middleware) {
293
- return new Middleware(fx.i0, (effect) => middleware(fx.i1(effect)), (s) => fx.i2(mapSink(s)));
494
+ run(sink) {
495
+ const onSuccess = (a) => sink.onSuccess(a);
496
+ return Effect.forEach(this.i1, onSuccess, DISCARD).pipe(Effect.zipRight(this.i0.run(sink)), Effect.zipRight(Effect.forEach(this.i2, onSuccess, DISCARD)));
497
+ }
498
+ static make(fx, start, end) {
499
+ if (isEmpty(fx) || isNever(fx))
500
+ return fx;
501
+ else if (isPadWith(fx)) {
502
+ return new PadWith(fx.i0, concat(start, fx.i1), concat(fx.i2, end));
294
503
  }
295
504
  else {
296
- return new Middleware(fx, middleware, mapSink);
505
+ return new PadWith(fx, start, end);
297
506
  }
298
507
  }
299
- toFx() {
300
- return fromSink((sink) => this.i1(run(this.i0, this.i2(sink))));
301
- }
302
508
  }
303
- export function succeed(value) {
304
- return new Succeed(value);
509
+ function concat(a, b) {
510
+ return {
511
+ *[Symbol.iterator]() {
512
+ yield* a;
513
+ yield* b;
514
+ }
515
+ };
305
516
  }
306
- export function fromIterable(iterable) {
307
- return new FromIterable(iterable);
517
+ /**
518
+ * @internal
519
+ */
520
+ export function isPadWith(fx) {
521
+ return fx.constructor === PadWith;
308
522
  }
309
- export const map = dual(2, function map(fx, f) {
310
- return Transformer.make(fx, Map(f));
311
- });
312
- export const mapBoth = dual(2, function mapBoth(fx, options) {
313
- return map(mapError(fx, options.onFailure), options.onSuccess);
314
- });
315
- export const filter = dual(2, function map(fx, f) {
316
- return Transformer.make(fx, Filter(f));
317
- });
318
- export const filterMap = dual(2, function map(fx, f) {
319
- return Transformer.make(fx, FilterMap(f));
320
- });
321
- export const compact = (fx) => filterMap(fx, identity);
322
- export const mapErrorCause = dual(2, function map(fx, f) {
323
- return TransformerCause.make(fx, Map(f));
324
- });
325
- export const mapError = dual(2, function map(fx, f) {
326
- return TransformerCause.make(fx, Map(Cause.map(f)));
327
- });
328
- export const filterCause = dual(2, function map(fx, f) {
329
- return TransformerCause.make(fx, Filter(f));
330
- });
331
- export const filterMapCause = dual(2, function map(fx, f) {
332
- return TransformerCause.make(fx, FilterMap(f));
333
- });
334
- export const filterError = dual(2, function map(fx, f) {
335
- return filterMapCause(fx, (cause) => Cause.failureOrCause(cause).pipe(Either.match({
336
- onLeft: (e) => f(e) ? Option.some(Cause.fail(e)) : Option.none(),
337
- onRight: Option.some
338
- })));
339
- });
340
- export const filterMapError = dual(2, function map(fx, f) {
341
- return filterMapCause(fx, (cause) => Cause.failureOrCause(cause).pipe(Either.match({
342
- onLeft: (e) => Option.map(f(e), Cause.fail),
343
- onRight: Option.some
344
- })));
345
- });
346
- export const filterMapErrorEffect = dual(2, (fx, f) => {
347
- return fromSink((sink) => run(fx, Sink.WithContext((cause) => Either.match(Cause.failureOrCause(cause), {
348
- onLeft: (e) => Effect.matchCauseEffect(f(e), {
349
- onFailure: sink.onFailure,
350
- onSuccess: Option.match({
351
- onNone: () => Effect.unit,
352
- onSome: (b) => sink.onFailure(Cause.fail(b))
353
- })
354
- }),
355
- onRight: sink.onFailure
356
- }), sink.onSuccess)));
357
- });
358
- export function observe(fx, onSuccees) {
359
- return helpers.withScopedFork((fork) => Effect.flatMap(Deferred.make(), (deferred) => run(fx, Sink.WithContext((cause) => Deferred.failCause(deferred, cause), (a) => Effect.catchAllCause(onSuccees(a), (cause) => Deferred.failCause(deferred, cause)))).pipe(Effect.intoDeferred(deferred), fork, Effect.flatMap(() => Deferred.await(deferred)))));
523
+ export function flatMapWithStrategy(fx, f, strategy, executionStrategy = ExecutionStrategy.sequential) {
524
+ return FlatMapWithStrategy.make(fx, f, strategy, executionStrategy);
360
525
  }
361
- export function drain(fx) {
362
- return observe(fx, constUnit);
526
+ export function switchMap(fx, f, executionStrategy) {
527
+ return flatMapWithStrategy(fx, f, Switch, executionStrategy);
363
528
  }
364
- export function toArray(fx) {
365
- return Effect.suspend(() => {
366
- const array = [];
367
- return Effect.as(observe(fx, (a) => Effect.sync(() => array.push(a))), array);
368
- });
529
+ export function switchMapEffect(fx, f, executionStrategy) {
530
+ return switchMap(fx, (a) => fromEffect(f(a)), executionStrategy);
369
531
  }
370
- export function toReadonlyArray(fx) {
371
- return toArray(fx);
532
+ export function exhaustMap(fx, f, executionStrategy) {
533
+ return flatMapWithStrategy(fx, f, Exhaust, executionStrategy);
372
534
  }
373
- export const flatMapWithStrategy = dual(3, function flatMapWithStrategy(fx, f, strategy) {
374
- return new WithFlattenStrategy(({ fork, sink }) => run(fx, Sink.WithContext(sink.onFailure, (a) => fork(run(from(f(a)), sink)))), strategy);
375
- });
376
- export const switchMap = dual(2, function switchMap(fx, f) {
377
- return flatMapWithStrategy(fx, f, strategies.Switch);
378
- });
379
- export const exhaustMap = dual(2, function exhaustmap(fx, f) {
380
- return flatMapWithStrategy(fx, f, strategies.Exhaust);
381
- });
382
- export const exhaust = (fx) => exhaustMap(fx, identity);
383
- export const exhaustMapLatest = dual(2, function exhaustMapLatest(fx, f) {
384
- return flatMapWithStrategy(fx, f, strategies.ExhaustLatest);
385
- });
386
- export const exhaustLatest = (fx) => exhaustMapLatest(fx, identity);
387
- export const flatMap = dual(2, function flatMap(fx, f) {
388
- return flatMapWithStrategy(fx, f, strategies.Unbounded);
389
- });
390
- export const flatten = (fx) => flatMap(fx, identity);
391
- export const flatMapConcurrently = dual(3, function flatMapConcurrently(fx, f, concurrency) {
392
- return flatMapWithStrategy(fx, f, strategies.Bounded(concurrency));
393
- });
394
- export const concatMap = dual(2, function concatMap(fx, f) {
395
- return flatMapConcurrently(fx, f, 1);
396
- });
397
- export const acquireUseRelease = dual(3, function acquireUseRelease(acquire, use, release) {
398
- return fromSink((sink) => Effect.catchAllCause(Effect.acquireUseRelease(acquire, (a) => run(from(use(a)), sink), (a, exit) => Effect.catchAllCause(release(a, exit), sink.onFailure)), sink.onFailure));
399
- });
400
- export function combine(fxs) {
401
- return fromSink((sink) => Effect.suspend(() => {
402
- const total = fxs.length;
403
- if (total === 0)
404
- return Effect.unit;
405
- const values = new globalThis.Map();
406
- const sample = () => Array.from({ length: total }, (_, i) => values.get(i));
407
- const emitIfReady = (value, index) => Effect.suspend(() => {
408
- values.set(index, value);
409
- if (values.size === total) {
410
- return sink.onSuccess(sample());
411
- }
412
- else {
413
- return Effect.unit;
414
- }
415
- });
416
- return Effect.all(fxs.map((fx, index) => run(fx, Sink.WithContext(sink.onFailure, (a) => emitIfReady(a, index)))), { concurrency: "unbounded" });
417
- }));
535
+ export function exhaustMapEffect(fx, f, executionStrategy) {
536
+ return exhaustMap(fx, (a) => fromEffect(f(a)), executionStrategy);
418
537
  }
419
- export function struct(fxs) {
420
- return map(combine(Reflect.ownKeys(fxs).map((k) => map(fxs[k], (a) => [k, a]))), Object.fromEntries);
538
+ export function exhaustMapLatest(fx, f, executionStrategy) {
539
+ return flatMapWithStrategy(fx, f, ExhaustLatest, executionStrategy);
421
540
  }
422
- export function merge(fxs) {
423
- return Merge.make(fxs, strategies.Unordered(Infinity));
541
+ export function exhaustMapLatestEffect(fx, f, executionStrategy) {
542
+ return exhaustMapLatest(fx, (a) => fromEffect(f(a)), executionStrategy);
424
543
  }
425
- export const mergeConcurrently = dual(2, function mergeConcurrently(fxs, concurrency) {
426
- return Merge.make(fxs, strategies.Unordered(concurrency));
427
- });
428
- export function mergeBuffer(fxs) {
429
- return Merge.make(fxs, strategies.Ordered(Infinity));
544
+ export function exhaustFilterMapLatestEffect(fx, f, executionStrategy) {
545
+ return exhaustMapLatest(fx, (a) => fromFxEffect(Effect.map(f(a), Option.match({
546
+ onNone: () => empty,
547
+ onSome: succeed
548
+ }))), executionStrategy);
430
549
  }
431
- export const mergeBufferConcurrently = dual(2, function mergeConcurrently(fxs, concurrency) {
432
- return Merge.make(fxs, strategies.Ordered(concurrency));
433
- });
434
- export const mergeSwitch = (fxs) => Merge.make(fxs, strategies.Switch);
435
- export function race(fxs) {
436
- if (fxs.length === 0)
437
- return empty;
438
- if (fxs.length === 1)
439
- return fxs[0];
440
- return withScopedFork(({ fork, sink }) => Effect.asyncEffect((resume) => Effect.gen(function* (_) {
441
- let winningIndex = -1;
442
- const fibers = yield* _(Effect.forEach(fxs, (fx, i) => fork(run(fx, Sink.Sink((cause) => Effect.suspend(() => pickWinner(i) ? sink.onFailure(cause) : Effect.unit), (a) => Effect.suspend(() => pickWinner(i) ? sink.onSuccess(a) : Effect.unit))))));
443
- function pickWinner(i) {
444
- if (winningIndex === -1) {
445
- winningIndex = i;
446
- resume(Effect.succeed([i, fibers]));
447
- }
448
- return winningIndex === i;
449
- }
450
- })).pipe(Effect.flatMap(([winningIndex, fibers]) => Effect.suspend(() => {
451
- const [winner] = fibers.splice(winningIndex, 1);
452
- return Effect.flatMap(Fiber.interruptAll(fibers), () => Fiber.join(winner));
453
- }))));
550
+ export function flatMapConcurrently(fx, f, capacity, executionStrategy) {
551
+ return flatMapWithStrategy(fx, f, Bounded(capacity), executionStrategy);
454
552
  }
455
- export const empty = new Empty();
456
- export const never = new Never();
457
- export const failCause = (cause) => new Fail(cause);
458
- export const fail = (e) => failCause(Cause.fail(e));
459
- export const fromSink = (f) => new FromSink((sink) => Effect.catchAllCause(f(sink), sink.onFailure));
460
- export const suspend = (f) => new Suspend(f);
461
- export const sync = (f) => new Sync(f);
462
- export const slice = dual(3, function slice(fx, skip, take) {
463
- return Slice.make(fx, boundsFrom(skip, take));
464
- });
465
- export const take = dual(2, function take(fx, n) {
466
- return slice(fx, 0, n);
467
- });
468
- export const drop = dual(2, function drop(fx, n) {
469
- return slice(fx, n, Infinity);
470
- });
471
- export const takeWhile = dual(2, function takeWhile(fx, predicate) {
472
- return withEarlyExit(({ sink }) => run(fx, Sink.WithContext(sink.onFailure, (a) => Effect.matchCauseEffect(predicate(a), {
473
- onFailure: sink.onFailure,
474
- onSuccess: (b) => b ? sink.onSuccess(a) : sink.earlyExit
475
- }))));
476
- });
477
- export const takeUntil = dual(2, function takeUntil(fx, predicate) {
478
- return takeWhile(fx, (a) => Effect.map(predicate(a), (x) => !x));
479
- });
480
- export const dropWhile = dual(2, function dropWhile(fx, predicate) {
481
- return withEarlyExit(({ sink }) => Effect.suspend(() => {
482
- let isDropping = true;
483
- return run(fx, Sink.WithContext(sink.onFailure, (a) => isDropping ?
484
- Effect.matchCauseEffect(predicate(a), {
485
- onFailure: sink.onFailure,
486
- onSuccess: (b) => {
487
- if (b) {
488
- return Effect.unit;
489
- }
490
- else {
491
- isDropping = false;
492
- return sink.onSuccess(a);
493
- }
494
- }
495
- }) :
496
- sink.onSuccess(a)));
497
- }));
498
- });
499
- export const dropUntil = dual(2, function dropUntil(fx, predicate) {
500
- return dropWhile(fx, (a) => Effect.map(predicate(a), (x) => !x));
501
- });
502
- export const dropAfter = dual(2, function dropAfter(fx, predicate) {
503
- return fromSink((sink) => Effect.suspend(() => {
504
- let isDropping = false;
505
- return run(fx, Sink.WithContext(sink.onFailure, (a) => isDropping ?
506
- Effect.unit :
507
- Effect.matchCauseEffect(predicate(a), {
508
- onFailure: sink.onFailure,
509
- onSuccess: (b) => {
510
- if (b) {
511
- isDropping = true;
512
- return Effect.unit;
513
- }
514
- else {
515
- return sink.onSuccess(a);
516
- }
517
- }
518
- })));
519
- }));
520
- });
521
- export const continueWith = dual(2, function continueWith(fx, f) {
522
- return fromSink((sink) => Effect.flatMap(run(fx, sink), () => run(f(), sink)));
523
- });
524
- export const recoverWith = dual(2, function recoverWith(fx, f) {
525
- return fromSink((sink) => Effect.catchAllCause(observe(fx, sink.onSuccess), (cause) => run(from(f(cause)), sink)));
526
- });
527
- export const mapEffect = dual(2, function mapEffect(fx, f) {
528
- return TransformerEffect.make(fx, MapEffect(f));
529
- });
530
- export const tap = dual(2, function tap(fx, f) {
531
- return TransformerEffect.make(fx, TapEffect(f));
532
- });
533
- export const filterEffect = dual(2, function filterEffect(fx, predicate) {
534
- return TransformerEffect.make(fx, FilterEffect(predicate));
535
- });
536
- export const filterMapEffect = dual(2, function filterEffect(fx, f) {
537
- return TransformerEffect.make(fx, FilterMapEffect(f));
538
- });
539
- export const middleware = dual((args) => args.length === 3 || typeof args[0] !== "function", function middleware(fx, f, g) {
540
- return Middleware.make(fx, f, g ?? identity);
541
- });
542
- export const loop = dual(3, function loop(fx, seed, f) {
543
- return Loop.make(fx, seed, f);
544
- });
545
- export const loopEffect = dual(3, function loopEffect(fx, seed, f) {
546
- return LoopEffect.make(fx, f, seed);
547
- });
548
- export const startWith = dual(2, function startWith(fx, value) {
549
- return fromSink((sink) => Effect.flatMap(sink.onSuccess(value), () => run(fx, sink)));
550
- });
551
- export const endWith = dual(2, function endWith(fx, value) {
552
- return fromSink((sink) => Effect.flatMap(run(fx, sink), () => sink.onSuccess(value)));
553
- });
554
- export const scan = dual(3, function loop(fx, seed, f) {
555
- return continueWith(new Succeed(seed), () => Loop.make(fx, seed, (b, a) => {
556
- const b2 = f(b, a);
557
- return [b2, b2];
558
- }));
559
- });
560
- export const scanEffect = dual(3, function loopEffect(fx, seed, f) {
561
- return continueWith(new Succeed(seed), () => LoopEffect.make(fx, (b, a) => Effect.map(f(b, a), (b2) => [b2, b2]), seed));
562
- });
563
- export const flatMapCauseWithStrategy = dual(3, function flatMapCause(fx, f, strategy) {
564
- return new WithFlattenStrategy(({ fork, sink }) => run(fx, Sink.WithContext((cause) => fork(run(from(f(cause)), sink)), sink.onSuccess)), strategy);
565
- });
566
- export const flatMapErrorWithStrategy = dual(3, function flatMapCause(fx, f, strategy) {
567
- return new WithFlattenStrategy(({ fork, sink }) => run(fx, Sink.WithContext((cause) => fork(run(from(cause.pipe(Cause.failureOrCause, Either.match({
568
- onLeft: f,
569
- onRight: failCause
570
- }))), sink)), sink.onSuccess)), strategy);
571
- });
572
- export const flatMapCause = dual(2, function flatMapCause(fx, f) {
573
- return flatMapCauseWithStrategy(fx, f, strategies.Unbounded);
574
- });
575
- export const flatMapError = dual(2, function flatMapError(fx, f) {
576
- return flatMapErrorWithStrategy(fx, f, strategies.Unbounded);
577
- });
578
- export const flatMapCauseConcurrently = dual(3, function flatMapCauseConcurrently(fx, f, concurrency) {
579
- return flatMapCauseWithStrategy(fx, f, strategies.Bounded(concurrency));
580
- });
581
- export const flatMapErrorConcurrently = dual(3, function flatMapCauseConcurrently(fx, f, concurrency) {
582
- return flatMapErrorWithStrategy(fx, f, strategies.Bounded(concurrency));
583
- });
584
- export const switchMapCause = dual(2, function switchMapCause(fx, f) {
585
- return flatMapCauseWithStrategy(fx, f, strategies.Switch);
586
- });
587
- export const switchMapError = dual(2, function switchMapError(fx, f) {
588
- return flatMapErrorWithStrategy(fx, f, strategies.Switch);
589
- });
590
- export const exhaustMapCause = dual(2, function exhaustMapCause(fx, f) {
591
- return flatMapCauseWithStrategy(fx, f, strategies.Exhaust);
592
- });
593
- export const exhaustMapError = dual(2, function switchMapError(fx, f) {
594
- return flatMapErrorWithStrategy(fx, f, strategies.Exhaust);
595
- });
596
- export const exhaustMapLatestCause = dual(2, function exhaustMapLatestCause(fx, f) {
597
- return flatMapCauseWithStrategy(fx, f, strategies.ExhaustLatest);
598
- });
599
- export const exhaustMapLatestError = dual(2, function switchMapError(fx, f) {
600
- return flatMapErrorWithStrategy(fx, f, strategies.ExhaustLatest);
601
- });
602
- export const matchCauseWithStrategy = dual(2, function flatMapCause(fx, options) {
603
- return new WithFlattenStrategy(({ fork, sink }) => run(fx, Sink.WithContext((cause) => fork(run(from(options.onFailure(cause)), sink)), (a) => fork(run(from(options.onSuccess(a)), sink)))), options.strategy);
604
- });
605
- export const matchErrorWithStrategy = dual(2, function flatMapCause(fx, options) {
606
- return new WithFlattenStrategy(({ fork, sink }) => run(fx, Sink.WithContext((cause) => fork(run(from(cause.pipe(Cause.failureOrCause, Either.match({
607
- onLeft: options.onFailure,
608
- onRight: failCause
609
- }))), sink)), (a) => fork(run(from(options.onSuccess(a)), sink)))), options.strategy);
610
- });
611
- export const matchCause = dual(2, function matchCause(fx, options) {
612
- return matchCauseWithStrategy(fx, {
613
- ...options,
614
- strategy: strategies.Unbounded
615
- });
616
- });
617
- export const match = dual(2, function matchCause(fx, options) {
618
- return matchErrorWithStrategy(fx, { ...options, strategy: strategies.Unbounded });
619
- });
620
- export const matchCauseConcurrently = dual(2, function matchCause(fx, options) {
621
- return matchCauseWithStrategy(fx, {
622
- onFailure: options.onFailure,
623
- onSuccess: options.onSuccess,
624
- strategy: strategies.Bounded(options.concurrency)
625
- });
626
- });
627
- export const matchErrorConcurrently = dual(2, function matchErrorConcurrently(fx, options) {
628
- return matchErrorWithStrategy(fx, { ...options, strategy: strategies.Bounded(options.concurrency) });
629
- });
630
- export const switchMatchCause = dual(2, function matchCause(fx, options) {
631
- return matchCauseWithStrategy(fx, {
632
- ...options,
633
- strategy: strategies.Switch
634
- });
635
- });
636
- export const switchMatch = dual(2, function matchCause(fx, options) {
637
- return matchErrorWithStrategy(fx, { ...options, strategy: strategies.Switch });
638
- });
639
- export const exhaustMatchCause = dual(2, function matchCause(fx, options) {
640
- return matchCauseWithStrategy(fx, {
641
- ...options,
642
- strategy: strategies.Exhaust
643
- });
644
- });
645
- export const exhaustMatch = dual(2, function matchCause(fx, options) {
646
- return matchErrorWithStrategy(fx, { ...options, strategy: strategies.Exhaust });
647
- });
648
- export const exhaustLatestMatchCause = dual(2, function matchCause(fx, options) {
649
- return matchCauseWithStrategy(fx, {
650
- ...options,
651
- strategy: strategies.ExhaustLatest
652
- });
653
- });
654
- export const exhaustLatestMatch = dual(2, function matchCause(fx, options) {
655
- return matchErrorWithStrategy(fx, { ...options, strategy: strategies.ExhaustLatest });
656
- });
657
- export const withEarlyExit = (f) => new WithEarlyExit(f);
658
- export const withScopedFork = (f) => new WithScopedFork(f);
659
- export const withFlattenStrategy = (f, strategy) => new WithFlattenStrategy(f, strategy);
660
- export const during = dual(2, function during(fx, window) {
661
- return withEarlyExit(({ fork, sink }) => Effect.suspend(() => {
662
- let taking = false;
663
- return Effect.flatMap(fork(run(take(window, 1), Sink.WithContext(sink.onFailure, (nested) => {
664
- taking = true;
665
- return fork(run(take(nested, 1), Sink.WithContext(sink.onFailure, () => sink.earlyExit)));
666
- }))), () => adjustTime(1).pipe(Effect.zipRight(run(fx, Sink.Sink(sink.onFailure, (a) => taking ? sink.onSuccess(a) : Effect.unit)))));
667
- }));
668
- });
669
- export const since = dual(2, function since(fx, window) {
670
- return during(fx, map(window, () => never));
671
- });
672
- export const until = dual(2, function until(fx, window) {
673
- return during(fx, succeed(window));
674
- });
675
- export const fromScheduled = dual(2, function fromScheduled(fx, scheduled) {
676
- return fromSink((sink) => Effect.catchAllCause(Effect.repeat(Effect.matchCauseEffect(fx, sink), scheduled), sink.onFailure));
677
- });
678
- export const periodic = dual(2, function periodic(fx, duration) {
679
- return fromScheduled(fx, Schedule.spaced(duration));
680
- });
681
- export const provide = dual(2, function provideContext(fx, context) {
682
- if (Layer.isLayer(context)) {
683
- return FxProvide.make(fx, Provide.ProvideLayer(context));
684
- }
685
- else if (isContext(context)) {
686
- return FxProvide.make(fx, Provide.ProvideContext(context));
687
- }
688
- else {
689
- return FxProvide.make(fx, Provide.ProvideRuntime(context));
690
- }
691
- });
692
- export const provideService = dual(3, function provideService(fx, tag, service) {
693
- return FxProvide.make(fx, Provide.ProvideService(tag, service));
694
- });
695
- export const provideServiceEffect = dual(3, function provideService(fx, tag, service) {
696
- return FxProvide.make(fx, Provide.ProvideServiceEffect(tag, service));
697
- });
698
- export const skipRepeatsWith = dual(2, function skipRepeatsWith(fx, eq) {
699
- return FilterMapLoop.make(fx, Option.none(), (previous, a) => Option.match(previous, {
700
- onNone: () => Option.some([a, Option.some(a)]),
701
- onSome: (prev) => eq(a, prev) ? Option.none() : Option.some([a, Option.some(a)])
702
- }));
703
- });
704
- export const skipRepeats = (fx) => skipRepeatsWith(fx, Equal.equals);
705
- class Reduce extends Effectable.Class {
706
- fx;
707
- seed;
708
- f;
709
- constructor(fx, seed, f) {
710
- super();
711
- this.fx = fx;
712
- this.seed = seed;
713
- this.f = f;
714
- }
715
- static make(fx, seed, f) {
716
- if (fx instanceof Transformer) {
717
- return FilterMapReduce.make(fx.i0, seed, compileSyncReducer(fx.i1, f));
718
- }
719
- return new Reduce(fx, seed, f);
720
- }
721
- cached = null;
722
- commit() {
723
- return (this.cached ||= Effect.suspend(() => {
724
- let acc = this.seed;
725
- return Effect.map(observe(this.fx, (a) => Effect.sync(() => acc = this.f(acc, a))), () => acc);
726
- }));
727
- }
553
+ export function concatMap(fx, f, executionStrategy) {
554
+ return flatMapConcurrently(fx, f, 1, executionStrategy);
555
+ }
556
+ export function flatMapConcurrentlyEffect(fx, f, capacity, executionStrategy) {
557
+ return flatMapConcurrently(fx, (a) => fromEffect(f(a)), capacity, executionStrategy);
728
558
  }
729
- class FilterMapReduce extends Effectable.Class {
559
+ export function flatMap(fx, f, executionStrategy) {
560
+ return flatMapWithStrategy(fx, f, Unbounded, executionStrategy);
561
+ }
562
+ export function flatMapEffect(fx, f, executionStrategy) {
563
+ return flatMap(fx, (a) => fromEffect(f(a)), executionStrategy);
564
+ }
565
+ class FlatMapWithStrategy extends FxBase {
730
566
  i0;
731
567
  i1;
732
568
  i2;
733
- constructor(i0, i1, i2) {
569
+ i3;
570
+ withFork;
571
+ constructor(i0, i1, i2, i3) {
734
572
  super();
735
573
  this.i0 = i0;
736
574
  this.i1 = i1;
737
575
  this.i2 = i2;
576
+ this.i3 = i3;
577
+ this.withFork = withFlattenStrategy(i2);
738
578
  }
739
- static make(fx, seed, f) {
740
- if (fx instanceof FromIterable) {
741
- if (Array.isArray(fx.i0)) {
742
- return new OnceEffect(Effect.sync(() => reduceFilterArray(fx.i0, seed, f)));
579
+ run(sink) {
580
+ return this.withFork((fork) => Sink.withEarlyExit(sink, (sink) => this.i0.run(Sink.make((cause) => Cause.isInterruptedOnly(cause) ? sink.earlyExit : sink.onFailure(cause), (a) => fork(this.i1(a).run(sink))))), this.i3);
581
+ }
582
+ static make(fx, f, strategy, executionStrategy) {
583
+ if (isEmpty(fx) || isNever(fx))
584
+ return fx;
585
+ else if (isProducer(fx)) {
586
+ if (fx.i0._tag === "Success")
587
+ return f(fx.i0.source);
588
+ if (fx.i0._tag === "FromSync") {
589
+ const producer = fx.i0;
590
+ return suspend(() => f(producer.source()));
743
591
  }
744
- else {
745
- return Effect.sync(() => reduceFilterIterable(fx.i0, seed, f));
592
+ const arr = Array.isArray(fx.i0.source) ? fx.i0.source : Array.from(fx.i0.source);
593
+ if (arr.length === 0)
594
+ return empty;
595
+ if (arr.length === 1)
596
+ return f(arr[0]);
597
+ switch (strategy._tag) {
598
+ case "Switch":
599
+ return f(arr[arr.length - 1]);
600
+ case "Exhaust":
601
+ return f(arr[0]);
602
+ case "ExhaustLatest":
603
+ return arr.length > 1 ? continueWith(f(arr[0]), () => f(arr[arr.length - 1])) : f(arr[0]);
604
+ default:
605
+ return new FlatMapWithStrategy(fx, f, strategy, executionStrategy);
746
606
  }
747
607
  }
748
- return new FilterMapReduce(fx, seed, f);
749
- }
750
- cached = null;
751
- commit() {
752
- return this.cached ||= Effect.suspend(() => {
753
- let acc = this.i1;
754
- return Effect.map(observe(this.i0, (a) => Option.match(this.i2(acc, a), {
755
- onNone: () => Effect.unit,
756
- onSome: (b) => Effect.succeed(acc = b)
757
- })), () => acc);
608
+ else if (isProducerEffect(fx) && fx.i0._tag === "FromEffect") {
609
+ return fromFxEffect(Effect.map(fx.i0.source, f));
610
+ }
611
+ else if (isTransformer(fx) && fx.i1._tag === "Map") {
612
+ const { f: op } = fx.i1;
613
+ return new FlatMapWithStrategy(fx.i0, (a) => f(op(a)), strategy, executionStrategy);
614
+ }
615
+ else {
616
+ return new FlatMapWithStrategy(fx, f, strategy, executionStrategy);
617
+ }
618
+ }
619
+ }
620
+ export function fromFxEffect(effect) {
621
+ return new FromFxEffect(effect);
622
+ }
623
+ class FromFxEffect extends FxBase {
624
+ i0;
625
+ constructor(i0) {
626
+ super();
627
+ this.i0 = i0;
628
+ }
629
+ run(sink) {
630
+ return Effect.matchCauseEffect(this.i0, {
631
+ onFailure: (cause) => sink.onFailure(cause),
632
+ onSuccess: (fx) => fx.run(sink)
758
633
  });
759
634
  }
760
635
  }
761
- function reduceFilterIterable(iterable, seed, f) {
762
- const iterator = iterable[Symbol.iterator]();
763
- let acc = seed;
764
- let result = iterator.next();
765
- let option = Option.none();
766
- while (!result.done) {
767
- option = f(acc, result.value);
768
- if (Option.isSome(option)) {
769
- acc = option.value;
636
+ export function gen(f) {
637
+ return fromFxEffect(Effect.gen(f));
638
+ }
639
+ export function genScoped(f) {
640
+ return scoped(fromFxEffect(Effect.gen(f)));
641
+ }
642
+ export function continueWith(fx, f) {
643
+ return ContinueWith.make(fx, f);
644
+ }
645
+ class ContinueWith extends FxBase {
646
+ i0;
647
+ i1;
648
+ constructor(i0, i1) {
649
+ super();
650
+ this.i0 = i0;
651
+ this.i1 = i1;
652
+ }
653
+ run(sink) {
654
+ return Effect.flatMap(this.i0.run(sink), () => this.i1().run(sink));
655
+ }
656
+ static make(fx, f) {
657
+ if (isEmpty(fx))
658
+ return f();
659
+ else if (isNever(fx))
660
+ return fx;
661
+ else if (isProducer(fx)) {
662
+ return SyncProducer.matchSyncProducer(fx.i0, {
663
+ Success: (source) => prependAll(f(), [source]),
664
+ FromSync: (source) => suspend(() => prependAll(f(), [source()])),
665
+ FromArray: (source) => prependAll(f(), source),
666
+ FromIterable: (source) => prependAll(f(), source)
667
+ });
668
+ }
669
+ else {
670
+ return new ContinueWith(fx, f);
671
+ }
672
+ }
673
+ }
674
+ export function orElseCause(fx, f) {
675
+ return OrElseCause.make(fx, f);
676
+ }
677
+ class OrElseCause extends FxBase {
678
+ i0;
679
+ i1;
680
+ constructor(i0, i1) {
681
+ super();
682
+ this.i0 = i0;
683
+ this.i1 = i1;
684
+ }
685
+ run(sink) {
686
+ return Effect.catchAllCause(observe(this.i0, sink.onSuccess), (cause) => this.i1(cause).run(sink));
687
+ }
688
+ static make(fx, f) {
689
+ if (isEmpty(fx))
690
+ return fx;
691
+ else if (isNever(fx))
692
+ return fx;
693
+ else {
694
+ return new OrElseCause(fx, f);
770
695
  }
771
- result = iterator.next();
772
696
  }
773
- return acc;
774
697
  }
775
- function reduceFilterArray(iterable, seed, f) {
776
- const length = iterable.length;
777
- let acc = seed;
778
- let option = Option.none();
779
- for (let i = 0; i < length; i++) {
780
- option = f(acc, iterable[i]);
781
- if (Option.isSome(option)) {
782
- acc = option.value;
698
+ export function orElse(fx, f) {
699
+ return OrElse.make(fx, f);
700
+ }
701
+ class OrElse extends FxBase {
702
+ i0;
703
+ i1;
704
+ constructor(i0, i1) {
705
+ super();
706
+ this.i0 = i0;
707
+ this.i1 = i1;
708
+ }
709
+ run(sink) {
710
+ return Effect.catchAll(Effect.asyncEffect((resume) => Effect.zipRight(this.i0.run(Sink.make((cause) => Either.match(Cause.failureOrCause(cause), {
711
+ onLeft: (e) => Effect.succeed(resume(Effect.fail(e))),
712
+ onRight: (cause) => sink.onFailure(cause)
713
+ }), sink.onSuccess)), Effect.sync(() => resume(Effect.unit)))), (error) => this.i1(error).run(sink));
714
+ }
715
+ static make(fx, f) {
716
+ if (isEmpty(fx))
717
+ return fx;
718
+ else if (isNever(fx))
719
+ return fx;
720
+ else {
721
+ return new OrElse(fx, f);
783
722
  }
784
723
  }
785
- return acc;
786
724
  }
787
- export const reduce = dual(3, function reduce(fx, seed, f) {
788
- return Reduce.make(fx, seed, f);
789
- });
790
- export function toChunk(fx) {
791
- return reduce(fx, Chunk.empty(), (Chunk.append));
725
+ export function suspend(f) {
726
+ return new Suspend(f);
792
727
  }
793
- export const snapshot = dual(3, function snapshot(fx, sampled, f) {
794
- return new Snapshot(fx, sampled, f);
795
- });
796
- export function fromEmitter(f) {
797
- return withEarlyExit(({ scope, sink }) => Effect.zipRight(Effect.provideService(Effect.flatMap(Emitter.make(sink), f), Scope.Scope, scope), Effect.never));
798
- }
799
- export function fromEffect(effect) {
800
- return matchEffectPrimitive(effect, {
801
- Success: (e) => succeed(e.i0),
802
- Failure: (e) => failCause(e.i0),
803
- Left: (e) => fail(e.left),
804
- Right: (e) => succeed(e.right),
805
- None: () => fail(new Cause.NoSuchElementException()),
806
- Some: (e) => succeed(e.value),
807
- Sync: (e) => sync(e.i0),
808
- Otherwise: () => fromSink((sink) => Effect.matchCauseEffect(effect, sink))
728
+ class Suspend extends FxBase {
729
+ i0;
730
+ constructor(i0) {
731
+ super();
732
+ this.i0 = i0;
733
+ }
734
+ run(sink) {
735
+ return this.i0().run(sink);
736
+ }
737
+ }
738
+ function isSuspend(fx) {
739
+ return fx.constructor === Suspend;
740
+ }
741
+ class SuspendedTransformer extends FxBase {
742
+ i0;
743
+ i1;
744
+ constructor(i0, i1) {
745
+ super();
746
+ this.i0 = i0;
747
+ this.i1 = i1;
748
+ }
749
+ run(sink) {
750
+ return this.i0().run(Op.compileOperatorSink(this.i1, sink));
751
+ }
752
+ }
753
+ function isSuspendedTransformer(fx) {
754
+ return fx.constructor === SuspendedTransformer;
755
+ }
756
+ export function mergeWithStrategy(fx, stategy) {
757
+ return MergeWithStrategy.make(fx, stategy);
758
+ }
759
+ export function merge(fx, other) {
760
+ return mergeWithStrategy([fx, other], Unordered(2));
761
+ }
762
+ export function mergeAll(fx) {
763
+ return mergeWithStrategy(fx, Unordered(Infinity));
764
+ }
765
+ export function mergeOrdered(fx) {
766
+ return mergeOrderedConcurrently(fx, Infinity);
767
+ }
768
+ export function mergeOrderedConcurrently(fx, concurrency) {
769
+ return mergeWithStrategy(fx, Ordered(concurrency));
770
+ }
771
+ export function mergeSwitch(fx) {
772
+ return mergeWithStrategy(fx, Switch);
773
+ }
774
+ class MergeWithStrategy extends FxBase {
775
+ i0;
776
+ i1;
777
+ constructor(i0, i1) {
778
+ super();
779
+ this.i0 = i0;
780
+ this.i1 = i1;
781
+ }
782
+ run(sink) {
783
+ switch (this.i1._tag) {
784
+ case "Unordered":
785
+ return runUnordered(this.i0, sink, this.i1.concurrency === Infinity ? "unbounded" : this.i1.concurrency);
786
+ case "Ordered":
787
+ return runOrdered(this.i0, sink, this.i1.concurrency === Infinity ? "unbounded" : this.i1.concurrency);
788
+ case "Switch":
789
+ return runSwitch(this.i0, sink);
790
+ }
791
+ }
792
+ static make(fx, strategy) {
793
+ if (fx.length === 0)
794
+ return empty;
795
+ else if (fx.length === 1)
796
+ return fx[0];
797
+ else
798
+ return new MergeWithStrategy(fx.filter(Predicate.not(isEmpty)), strategy);
799
+ }
800
+ }
801
+ function runUnordered(fx, sink, concurrency) {
802
+ return Effect.forEach(fx, (fx) => fx.run(sink), { concurrency, discard: true });
803
+ }
804
+ function runOrdered(fx, sink, concurrency) {
805
+ return Effect.suspend(() => {
806
+ const buffers = withBuffers(fx.length, sink);
807
+ return Effect.all(fx.map((fx, i) => Effect.flatMap(fx.run(Sink.make((cause) => Cause.isInterruptedOnly(cause) ? Effect.unit : sink.onFailure(cause), (a) => buffers.onSuccess(i, a))), () => buffers.onEnd(i))), {
808
+ concurrency
809
+ });
809
810
  });
810
811
  }
811
- export function fromStream(stream, options) {
812
- return fromSink((sink) => Effect.acquireUseRelease(Scope.make(), (scope) => Effect.gen(function* (_) {
813
- const pull = Effect.either(yield* _(Stream.toPull(stream), Effect.provideService(Scope.Scope, scope)));
814
- while (true) {
815
- const either = yield* _(pull);
816
- if (Either.isRight(either)) {
817
- yield* _(Effect.forEach(either.right, sink.onSuccess));
818
- }
819
- else if (Option.isNone(either.left)) {
820
- return;
821
- }
822
- else {
823
- yield* _(sink.onFailure(Cause.fail(either.left.value)));
812
+ function runSwitch(fx, sink) {
813
+ return Effect.forEach(fx, (fx) => fx.run(sink), { concurrency: 1, discard: true });
814
+ }
815
+ export function takeWhile(fx, f) {
816
+ return TakeWhile.make(fx, f);
817
+ }
818
+ export function takeUntil(fx, f) {
819
+ return TakeWhile.make(fx, Predicate.not(f));
820
+ }
821
+ class TakeWhile extends FxBase {
822
+ i0;
823
+ i1;
824
+ constructor(i0, i1) {
825
+ super();
826
+ this.i0 = i0;
827
+ this.i1 = i1;
828
+ }
829
+ run(sink) {
830
+ return Sink.takeWhile(sink, this.i1, (s) => this.i0.run(s));
831
+ }
832
+ static make(fx, predicate) {
833
+ if (isEmpty(fx) || isNever(fx))
834
+ return fx;
835
+ else {
836
+ return new TakeWhile(fx, predicate);
837
+ }
838
+ }
839
+ }
840
+ export function dropWhile(fx, f) {
841
+ return DropUntil.make(fx, f);
842
+ }
843
+ export function dropUntil(fx, f) {
844
+ return DropUntil.make(fx, Predicate.not(f));
845
+ }
846
+ class DropUntil extends FxBase {
847
+ i0;
848
+ i1;
849
+ constructor(i0, i1) {
850
+ super();
851
+ this.i0 = i0;
852
+ this.i1 = i1;
853
+ }
854
+ run(sink) {
855
+ return this.i0.run(Sink.dropWhile(sink, this.i1));
856
+ }
857
+ static make(fx, predicate) {
858
+ if (isEmpty(fx) || isNever(fx))
859
+ return fx;
860
+ else {
861
+ return new DropUntil(fx, predicate);
862
+ }
863
+ }
864
+ }
865
+ export function dropAfter(fx, f) {
866
+ return DropAfter.make(fx, f);
867
+ }
868
+ class DropAfter extends FxBase {
869
+ i0;
870
+ i1;
871
+ constructor(i0, i1) {
872
+ super();
873
+ this.i0 = i0;
874
+ this.i1 = i1;
875
+ }
876
+ run(sink) {
877
+ return this.i0.run(Sink.dropAfter(sink, this.i1));
878
+ }
879
+ static make(fx, predicate) {
880
+ if (isEmpty(fx) || isNever(fx))
881
+ return fx;
882
+ else {
883
+ return new DropAfter(fx, predicate);
884
+ }
885
+ }
886
+ }
887
+ export function takeWhileEffect(fx, f) {
888
+ return TakeWhileEffect.make(fx, f);
889
+ }
890
+ export function takeUntilEffect(fx, f) {
891
+ return TakeWhileEffect.make(fx, (a) => Effect.map(f(a), Boolean.not));
892
+ }
893
+ class TakeWhileEffect extends FxBase {
894
+ i0;
895
+ i1;
896
+ constructor(i0, i1) {
897
+ super();
898
+ this.i0 = i0;
899
+ this.i1 = i1;
900
+ }
901
+ run(sink) {
902
+ return Sink.takeWhileEffect(sink, this.i1, (s) => this.i0.run(s));
903
+ }
904
+ static make(fx, f) {
905
+ if (isEmpty(fx) || isNever(fx))
906
+ return fx;
907
+ else {
908
+ return new TakeWhileEffect(fx, f);
909
+ }
910
+ }
911
+ }
912
+ export function dropWhileEffect(fx, f) {
913
+ return DropWhileEffect.make(fx, f);
914
+ }
915
+ export function dropUntilEffect(fx, f) {
916
+ return DropWhileEffect.make(fx, (a) => Effect.map(f(a), Boolean.not));
917
+ }
918
+ class DropWhileEffect extends FxBase {
919
+ i0;
920
+ i1;
921
+ constructor(i0, i1) {
922
+ super();
923
+ this.i0 = i0;
924
+ this.i1 = i1;
925
+ }
926
+ run(sink) {
927
+ return this.i0.run(Sink.dropWhileEffect(sink, this.i1));
928
+ }
929
+ static make(fx, f) {
930
+ if (isEmpty(fx) || isNever(fx))
931
+ return fx;
932
+ else {
933
+ return new DropWhileEffect(fx, f);
934
+ }
935
+ }
936
+ }
937
+ export function dropAfterEffect(fx, f) {
938
+ return DropAfterEffect.make(fx, f);
939
+ }
940
+ class DropAfterEffect extends FxBase {
941
+ i0;
942
+ i1;
943
+ constructor(i0, i1) {
944
+ super();
945
+ this.i0 = i0;
946
+ this.i1 = i1;
947
+ }
948
+ run(sink) {
949
+ return this.i0.run(Sink.dropAfterEffect(sink, this.i1));
950
+ }
951
+ static make(fx, f) {
952
+ if (isEmpty(fx) || isNever(fx))
953
+ return fx;
954
+ else {
955
+ return new DropAfterEffect(fx, f);
956
+ }
957
+ }
958
+ }
959
+ export function during(fx, window) {
960
+ return During.make(fx, window);
961
+ }
962
+ export function since(fx, window) {
963
+ return During.make(fx, map(window, () => never));
964
+ }
965
+ export function until(fx, window) {
966
+ return During.make(fx, succeed(window));
967
+ }
968
+ class During extends FxBase {
969
+ i0;
970
+ i1;
971
+ constructor(i0, i1) {
972
+ super();
973
+ this.i0 = i0;
974
+ this.i1 = i1;
975
+ }
976
+ run(sink) {
977
+ return withScopedFork((fork) => Sink.withEarlyExit(sink, (s) => {
978
+ let taking = false;
979
+ const onFailure = (cause) => s.onFailure(cause);
980
+ return Effect.flatMap(fork(take(this.i1, 1).run(Sink.make(onFailure, (nested) => {
981
+ taking = true;
982
+ return take(nested, 1).run(Sink.make(onFailure, () => s.earlyExit));
983
+ }))), () => Effect.zipRight(
984
+ // Allow fibers to start
985
+ adjustTime(1), this.i0.run(Sink.make(onFailure, (a) => taking ? s.onSuccess(a) : Effect.unit))));
986
+ }), ExecutionStrategy.sequential);
987
+ }
988
+ static make(fx, window) {
989
+ if (isEmpty(fx) || isNever(fx))
990
+ return fx;
991
+ else {
992
+ return new During(fx, window);
993
+ }
994
+ }
995
+ }
996
+ export function middleware(fx, effect, sink) {
997
+ return new Middleware(fx, effect, sink);
998
+ }
999
+ export function onExit(fx, f) {
1000
+ return middleware(fx, Effect.onExit(f));
1001
+ }
1002
+ export function onInterrupt(fx, f) {
1003
+ return middleware(fx, Effect.onInterrupt(f));
1004
+ }
1005
+ export function onError(fx, f) {
1006
+ return middleware(fx, Effect.onError(f));
1007
+ }
1008
+ export const scoped = (fx) => middleware(fx, Effect.scoped);
1009
+ export function annotateLogs(fx, key, value) {
1010
+ return middleware(fx, (effect) => Effect.annotateLogs(effect, key, value));
1011
+ }
1012
+ export function annotateSpans(fx, key, value) {
1013
+ return middleware(fx, (effect) => Effect.annotateSpans(effect, key, value));
1014
+ }
1015
+ export const interruptible = (fx) => middleware(fx, Effect.interruptible);
1016
+ export const uninterruptible = (fx) => middleware(fx, Effect.uninterruptible);
1017
+ export function locally(use, self, value) {
1018
+ return middleware(use, (effect) => Effect.locally(effect, self, value));
1019
+ }
1020
+ export function locallyWith(use, self, f) {
1021
+ return middleware(use, (effect) => Effect.locallyWith(effect, self, f));
1022
+ }
1023
+ export function withTracerTiming(fx, enabled) {
1024
+ return middleware(fx, (effect) => Effect.withTracerTiming(effect, enabled));
1025
+ }
1026
+ export function withConcurrency(fx, concurrency) {
1027
+ return middleware(fx, (effect) => Effect.withConcurrency(effect, concurrency));
1028
+ }
1029
+ export function withConfigProvider(fx, configProvider) {
1030
+ return middleware(fx, (effect) => Effect.withConfigProvider(effect, configProvider));
1031
+ }
1032
+ export function withLogSpan(fx, span) {
1033
+ return middleware(fx, (effect) => Effect.withLogSpan(effect, span));
1034
+ }
1035
+ export function withMaxOpsBeforeYield(fx, maxOps) {
1036
+ return middleware(fx, (effect) => Effect.withMaxOpsBeforeYield(effect, maxOps));
1037
+ }
1038
+ export function withParentSpan(fx, parentSpan) {
1039
+ return middleware(fx, (effect) => Effect.withParentSpan(effect, parentSpan));
1040
+ }
1041
+ export function withRequestBatching(fx, requestBatching) {
1042
+ return middleware(fx, (effect) => Effect.withRequestBatching(effect, requestBatching));
1043
+ }
1044
+ export function withRequestCache(fx, cache) {
1045
+ return middleware(fx, (effect) => Effect.withRequestCache(effect, cache));
1046
+ }
1047
+ export function withRequestCaching(fx, requestCaching) {
1048
+ return middleware(fx, (effect) => Effect.withRequestCaching(effect, requestCaching));
1049
+ }
1050
+ export function withScheduler(fx, scheduler) {
1051
+ return middleware(fx, (effect) => Effect.withScheduler(effect, scheduler));
1052
+ }
1053
+ export function withTracer(fx, tracer) {
1054
+ return middleware(fx, (effect) => Effect.withTracer(effect, tracer));
1055
+ }
1056
+ class Middleware extends FxBase {
1057
+ i0;
1058
+ i1;
1059
+ i2;
1060
+ constructor(i0, i1, i2) {
1061
+ super();
1062
+ this.i0 = i0;
1063
+ this.i1 = i1;
1064
+ this.i2 = i2;
1065
+ }
1066
+ run(sink) {
1067
+ return Effect.contextWithEffect((ctx) => {
1068
+ const s = Sink.provide(sink, ctx);
1069
+ return this.i1(this.i0.run(this.i2 ? this.i2(s) : s));
1070
+ });
1071
+ }
1072
+ }
1073
+ export function acquireUseRelease(acquire, use, release) {
1074
+ return new AcquireUseRelease(acquire, use, release);
1075
+ }
1076
+ class AcquireUseRelease extends FxBase {
1077
+ acquire;
1078
+ use;
1079
+ release;
1080
+ constructor(acquire, use, release) {
1081
+ super();
1082
+ this.acquire = acquire;
1083
+ this.use = use;
1084
+ this.release = release;
1085
+ }
1086
+ run(sink) {
1087
+ return Effect.catchAllCause(Effect.acquireUseRelease(this.acquire, (a) => this.use(a).run(sink), (a, exit) => Effect.catchAllCause(this.release(a, exit), (cause) => sink.onFailure(cause))), (cause) => sink.onFailure(cause));
1088
+ }
1089
+ }
1090
+ export function withSpan(self, name, options = {}) {
1091
+ return acquireUseRelease(Effect.flatMap(Effect.optionFromOptional(Effect.currentSpan), (parent) => Effect.makeSpan(name, { ...options, parent: options?.parent || Option.getOrUndefined(parent) })), (span) => middleware(self, (effect) => Effect.provideService(effect, Tracer.ParentSpan, span), (s) => Sink.setSpan(s, span)), (span, exit) => Effect.flatMap(Clock.currentTimeNanos, (time) => Effect.sync(() => span.end(time, exit))));
1092
+ }
1093
+ export function provideContext(fx, context) {
1094
+ return ProvideFx.make(fx, Provide.ProvideContext(context));
1095
+ }
1096
+ export function provideLayer(fx, layer) {
1097
+ return ProvideFx.make(fx, Provide.ProvideLayer(layer));
1098
+ }
1099
+ export function provideRuntime(fx, runtime) {
1100
+ return ProvideFx.make(fx, Provide.ProvideRuntime(runtime));
1101
+ }
1102
+ export function provideService(fx, service, instance) {
1103
+ return ProvideFx.make(fx, Provide.ProvideService(service, instance));
1104
+ }
1105
+ export function provideServiceEffect(fx, service, instance) {
1106
+ return ProvideFx.make(fx, Provide.ProvideServiceEffect(service, instance));
1107
+ }
1108
+ export function provide(fx, provide) {
1109
+ if (Layer.isLayer(provide))
1110
+ return provideLayer(fx, provide);
1111
+ else if (Context.isContext(provide))
1112
+ return provideContext(fx, provide);
1113
+ else
1114
+ return provideRuntime(fx, provide);
1115
+ }
1116
+ class ProvideFx extends FxBase {
1117
+ i0;
1118
+ i1;
1119
+ constructor(i0, i1) {
1120
+ super();
1121
+ this.i0 = i0;
1122
+ this.i1 = i1;
1123
+ }
1124
+ run(sink) {
1125
+ return Effect.acquireUseRelease(Scope.make(), (scope) => Effect.matchEffect(Provide.buildWithScope(this.i1, scope), {
1126
+ onFailure: (error) => sink.onFailure(Cause.fail(error)),
1127
+ onSuccess: (ctx) => Effect.provide(this.i0.run(sink), ctx)
1128
+ }), (scope, exit) => Scope.close(scope, exit));
1129
+ }
1130
+ static make(fx, provide) {
1131
+ if (isEmpty(fx) || isNever(fx))
1132
+ return fx;
1133
+ else if (isProvideFx(fx)) {
1134
+ return new ProvideFx(fx.i0, Provide.merge(fx.i1, provide));
1135
+ }
1136
+ else {
1137
+ return new ProvideFx(fx, provide);
1138
+ }
1139
+ }
1140
+ }
1141
+ function isProvideFx(u) {
1142
+ return u.constructor === ProvideFx;
1143
+ }
1144
+ export function mapCause(fx, f) {
1145
+ return new TransformerCause(fx, SyncOp.Map(f));
1146
+ }
1147
+ export function mapError(fx, f) {
1148
+ return mapCause(fx, Cause.map(f));
1149
+ }
1150
+ export function filterCause(fx, f) {
1151
+ return new TransformerCause(fx, SyncOp.Filter(f));
1152
+ }
1153
+ export function filterError(fx, f) {
1154
+ return filterCause(fx, (cause) => Option.match(Cause.failureOption(cause), {
1155
+ onNone: constTrue,
1156
+ onSome: f
1157
+ }));
1158
+ }
1159
+ export function filterMapCause(fx, f) {
1160
+ return new TransformerCause(fx, SyncOp.FilterMap(f));
1161
+ }
1162
+ export function filterMapError(fx, f) {
1163
+ return filterMapCause(fx, (cause) => Either.match(Cause.failureOrCause(cause), {
1164
+ onLeft: (e) => Option.map(f(e), Cause.fail),
1165
+ onRight: Option.some
1166
+ }));
1167
+ }
1168
+ export function mapCauseEffect(fx, f) {
1169
+ return new TransformerCause(fx, EffectOp.MapEffect(f));
1170
+ }
1171
+ export function mapErrorEffect(fx, f) {
1172
+ return mapCauseEffect(fx, (cause) => Either.match(Cause.failureOrCause(cause), {
1173
+ onLeft: (e) => Effect.map(f(e), Cause.fail),
1174
+ onRight: (cause) => Effect.succeed(cause)
1175
+ }));
1176
+ }
1177
+ export function filterCauseEffect(fx, f) {
1178
+ return new TransformerCause(fx, EffectOp.FilterEffect(f));
1179
+ }
1180
+ export function filterErrorEffect(fx, f) {
1181
+ return filterCauseEffect(fx, (cause) => Either.match(Cause.failureOrCause(cause), {
1182
+ onLeft: f,
1183
+ onRight: () => Effect.succeed(true)
1184
+ }));
1185
+ }
1186
+ export function filterMapCauseEffect(fx, f) {
1187
+ return new TransformerCause(fx, EffectOp.FilterMapEffect(f));
1188
+ }
1189
+ export function filterMapErrorEffect(fx, f) {
1190
+ return filterMapCauseEffect(fx, (cause) => Either.match(Cause.failureOrCause(cause), {
1191
+ onLeft: (e) => Effect.map(f(e), Option.map(Cause.fail)),
1192
+ onRight: (cause) => Effect.succeed(Option.some(cause))
1193
+ }));
1194
+ }
1195
+ export function loopCause(fx, seed, f) {
1196
+ return new TransformerCause(fx, SyncLoopOp.LoopOperator(seed, f));
1197
+ }
1198
+ export function loopError(fx, seed, f) {
1199
+ return loopCause(fx, seed, (b, cause) => Either.match(Cause.failureOrCause(cause), {
1200
+ onLeft: (e) => {
1201
+ const [c, b2] = f(b, e);
1202
+ return [Cause.fail(c), b2];
1203
+ },
1204
+ onRight: (cause) => [cause, b]
1205
+ }));
1206
+ }
1207
+ export function loopCauseEffect(fx, seed, f) {
1208
+ return new TransformerCause(fx, EffectLoopOp.LoopEffectOperator(seed, f));
1209
+ }
1210
+ export function loopErrorEffect(fx, seed, f) {
1211
+ return loopCauseEffect(fx, seed, (b, cause) => Either.match(Cause.failureOrCause(cause), {
1212
+ onLeft: (e) => Effect.map(f(b, e), ([c, b2]) => [Cause.fail(c), b2]),
1213
+ onRight: (cause) => Effect.succeed([cause, b])
1214
+ }));
1215
+ }
1216
+ export function filterMapLoopCause(fx, seed, f) {
1217
+ return new TransformerCause(fx, SyncLoopOp.FilterMapLoopOperator(seed, f));
1218
+ }
1219
+ export function filterMapLoopError(fx, seed, f) {
1220
+ return filterMapLoopCause(fx, seed, (b, cause) => Either.match(Cause.failureOrCause(cause), {
1221
+ onLeft: (e) => {
1222
+ const [c, b2] = f(b, e);
1223
+ return [Option.map(Cause.fail)(c), b2];
1224
+ },
1225
+ onRight: (cause) => [Option.some(cause), b]
1226
+ }));
1227
+ }
1228
+ export function filterMapLoopCauseEffect(fx, seed, f) {
1229
+ return new TransformerCause(fx, EffectLoopOp.FilterMapLoopEffectOperator(seed, f));
1230
+ }
1231
+ export function filterMapLoopErrorEffect(fx, seed, f) {
1232
+ return filterMapLoopCauseEffect(fx, seed, (b, cause) => Either.match(Cause.failureOrCause(cause), {
1233
+ onLeft: (e) => Effect.map(f(b, e), ([c, b2]) => [Option.map(c, Cause.fail), b2]),
1234
+ onRight: (cause) => Effect.succeed([Option.some(cause), b])
1235
+ }));
1236
+ }
1237
+ class TransformerCause extends FxBase {
1238
+ i0;
1239
+ i1;
1240
+ constructor(i0, i1) {
1241
+ super();
1242
+ this.i0 = i0;
1243
+ this.i1 = i1;
1244
+ }
1245
+ run(sink) {
1246
+ return this.i0.run(Op.compileOperatorSinkCause(this.i1, sink));
1247
+ }
1248
+ }
1249
+ export function flatMapCauseWithStrategy(fx, f, flattenStrategy, executionStrategy = ExecutionStrategy.sequential) {
1250
+ return new FlatMapCauseWithStrategy(fx, f, flattenStrategy, executionStrategy);
1251
+ }
1252
+ export function flatMapErrorWithStrategy(fx, f, flattenStrategy, executionStrategy = ExecutionStrategy.sequential) {
1253
+ return flatMapCauseWithStrategy(fx, (cause) => Either.match(Cause.failureOrCause(cause), {
1254
+ onLeft: f,
1255
+ onRight: (cause) => failCause(cause)
1256
+ }), flattenStrategy, executionStrategy);
1257
+ }
1258
+ export function switchMapCause(fx, f, executionStrategy) {
1259
+ return flatMapCauseWithStrategy(fx, f, Switch, executionStrategy);
1260
+ }
1261
+ export function switchMapError(fx, f, executionStrategy) {
1262
+ return flatMapErrorWithStrategy(fx, f, Switch, executionStrategy);
1263
+ }
1264
+ export function flatMapCause(fx, f, executionStrategy) {
1265
+ return flatMapCauseWithStrategy(fx, f, Unbounded, executionStrategy);
1266
+ }
1267
+ export function flatMapError(fx, f, executionStrategy) {
1268
+ return flatMapErrorWithStrategy(fx, f, Unbounded, executionStrategy);
1269
+ }
1270
+ export function flatMapCauseConcurrently(fx, f, concurrency, executionStrategy) {
1271
+ return flatMapCauseWithStrategy(fx, f, Bounded(concurrency), executionStrategy);
1272
+ }
1273
+ export function flatMapErrorConcurrently(fx, f, concurrency, executionStrategy) {
1274
+ return flatMapErrorWithStrategy(fx, f, Bounded(concurrency), executionStrategy);
1275
+ }
1276
+ export function exhaustMapCause(fx, f, executionStrategy) {
1277
+ return flatMapCauseWithStrategy(fx, f, Exhaust, executionStrategy);
1278
+ }
1279
+ export function exhaustMapError(fx, f, executionStrategy) {
1280
+ return flatMapErrorWithStrategy(fx, f, Exhaust, executionStrategy);
1281
+ }
1282
+ export function exhaustMapLatestCause(fx, f, executionStrategy) {
1283
+ return flatMapCauseWithStrategy(fx, f, ExhaustLatest, executionStrategy);
1284
+ }
1285
+ export function exhaustMapLatestError(fx, f, executionStrategy) {
1286
+ return flatMapErrorWithStrategy(fx, f, ExhaustLatest, executionStrategy);
1287
+ }
1288
+ class FlatMapCauseWithStrategy extends FxBase {
1289
+ i0;
1290
+ i1;
1291
+ i2;
1292
+ i3;
1293
+ withFork;
1294
+ constructor(i0, i1, i2, i3) {
1295
+ super();
1296
+ this.i0 = i0;
1297
+ this.i1 = i1;
1298
+ this.i2 = i2;
1299
+ this.i3 = i3;
1300
+ this.withFork = withFlattenStrategy(this.i2);
1301
+ }
1302
+ run(sink) {
1303
+ return this.withFork((fork) => this.i0.run(Sink.make((cause) => fork(this.i1(cause).run(sink)), (a) => sink.onSuccess(a))), this.i3);
1304
+ }
1305
+ }
1306
+ class MatchWithStrategy extends FxBase {
1307
+ i0;
1308
+ i1;
1309
+ i2;
1310
+ i3;
1311
+ i4;
1312
+ withFork;
1313
+ constructor(i0, i1, i2, i3, i4) {
1314
+ super();
1315
+ this.i0 = i0;
1316
+ this.i1 = i1;
1317
+ this.i2 = i2;
1318
+ this.i3 = i3;
1319
+ this.i4 = i4;
1320
+ this.withFork = withFlattenStrategy(this.i3);
1321
+ }
1322
+ run(sink) {
1323
+ return this.withFork((fork) => this.i0.run(Sink.make((cause) => fork(this.i1(cause).run(sink)), (a) => fork(this.i2(a).run(sink)))), this.i4);
1324
+ }
1325
+ }
1326
+ export function matchCauseWithStrategy(fx, flattenStrategy, opts) {
1327
+ return new MatchWithStrategy(fx, opts.onFailure, opts.onSuccess, flattenStrategy, opts.executionStrategy || ExecutionStrategy.sequential);
1328
+ }
1329
+ export function matchErrorWithStrategy(fx, flattenStrategy, { executionStrategy, onFailure, onSuccess }) {
1330
+ return new MatchWithStrategy(fx, (cause) => Either.match(Cause.failureOrCause(cause), {
1331
+ onLeft: onFailure,
1332
+ onRight: (cause) => failCause(cause)
1333
+ }), onSuccess, flattenStrategy, executionStrategy || ExecutionStrategy.sequential);
1334
+ }
1335
+ export function matchCause(fx, opts) {
1336
+ return matchCauseWithStrategy(fx, Unbounded, opts);
1337
+ }
1338
+ export function matchError(fx, opts) {
1339
+ return matchErrorWithStrategy(fx, Unbounded, opts);
1340
+ }
1341
+ export function matchCauseConcurrently(fx, concurrency, opts) {
1342
+ return matchCauseWithStrategy(fx, Bounded(concurrency), opts);
1343
+ }
1344
+ export function matchErrorConcurrently(fx, concurrency, opts) {
1345
+ return matchErrorWithStrategy(fx, Bounded(concurrency), opts);
1346
+ }
1347
+ export function switchMatchCause(fx, opts) {
1348
+ return matchCauseWithStrategy(fx, Switch, opts);
1349
+ }
1350
+ export function switchMatchError(fx, opts) {
1351
+ return matchErrorWithStrategy(fx, Switch, opts);
1352
+ }
1353
+ export function exhaustMatchCause(fx, opts) {
1354
+ return matchCauseWithStrategy(fx, Exhaust, opts);
1355
+ }
1356
+ export function exhaustMatchError(fx, opts) {
1357
+ return matchErrorWithStrategy(fx, Exhaust, opts);
1358
+ }
1359
+ export function exhaustMatchLatestCause(fx, opts) {
1360
+ return matchCauseWithStrategy(fx, ExhaustLatest, opts);
1361
+ }
1362
+ export function exhaustMatchLatestError(fx, opts) {
1363
+ return matchErrorWithStrategy(fx, ExhaustLatest, opts);
1364
+ }
1365
+ export function tuple(fx) {
1366
+ return new Tuple(fx);
1367
+ }
1368
+ class Tuple extends FxBase {
1369
+ i0;
1370
+ constructor(i0) {
1371
+ super();
1372
+ this.i0 = i0;
1373
+ }
1374
+ run(sink) {
1375
+ return tupleSink(sink, (onSuccess) => Effect.forEach(this.i0, (fx, i) => fx.run(Sink.make(sink.onFailure, (a) => onSuccess(i, a))), UNBOUNDED), this.i0.length);
1376
+ }
1377
+ }
1378
+ export function struct(fx) {
1379
+ const entries = Object.entries(fx);
1380
+ return map(tuple(entries.map(([key, fx]) => map(fx, (a) => [key, a]))), Object.fromEntries);
1381
+ }
1382
+ export function all(fx) {
1383
+ if (Array.isArray(fx))
1384
+ return tuple(fx);
1385
+ else
1386
+ return struct(fx);
1387
+ }
1388
+ export function exit(fx) {
1389
+ return new ExitFx(fx);
1390
+ }
1391
+ class ExitFx extends FxBase {
1392
+ i0;
1393
+ constructor(i0) {
1394
+ super();
1395
+ this.i0 = i0;
1396
+ }
1397
+ run(sink) {
1398
+ return this.i0.run(Sink.make((cause) => sink.onSuccess(Exit.failCause(cause)), (a) => sink.onSuccess(Exit.succeed(a))));
1399
+ }
1400
+ }
1401
+ export function toEnqueue(fx, queue) {
1402
+ return observe(fx, (a) => queue.offer(a));
1403
+ }
1404
+ export function debounce(fx, delay) {
1405
+ return switchMapEffect(fx, (a) => Effect.as(Effect.sleep(delay), a));
1406
+ }
1407
+ export function throttle(fx, delay) {
1408
+ return exhaustMapEffect(fx, (a) => Effect.as(Effect.sleep(delay), a));
1409
+ }
1410
+ export function throttleLatest(fx, delay) {
1411
+ return exhaustMapLatestEffect(fx, (a) => Effect.as(Effect.sleep(delay), a));
1412
+ }
1413
+ export function fromAsyncIterable(iterable) {
1414
+ return new FromAsyncIterable(iterable);
1415
+ }
1416
+ class FromAsyncIterable extends FxBase {
1417
+ i0;
1418
+ constructor(i0) {
1419
+ super();
1420
+ this.i0 = i0;
1421
+ }
1422
+ run(sink) {
1423
+ return Effect.asyncEffect((cb) => {
1424
+ const iterator = this.i0[Symbol.asyncIterator]();
1425
+ const loop = (result) => result.done
1426
+ ? Effect.sync(() => cb(Effect.unit))
1427
+ : Effect.zipRight(sink.onSuccess(result.value), Effect.flatMap(Effect.promise(() => iterator.next()), loop));
1428
+ return Effect.flatMap(Effect.promise(() => iterator.next()), loop);
1429
+ });
1430
+ }
1431
+ }
1432
+ export function findFirst(fx, predicate) {
1433
+ return Effect.asyncEffect((cb) => observe(fx, (a) => predicate(a) ? Effect.sync(() => cb(Effect.succeed(a))) : Effect.unit));
1434
+ }
1435
+ export function first(fx) {
1436
+ return findFirst(fx, constTrue);
1437
+ }
1438
+ export function either(fx) {
1439
+ return new EitherFx(fx);
1440
+ }
1441
+ class EitherFx extends FxBase {
1442
+ i0;
1443
+ constructor(i0) {
1444
+ super();
1445
+ this.i0 = i0;
1446
+ }
1447
+ run(sink) {
1448
+ return this.i0.run(Sink.make((cause) => Either.match(Cause.failureOrCause(cause), {
1449
+ onLeft: (e) => sink.onSuccess(Either.left(e)),
1450
+ onRight: (cause) => sink.onFailure(cause)
1451
+ }), (a) => sink.onSuccess(Either.right(a))));
1452
+ }
1453
+ }
1454
+ export function mergeFirst(fx, that) {
1455
+ return merge(fx, filter(that, constFalse));
1456
+ }
1457
+ export function mergeRace(fx, that) {
1458
+ return new MergeRace(fx, that);
1459
+ }
1460
+ class MergeRace extends FxBase {
1461
+ i0;
1462
+ i1;
1463
+ constructor(i0, i1) {
1464
+ super();
1465
+ this.i0 = i0;
1466
+ this.i1 = i1;
1467
+ }
1468
+ run(sink) {
1469
+ return Effect.gen(this, function* (_) {
1470
+ const fiber1 = yield* _(Effect.fork(this.i0.run(Sink.make(sink.onFailure, (a) => Effect.flatMap(sink.onSuccess(a), () => Fiber.interrupt(fiber2))))));
1471
+ const fiber2 = yield* _(Effect.fork(this.i1.run(sink)));
1472
+ return yield* _(Fiber.joinAll([fiber1, fiber2]));
1473
+ });
1474
+ }
1475
+ }
1476
+ export function raceAll(fx) {
1477
+ return new RaceAll(fx);
1478
+ }
1479
+ export function race(fx, that) {
1480
+ return raceAll([fx, that]);
1481
+ }
1482
+ class RaceAll extends FxBase {
1483
+ i0;
1484
+ constructor(i0) {
1485
+ super();
1486
+ this.i0 = i0;
1487
+ }
1488
+ run(sink) {
1489
+ return Effect.gen(this, function* (_) {
1490
+ const winner = yield* _(Deferred.make());
1491
+ const fibers = [];
1492
+ for (const fx of this.i0) {
1493
+ const fiber = yield* _(Effect.fork(fx.run(Sink.make(sink.onFailure, (a) => Effect.flatMap(Deferred.succeed(winner, fiber), () => sink.onSuccess(a))))));
1494
+ fibers.push(fiber);
824
1495
  }
825
- // Schedule subsequent pulls using the current scheduler.
826
- yield* _(Effect.yieldNow(options));
827
- }
828
- }), Scope.close));
829
- }
830
- const matchers = {
831
- RefSubject: identity,
832
- Fx: identity,
833
- Effect: fromEffect,
834
- Cause: failCause,
835
- Iterable: fromIterable,
836
- Otherwise: succeed
837
- };
838
- export function from(input) {
839
- return matchFxInput(input, matchers);
1496
+ const winningFiber = yield* _(Deferred.await(winner));
1497
+ yield* _(Fiber.interruptAll(fibers.filter((x) => x !== winningFiber)));
1498
+ return yield* _(Fiber.join(winningFiber));
1499
+ });
1500
+ }
1501
+ }
1502
+ export function snapshot(fx, sampled, f) {
1503
+ return new Snapshot(fx, sampled, f);
1504
+ }
1505
+ export function sample(fx, sampled) {
1506
+ return snapshot(fx, sampled, (_, b) => b);
1507
+ }
1508
+ class Snapshot extends FxBase {
1509
+ i0;
1510
+ i1;
1511
+ i2;
1512
+ constructor(i0, i1, i2) {
1513
+ super();
1514
+ this.i0 = i0;
1515
+ this.i1 = i1;
1516
+ this.i2 = i2;
1517
+ }
1518
+ run(sink) {
1519
+ return Effect.flatMap(Ref.make(Option.none()), (ref) => Effect.all([
1520
+ this.i1.run(Sink.make(sink.onFailure, (b) => Ref.set(ref, Option.some(b)))),
1521
+ this.i0.run(Sink.make(sink.onFailure, (a) => Effect.flatMap(Ref.get(ref), Option.match({
1522
+ onNone: () => Effect.unit,
1523
+ onSome: (b) => sink.onSuccess(this.i2(a, b))
1524
+ }))))
1525
+ ], UNBOUNDED));
1526
+ }
1527
+ }
1528
+ export function snapshotEffect(fx, sampled, f) {
1529
+ return new SnapshotEffect(fx, sampled, f);
1530
+ }
1531
+ class SnapshotEffect extends FxBase {
1532
+ i0;
1533
+ i1;
1534
+ i2;
1535
+ constructor(i0, i1, i2) {
1536
+ super();
1537
+ this.i0 = i0;
1538
+ this.i1 = i1;
1539
+ this.i2 = i2;
1540
+ }
1541
+ run(sink) {
1542
+ return Effect.flatMap(Ref.make(Option.none()), (ref) => Effect.flatMap(Effect.tap(Effect.fork(this.i1.run(Sink.make(sink.onFailure, (b) => Ref.set(ref, Option.some(b))))), () => this.i0.run(Sink.make(sink.onFailure, (a) => Effect.flatMap(Ref.get(ref), Option.match({
1543
+ onNone: () => Effect.unit,
1544
+ onSome: (b) => Effect.matchCauseEffect(this.i2(a, b), sink)
1545
+ }))))), Fiber.interrupt));
1546
+ }
1547
+ }
1548
+ function if_(bool, options) {
1549
+ return switchMap(bool, (b) => b ? options.onTrue : options.onFalse);
1550
+ }
1551
+ export { if_ as if };
1552
+ export function when(bool, options) {
1553
+ return if_(bool, {
1554
+ onTrue: succeed(options.onTrue),
1555
+ onFalse: succeed(options.onFalse)
1556
+ });
1557
+ }
1558
+ export function mapBoth(fx, f, g) {
1559
+ return map(mapError(fx, f), g);
840
1560
  }
841
1561
  //# sourceMappingURL=core.js.map