@typed/fx 0.0.4 → 0.0.8

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 (461) hide show
  1. package/.eslintcache +1 -1
  2. package/cjs/Cause/Cause.d.ts +10 -0
  3. package/cjs/Cause/Cause.d.ts.map +1 -1
  4. package/cjs/Cause/Cause.js +37 -2
  5. package/cjs/Cause/Cause.js.map +1 -1
  6. package/cjs/Cause/Renderer.js +1 -1
  7. package/cjs/Cause/Renderer.js.map +1 -1
  8. package/cjs/Env/Env.d.ts +9 -1
  9. package/cjs/Env/Env.d.ts.map +1 -1
  10. package/cjs/Env/Env.js +32 -5
  11. package/cjs/Env/Env.js.map +1 -1
  12. package/cjs/Fiber/fromScope.d.ts +5 -0
  13. package/cjs/Fiber/fromScope.d.ts.map +1 -0
  14. package/cjs/Fiber/fromScope.js +17 -0
  15. package/cjs/Fiber/fromScope.js.map +1 -0
  16. package/cjs/FiberRef/builtins.d.ts +4 -1
  17. package/cjs/FiberRef/builtins.d.ts.map +1 -1
  18. package/cjs/FiberRef/builtins.js +40 -13
  19. package/cjs/FiberRef/builtins.js.map +1 -1
  20. package/cjs/FiberRuntime/FiberRuntime.d.ts.map +1 -1
  21. package/cjs/FiberRuntime/FiberRuntime.js +23 -14
  22. package/cjs/FiberRuntime/FiberRuntime.js.map +1 -1
  23. package/cjs/Fx/Fx.d.ts +4 -3
  24. package/cjs/Fx/Fx.d.ts.map +1 -1
  25. package/cjs/Fx/Fx.js +25 -24
  26. package/cjs/Fx/Fx.js.map +1 -1
  27. package/cjs/Fx/join.d.ts +1 -1
  28. package/cjs/Fx/join.d.ts.map +1 -1
  29. package/cjs/Fx/join.js +1 -1
  30. package/cjs/Fx/join.js.map +1 -1
  31. package/cjs/Schedule/ScheduleState.d.ts +3 -0
  32. package/cjs/Schedule/ScheduleState.d.ts.map +1 -1
  33. package/cjs/Schedule/ScheduleState.js +3 -0
  34. package/cjs/Schedule/ScheduleState.js.map +1 -1
  35. package/cjs/Scheduler/RootScheduler.d.ts.map +1 -1
  36. package/cjs/Scheduler/RootScheduler.js +4 -5
  37. package/cjs/Scheduler/RootScheduler.js.map +1 -1
  38. package/cjs/Scheduler/Scheduler.d.ts +2 -2
  39. package/cjs/Scheduler/Scheduler.d.ts.map +1 -1
  40. package/cjs/Scheduler/Scheduler.js.map +1 -1
  41. package/cjs/Scheduler/callbackScheduler.d.ts.map +1 -1
  42. package/cjs/Scheduler/callbackScheduler.js +1 -0
  43. package/cjs/Scheduler/callbackScheduler.js.map +1 -1
  44. package/cjs/Scheduler/runSchedule.d.ts +1 -1
  45. package/cjs/Scheduler/runSchedule.d.ts.map +1 -1
  46. package/cjs/Scheduler/runSchedule.js +3 -2
  47. package/cjs/Scheduler/runSchedule.js.map +1 -1
  48. package/cjs/Scope/LocalScope.d.ts.map +1 -1
  49. package/cjs/Scope/LocalScope.js +8 -8
  50. package/cjs/Scope/LocalScope.js.map +1 -1
  51. package/cjs/Semaphore/Semaphore.d.ts +9 -9
  52. package/cjs/Semaphore/Semaphore.d.ts.map +1 -1
  53. package/cjs/Semaphore/Semaphore.js +34 -15
  54. package/cjs/Semaphore/Semaphore.js.map +1 -1
  55. package/cjs/Sink/Sink.d.ts +1 -0
  56. package/cjs/Sink/Sink.d.ts.map +1 -1
  57. package/cjs/Sink/Sink.js +11 -1
  58. package/cjs/Sink/Sink.js.map +1 -1
  59. package/cjs/Stream/Stream.d.ts +5 -14
  60. package/cjs/Stream/Stream.d.ts.map +1 -1
  61. package/cjs/Stream/Stream.js.map +1 -1
  62. package/cjs/Stream/acquirePermit.d.ts +15 -0
  63. package/cjs/Stream/acquirePermit.d.ts.map +1 -0
  64. package/cjs/Stream/acquirePermit.js +43 -0
  65. package/cjs/Stream/acquirePermit.js.map +1 -0
  66. package/cjs/Stream/bimap.d.ts +33 -4
  67. package/cjs/Stream/bimap.d.ts.map +1 -1
  68. package/cjs/Stream/bimap.js +109 -12
  69. package/cjs/Stream/bimap.js.map +1 -1
  70. package/cjs/Stream/continueWith.d.ts +4 -3
  71. package/cjs/Stream/continueWith.d.ts.map +1 -1
  72. package/cjs/Stream/continueWith.js +9 -6
  73. package/cjs/Stream/continueWith.js.map +1 -1
  74. package/cjs/Stream/delay.d.ts +4 -0
  75. package/cjs/Stream/delay.d.ts.map +1 -0
  76. package/cjs/Stream/delay.js +30 -0
  77. package/cjs/Stream/delay.js.map +1 -0
  78. package/cjs/Stream/drain.d.ts +2 -2
  79. package/cjs/Stream/drain.d.ts.map +1 -1
  80. package/cjs/Stream/drain.js +5 -5
  81. package/cjs/Stream/drain.js.map +1 -1
  82. package/cjs/Stream/empty.d.ts.map +1 -1
  83. package/cjs/Stream/empty.js +1 -2
  84. package/cjs/Stream/empty.js.map +1 -1
  85. package/cjs/Stream/flatMap.d.ts +7 -5
  86. package/cjs/Stream/flatMap.d.ts.map +1 -1
  87. package/cjs/Stream/flatMap.js +33 -19
  88. package/cjs/Stream/flatMap.js.map +1 -1
  89. package/cjs/Stream/flatMapConcurrently.d.ts +6 -0
  90. package/cjs/Stream/flatMapConcurrently.d.ts.map +1 -0
  91. package/cjs/Stream/flatMapConcurrently.js +25 -0
  92. package/cjs/Stream/flatMapConcurrently.js.map +1 -0
  93. package/cjs/Stream/flatMapFx.d.ts +5 -1
  94. package/cjs/Stream/flatMapFx.d.ts.map +1 -1
  95. package/cjs/Stream/flatMapFx.js +18 -3
  96. package/cjs/Stream/flatMapFx.js.map +1 -1
  97. package/cjs/Stream/foldMap.d.ts +11 -0
  98. package/cjs/Stream/foldMap.d.ts.map +1 -0
  99. package/cjs/Stream/foldMap.js +67 -0
  100. package/cjs/Stream/foldMap.js.map +1 -0
  101. package/cjs/Stream/fromArray.d.ts +2 -0
  102. package/cjs/Stream/fromArray.d.ts.map +1 -0
  103. package/cjs/Stream/fromArray.js +12 -0
  104. package/cjs/Stream/fromArray.js.map +1 -0
  105. package/cjs/Stream/fromCallback.d.ts +4 -3
  106. package/cjs/Stream/fromCallback.d.ts.map +1 -1
  107. package/cjs/Stream/fromCallback.js +26 -26
  108. package/cjs/Stream/fromCallback.js.map +1 -1
  109. package/cjs/Stream/fromFx.d.ts +23 -8
  110. package/cjs/Stream/fromFx.d.ts.map +1 -1
  111. package/cjs/Stream/fromFx.js +36 -16
  112. package/cjs/Stream/fromFx.js.map +1 -1
  113. package/cjs/Stream/hkt.d.ts +24 -1
  114. package/cjs/Stream/hkt.d.ts.map +1 -1
  115. package/cjs/Stream/hkt.js +43 -5
  116. package/cjs/Stream/hkt.js.map +1 -1
  117. package/cjs/Stream/hold.d.ts +27 -0
  118. package/cjs/Stream/hold.d.ts.map +1 -0
  119. package/cjs/Stream/hold.js +94 -0
  120. package/cjs/Stream/hold.js.map +1 -0
  121. package/cjs/Stream/index.d.ts +14 -1
  122. package/cjs/Stream/index.d.ts.map +1 -1
  123. package/cjs/Stream/index.js +14 -1
  124. package/cjs/Stream/index.js.map +1 -1
  125. package/cjs/Stream/lazy.d.ts +3 -0
  126. package/cjs/Stream/lazy.d.ts.map +1 -0
  127. package/cjs/Stream/lazy.js +9 -0
  128. package/cjs/Stream/lazy.js.map +1 -0
  129. package/cjs/Stream/merge.d.ts +18 -0
  130. package/cjs/Stream/merge.d.ts.map +1 -0
  131. package/cjs/Stream/merge.js +84 -0
  132. package/cjs/Stream/merge.js.map +1 -0
  133. package/cjs/Stream/multicast.d.ts +27 -0
  134. package/cjs/Stream/multicast.d.ts.map +1 -0
  135. package/cjs/Stream/multicast.js +83 -0
  136. package/cjs/Stream/multicast.js.map +1 -0
  137. package/cjs/Stream/orElse.d.ts +17 -0
  138. package/cjs/Stream/orElse.d.ts.map +1 -0
  139. package/cjs/Stream/orElse.js +105 -0
  140. package/cjs/Stream/orElse.js.map +1 -0
  141. package/cjs/Stream/periodic.d.ts +1 -1
  142. package/cjs/Stream/periodic.d.ts.map +1 -1
  143. package/cjs/Stream/periodic.js +1 -1
  144. package/cjs/Stream/periodic.js.map +1 -1
  145. package/cjs/Stream/provide.d.ts +8 -0
  146. package/cjs/Stream/provide.d.ts.map +1 -0
  147. package/cjs/Stream/provide.js +42 -0
  148. package/cjs/Stream/provide.js.map +1 -0
  149. package/cjs/Stream/run.d.ts +2 -2
  150. package/cjs/Stream/run.d.ts.map +1 -1
  151. package/cjs/Stream/run.js +1 -1
  152. package/cjs/Stream/run.js.map +1 -1
  153. package/cjs/Stream/scan.d.ts +17 -0
  154. package/cjs/Stream/scan.d.ts.map +1 -0
  155. package/cjs/Stream/scan.js +32 -0
  156. package/cjs/Stream/scan.js.map +1 -0
  157. package/cjs/Stream/scheduled.d.ts +12 -3
  158. package/cjs/Stream/scheduled.d.ts.map +1 -1
  159. package/cjs/Stream/scheduled.js +23 -5
  160. package/cjs/Stream/scheduled.js.map +1 -1
  161. package/cjs/Stream/setFiberRefLocally.d.ts +15 -0
  162. package/cjs/Stream/setFiberRefLocally.d.ts.map +1 -0
  163. package/cjs/Stream/setFiberRefLocally.js +53 -0
  164. package/cjs/Stream/setFiberRefLocally.js.map +1 -0
  165. package/cjs/Stream/skipRepeats.d.ts +5 -0
  166. package/cjs/Stream/skipRepeats.d.ts.map +1 -0
  167. package/cjs/Stream/skipRepeats.js +36 -0
  168. package/cjs/Stream/skipRepeats.js.map +1 -0
  169. package/cjs/Stream/switchMap.d.ts +18 -0
  170. package/cjs/Stream/switchMap.d.ts.map +1 -0
  171. package/cjs/Stream/switchMap.js +124 -0
  172. package/cjs/Stream/switchMap.js.map +1 -0
  173. package/cjs/Stream/tap.d.ts +2 -1
  174. package/cjs/Stream/tap.d.ts.map +1 -1
  175. package/cjs/Stream/tap.js +4 -2
  176. package/cjs/Stream/tap.js.map +1 -1
  177. package/cjs/Supervisor/index.d.ts +2 -0
  178. package/cjs/Supervisor/index.d.ts.map +1 -1
  179. package/cjs/Supervisor/index.js +2 -0
  180. package/cjs/Supervisor/index.js.map +1 -1
  181. package/cjs/Supervisor/inheritFiberRefs.d.ts +2 -0
  182. package/cjs/Supervisor/inheritFiberRefs.d.ts.map +1 -0
  183. package/cjs/Supervisor/inheritFiberRefs.js +17 -0
  184. package/cjs/Supervisor/inheritFiberRefs.js.map +1 -0
  185. package/cjs/Timeline/index.js.map +1 -1
  186. package/esm/Cause/Cause.d.ts +10 -0
  187. package/esm/Cause/Cause.d.ts.map +1 -1
  188. package/esm/Cause/Cause.js +27 -0
  189. package/esm/Cause/Cause.js.map +1 -1
  190. package/esm/Cause/Renderer.js +1 -1
  191. package/esm/Cause/Renderer.js.map +1 -1
  192. package/esm/Env/Env.d.ts +9 -1
  193. package/esm/Env/Env.d.ts.map +1 -1
  194. package/esm/Env/Env.js +8 -5
  195. package/esm/Env/Env.js.map +1 -1
  196. package/esm/Fiber/fromScope.d.ts +5 -0
  197. package/esm/Fiber/fromScope.d.ts.map +1 -0
  198. package/esm/Fiber/fromScope.js +13 -0
  199. package/esm/Fiber/fromScope.js.map +1 -0
  200. package/esm/FiberRef/builtins.d.ts +4 -1
  201. package/esm/FiberRef/builtins.d.ts.map +1 -1
  202. package/esm/FiberRef/builtins.js +36 -12
  203. package/esm/FiberRef/builtins.js.map +1 -1
  204. package/esm/FiberRuntime/FiberRuntime.d.ts.map +1 -1
  205. package/esm/FiberRuntime/FiberRuntime.js +23 -14
  206. package/esm/FiberRuntime/FiberRuntime.js.map +1 -1
  207. package/esm/Fx/Fx.d.ts +4 -3
  208. package/esm/Fx/Fx.d.ts.map +1 -1
  209. package/esm/Fx/Fx.js +22 -22
  210. package/esm/Fx/Fx.js.map +1 -1
  211. package/esm/Fx/join.d.ts +1 -1
  212. package/esm/Fx/join.d.ts.map +1 -1
  213. package/esm/Fx/join.js +1 -1
  214. package/esm/Fx/join.js.map +1 -1
  215. package/esm/Schedule/ScheduleState.d.ts +3 -0
  216. package/esm/Schedule/ScheduleState.d.ts.map +1 -1
  217. package/esm/Schedule/ScheduleState.js +3 -0
  218. package/esm/Schedule/ScheduleState.js.map +1 -1
  219. package/esm/Scheduler/RootScheduler.d.ts.map +1 -1
  220. package/esm/Scheduler/RootScheduler.js +4 -5
  221. package/esm/Scheduler/RootScheduler.js.map +1 -1
  222. package/esm/Scheduler/Scheduler.d.ts +2 -2
  223. package/esm/Scheduler/Scheduler.d.ts.map +1 -1
  224. package/esm/Scheduler/Scheduler.js.map +1 -1
  225. package/esm/Scheduler/callbackScheduler.d.ts.map +1 -1
  226. package/esm/Scheduler/callbackScheduler.js +1 -0
  227. package/esm/Scheduler/callbackScheduler.js.map +1 -1
  228. package/esm/Scheduler/runSchedule.d.ts +1 -1
  229. package/esm/Scheduler/runSchedule.d.ts.map +1 -1
  230. package/esm/Scheduler/runSchedule.js +4 -3
  231. package/esm/Scheduler/runSchedule.js.map +1 -1
  232. package/esm/Scope/LocalScope.d.ts.map +1 -1
  233. package/esm/Scope/LocalScope.js +8 -8
  234. package/esm/Scope/LocalScope.js.map +1 -1
  235. package/esm/Semaphore/Semaphore.d.ts +9 -9
  236. package/esm/Semaphore/Semaphore.d.ts.map +1 -1
  237. package/esm/Semaphore/Semaphore.js +11 -15
  238. package/esm/Semaphore/Semaphore.js.map +1 -1
  239. package/esm/Sink/Sink.d.ts +1 -0
  240. package/esm/Sink/Sink.d.ts.map +1 -1
  241. package/esm/Sink/Sink.js +10 -1
  242. package/esm/Sink/Sink.js.map +1 -1
  243. package/esm/Stream/Stream.d.ts +5 -14
  244. package/esm/Stream/Stream.d.ts.map +1 -1
  245. package/esm/Stream/Stream.js.map +1 -1
  246. package/esm/Stream/acquirePermit.d.ts +15 -0
  247. package/esm/Stream/acquirePermit.d.ts.map +1 -0
  248. package/esm/Stream/acquirePermit.js +15 -0
  249. package/esm/Stream/acquirePermit.js.map +1 -0
  250. package/esm/Stream/bimap.d.ts +33 -4
  251. package/esm/Stream/bimap.d.ts.map +1 -1
  252. package/esm/Stream/bimap.js +102 -11
  253. package/esm/Stream/bimap.js.map +1 -1
  254. package/esm/Stream/continueWith.d.ts +4 -3
  255. package/esm/Stream/continueWith.d.ts.map +1 -1
  256. package/esm/Stream/continueWith.js +9 -6
  257. package/esm/Stream/continueWith.js.map +1 -1
  258. package/esm/Stream/delay.d.ts +4 -0
  259. package/esm/Stream/delay.d.ts.map +1 -0
  260. package/esm/Stream/delay.js +26 -0
  261. package/esm/Stream/delay.js.map +1 -0
  262. package/esm/Stream/drain.d.ts +2 -2
  263. package/esm/Stream/drain.d.ts.map +1 -1
  264. package/esm/Stream/drain.js +6 -6
  265. package/esm/Stream/drain.js.map +1 -1
  266. package/esm/Stream/empty.d.ts.map +1 -1
  267. package/esm/Stream/empty.js +1 -2
  268. package/esm/Stream/empty.js.map +1 -1
  269. package/esm/Stream/flatMap.d.ts +7 -5
  270. package/esm/Stream/flatMap.d.ts.map +1 -1
  271. package/esm/Stream/flatMap.js +32 -19
  272. package/esm/Stream/flatMap.js.map +1 -1
  273. package/esm/Stream/flatMapConcurrently.d.ts +6 -0
  274. package/esm/Stream/flatMapConcurrently.d.ts.map +1 -0
  275. package/esm/Stream/flatMapConcurrently.js +19 -0
  276. package/esm/Stream/flatMapConcurrently.js.map +1 -0
  277. package/esm/Stream/flatMapFx.d.ts +5 -1
  278. package/esm/Stream/flatMapFx.d.ts.map +1 -1
  279. package/esm/Stream/flatMapFx.js +16 -4
  280. package/esm/Stream/flatMapFx.js.map +1 -1
  281. package/esm/Stream/foldMap.d.ts +11 -0
  282. package/esm/Stream/foldMap.d.ts.map +1 -0
  283. package/esm/Stream/foldMap.js +38 -0
  284. package/esm/Stream/foldMap.js.map +1 -0
  285. package/esm/Stream/fromArray.d.ts +2 -0
  286. package/esm/Stream/fromArray.d.ts.map +1 -0
  287. package/esm/Stream/fromArray.js +8 -0
  288. package/esm/Stream/fromArray.js.map +1 -0
  289. package/esm/Stream/fromCallback.d.ts +4 -3
  290. package/esm/Stream/fromCallback.d.ts.map +1 -1
  291. package/esm/Stream/fromCallback.js +26 -26
  292. package/esm/Stream/fromCallback.js.map +1 -1
  293. package/esm/Stream/fromFx.d.ts +23 -8
  294. package/esm/Stream/fromFx.d.ts.map +1 -1
  295. package/esm/Stream/fromFx.js +24 -14
  296. package/esm/Stream/fromFx.js.map +1 -1
  297. package/esm/Stream/hkt.d.ts +24 -1
  298. package/esm/Stream/hkt.d.ts.map +1 -1
  299. package/esm/Stream/hkt.js +41 -4
  300. package/esm/Stream/hkt.js.map +1 -1
  301. package/esm/Stream/hold.d.ts +27 -0
  302. package/esm/Stream/hold.d.ts.map +1 -0
  303. package/esm/Stream/hold.js +66 -0
  304. package/esm/Stream/hold.js.map +1 -0
  305. package/esm/Stream/index.d.ts +14 -1
  306. package/esm/Stream/index.d.ts.map +1 -1
  307. package/esm/Stream/index.js +14 -1
  308. package/esm/Stream/index.js.map +1 -1
  309. package/esm/Stream/lazy.d.ts +3 -0
  310. package/esm/Stream/lazy.d.ts.map +1 -0
  311. package/esm/Stream/lazy.js +5 -0
  312. package/esm/Stream/lazy.js.map +1 -0
  313. package/esm/Stream/merge.d.ts +18 -0
  314. package/esm/Stream/merge.d.ts.map +1 -0
  315. package/esm/Stream/merge.js +55 -0
  316. package/esm/Stream/merge.js.map +1 -0
  317. package/esm/Stream/multicast.d.ts +27 -0
  318. package/esm/Stream/multicast.d.ts.map +1 -0
  319. package/esm/Stream/multicast.js +55 -0
  320. package/esm/Stream/multicast.js.map +1 -0
  321. package/esm/Stream/orElse.d.ts +17 -0
  322. package/esm/Stream/orElse.d.ts.map +1 -0
  323. package/esm/Stream/orElse.js +77 -0
  324. package/esm/Stream/orElse.js.map +1 -0
  325. package/esm/Stream/periodic.d.ts +1 -1
  326. package/esm/Stream/periodic.d.ts.map +1 -1
  327. package/esm/Stream/periodic.js +1 -1
  328. package/esm/Stream/periodic.js.map +1 -1
  329. package/esm/Stream/provide.d.ts +8 -0
  330. package/esm/Stream/provide.d.ts.map +1 -0
  331. package/esm/Stream/provide.js +13 -0
  332. package/esm/Stream/provide.js.map +1 -0
  333. package/esm/Stream/run.d.ts +2 -2
  334. package/esm/Stream/run.d.ts.map +1 -1
  335. package/esm/Stream/run.js +2 -2
  336. package/esm/Stream/run.js.map +1 -1
  337. package/esm/Stream/scan.d.ts +17 -0
  338. package/esm/Stream/scan.d.ts.map +1 -0
  339. package/esm/Stream/scan.js +27 -0
  340. package/esm/Stream/scan.js.map +1 -0
  341. package/esm/Stream/scheduled.d.ts +12 -3
  342. package/esm/Stream/scheduled.d.ts.map +1 -1
  343. package/esm/Stream/scheduled.js +16 -4
  344. package/esm/Stream/scheduled.js.map +1 -1
  345. package/esm/Stream/setFiberRefLocally.d.ts +15 -0
  346. package/esm/Stream/setFiberRefLocally.d.ts.map +1 -0
  347. package/esm/Stream/setFiberRefLocally.js +25 -0
  348. package/esm/Stream/setFiberRefLocally.js.map +1 -0
  349. package/esm/Stream/skipRepeats.d.ts +5 -0
  350. package/esm/Stream/skipRepeats.d.ts.map +1 -0
  351. package/esm/Stream/skipRepeats.js +31 -0
  352. package/esm/Stream/skipRepeats.js.map +1 -0
  353. package/esm/Stream/switchMap.d.ts +18 -0
  354. package/esm/Stream/switchMap.d.ts.map +1 -0
  355. package/esm/Stream/switchMap.js +95 -0
  356. package/esm/Stream/switchMap.js.map +1 -0
  357. package/esm/Stream/tap.d.ts +2 -1
  358. package/esm/Stream/tap.d.ts.map +1 -1
  359. package/esm/Stream/tap.js +3 -2
  360. package/esm/Stream/tap.js.map +1 -1
  361. package/esm/Supervisor/index.d.ts +2 -0
  362. package/esm/Supervisor/index.d.ts.map +1 -1
  363. package/esm/Supervisor/index.js +2 -0
  364. package/esm/Supervisor/index.js.map +1 -1
  365. package/esm/Supervisor/inheritFiberRefs.d.ts +2 -0
  366. package/esm/Supervisor/inheritFiberRefs.d.ts.map +1 -0
  367. package/esm/Supervisor/inheritFiberRefs.js +14 -0
  368. package/esm/Supervisor/inheritFiberRefs.js.map +1 -0
  369. package/esm/Timeline/index.js.map +1 -1
  370. package/package.json +173 -37
  371. package/perf/Stream-filter-map-reduce.test.ts +103 -0
  372. package/readme.md +1 -1
  373. package/src/Cause/Cause.ts +63 -0
  374. package/src/Cause/Renderer.ts +1 -1
  375. package/src/Env/Env.ts +15 -5
  376. package/src/Fiber/fromScope.ts +19 -0
  377. package/src/FiberRef/builtins.ts +63 -11
  378. package/src/FiberRuntime/FiberRuntime.ts +35 -28
  379. package/src/Fx/Fx.test.ts +2 -2
  380. package/src/Fx/Fx.ts +11 -10
  381. package/src/Fx/join.ts +4 -4
  382. package/src/Schedule/ScheduleState.ts +3 -0
  383. package/src/Scheduler/RootScheduler.ts +7 -9
  384. package/src/Scheduler/Scheduler.ts +5 -6
  385. package/src/Scheduler/callbackScheduler.ts +1 -0
  386. package/src/Scheduler/runSchedule.ts +5 -2
  387. package/src/Scope/LocalScope.ts +8 -6
  388. package/src/Semaphore/Semaphore.ts +12 -19
  389. package/src/Sink/Sink.ts +11 -1
  390. package/src/Stream/Stream.ts +9 -15
  391. package/src/Stream/acquirePermit.ts +32 -0
  392. package/src/Stream/bimap.ts +164 -12
  393. package/src/Stream/continueWith.ts +11 -7
  394. package/src/Stream/delay.ts +43 -0
  395. package/src/Stream/drain.ts +8 -7
  396. package/src/Stream/empty.ts +1 -2
  397. package/src/Stream/flatMap.test.ts +8 -21
  398. package/src/Stream/flatMap.ts +46 -24
  399. package/src/Stream/flatMapConcurrently.test.ts +77 -0
  400. package/src/Stream/flatMapConcurrently.ts +34 -0
  401. package/src/Stream/flatMapFx.ts +37 -4
  402. package/src/Stream/foldMap.ts +88 -0
  403. package/src/Stream/fromArray.ts +8 -0
  404. package/src/Stream/fromCallback.test.ts +3 -16
  405. package/src/Stream/fromCallback.ts +48 -29
  406. package/src/Stream/fromFx.test.ts +3 -24
  407. package/src/Stream/fromFx.ts +54 -28
  408. package/src/Stream/hkt.ts +59 -10
  409. package/src/Stream/hold.test.ts +52 -0
  410. package/src/Stream/hold.ts +117 -0
  411. package/src/Stream/index.ts +14 -1
  412. package/src/Stream/lazy.ts +5 -0
  413. package/src/Stream/logging.test.ts +8 -3
  414. package/src/Stream/merge.test.ts +31 -0
  415. package/src/Stream/merge.ts +76 -0
  416. package/src/Stream/multicast.test.ts +32 -0
  417. package/src/Stream/multicast.ts +114 -0
  418. package/src/Stream/orElse.ts +107 -0
  419. package/src/Stream/periodic.ts +2 -2
  420. package/src/Stream/provide.test.ts +78 -0
  421. package/src/Stream/provide.ts +34 -0
  422. package/src/Stream/run.ts +6 -3
  423. package/src/Stream/scan.test.ts +28 -0
  424. package/src/Stream/scan.ts +46 -0
  425. package/src/Stream/scheduled.test.ts +9 -1
  426. package/src/Stream/scheduled.ts +46 -12
  427. package/src/Stream/setFiberRefLocally.ts +38 -0
  428. package/src/Stream/skipRepeats.ts +41 -0
  429. package/src/Stream/switchMap.test.ts +43 -0
  430. package/src/Stream/switchMap.ts +145 -0
  431. package/src/Stream/tap.ts +6 -3
  432. package/src/Supervisor/index.ts +2 -0
  433. package/src/Supervisor/inheritFiberRefs.ts +17 -0
  434. package/src/Timeline/index.ts +1 -0
  435. package/cjs/Effect/Effect.d.ts +0 -199
  436. package/cjs/Effect/Effect.d.ts.map +0 -1
  437. package/cjs/Effect/Effect.js +0 -66
  438. package/cjs/Effect/Effect.js.map +0 -1
  439. package/cjs/Stream/map.d.ts +0 -14
  440. package/cjs/Stream/map.d.ts.map +0 -1
  441. package/cjs/Stream/map.js +0 -30
  442. package/cjs/Stream/map.js.map +0 -1
  443. package/cjs/test.d.ts +0 -1
  444. package/cjs/test.d.ts.map +0 -1
  445. package/cjs/test.js +0 -2
  446. package/cjs/test.js.map +0 -1
  447. package/esm/Effect/Effect.d.ts +0 -199
  448. package/esm/Effect/Effect.d.ts.map +0 -1
  449. package/esm/Effect/Effect.js +0 -50
  450. package/esm/Effect/Effect.js.map +0 -1
  451. package/esm/Stream/map.d.ts +0 -14
  452. package/esm/Stream/map.d.ts.map +0 -1
  453. package/esm/Stream/map.js +0 -25
  454. package/esm/Stream/map.js.map +0 -1
  455. package/esm/test.d.ts +0 -1
  456. package/esm/test.d.ts.map +0 -1
  457. package/esm/test.js +0 -2
  458. package/esm/test.js.map +0 -1
  459. package/src/Effect/Effect.ts +0 -109
  460. package/src/Stream/map.ts +0 -35
  461. package/src/test.ts +0 -0
package/src/Stream/hkt.ts CHANGED
@@ -1,18 +1,24 @@
1
1
  import { HKT3, Params } from 'hkt-ts'
2
+ import * as AB from 'hkt-ts/Typeclass/AssociativeBoth'
3
+ import * as AF from 'hkt-ts/Typeclass/AssociativeFlatten'
2
4
  import * as B from 'hkt-ts/Typeclass/Bicovariant'
5
+ import { Bottom3 } from 'hkt-ts/Typeclass/Bottom'
3
6
  import * as C from 'hkt-ts/Typeclass/Covariant'
7
+ import * as FM from 'hkt-ts/Typeclass/FilterMap'
8
+ import * as IB from 'hkt-ts/Typeclass/IdentityBoth'
9
+ import { IdentityFlatten3 } from 'hkt-ts/Typeclass/IdentityFlatten'
10
+ import * as T from 'hkt-ts/Typeclass/Top'
11
+ import { NonNegativeInteger } from 'hkt-ts/number'
4
12
 
5
13
  import { Stream } from './Stream.js'
6
- import { bimap } from './bimap.js'
7
- import { map } from './map.js'
14
+ import { bimap, filterMap, map } from './bimap.js'
15
+ import { join } from './flatMap.js'
16
+ import { mergeConcurrently } from './flatMapConcurrently.js'
17
+ import { fromFx } from './fromFx.js'
18
+
19
+ import * as Fx from '@/Fx/index.js'
8
20
 
9
- // TODO: AssociativeBoth
10
21
  // TOOD: AssociativeEither
11
- // TODO: Top
12
- // TODO: Bottom
13
- // TODO: FilterMap
14
- // TODO: AssociativeFlatten
15
- // TODO: IdentityBoth
16
22
  // TODO: IdentityEither
17
23
  // TODO: IdentityFlatten
18
24
 
@@ -24,8 +30,6 @@ export const Bicovariant: B.Bicovariant3<StreamHKT> = {
24
30
  bimap,
25
31
  }
26
32
 
27
- export const mapLeft = B.mapLeft(Bicovariant)
28
-
29
33
  export const Covariant: C.Covariant3<StreamHKT> = {
30
34
  map,
31
35
  }
@@ -34,3 +38,48 @@ export const bindTo = C.bindTo(Covariant)
34
38
  export const flap = C.flap(Covariant)
35
39
  export const mapTo = C.mapTo(Covariant)
36
40
  export const tupled = C.tupled(Covariant)
41
+
42
+ export const Top: T.Top3<StreamHKT> = {
43
+ top: fromFx(Fx.top),
44
+ }
45
+
46
+ export const Bottom: Bottom3<StreamHKT> = {
47
+ bottom: fromFx(Fx.bottom),
48
+ }
49
+
50
+ export const Flatten: AF.AssociativeFlatten3<StreamHKT> = {
51
+ flatten: join,
52
+ }
53
+
54
+ export const IdentityFlatten: IdentityFlatten3<StreamHKT> = {
55
+ ...Flatten,
56
+ ...Top,
57
+ }
58
+
59
+ export const makeFlattenConcurrently = (
60
+ concurrencyLevel: NonNegativeInteger,
61
+ ): AF.AssociativeFlatten3<StreamHKT> => ({
62
+ flatten: mergeConcurrently(concurrencyLevel),
63
+ })
64
+
65
+ export const AssociativeBoth = AF.makeAssociativeBoth<StreamHKT>({
66
+ ...Flatten,
67
+ ...Covariant,
68
+ })
69
+
70
+ export const zipLeftSeq = AB.zipLeft<StreamHKT>({ ...AssociativeBoth, ...Covariant })
71
+ export const zipRightSeq = AB.zipRight<StreamHKT>({ ...AssociativeBoth, ...Covariant })
72
+
73
+ export const IdentityBoth: IB.IdentityBoth3<StreamHKT> = {
74
+ ...AssociativeBoth,
75
+ ...Top,
76
+ }
77
+
78
+ export const structSeq = IB.struct<StreamHKT>({ ...IdentityBoth, ...Covariant })
79
+
80
+ export const FilterMap: FM.FilterMap3<StreamHKT> = {
81
+ filterMap,
82
+ }
83
+
84
+ export const compact = FM.compact(FilterMap)
85
+ export const filter = FM.filter(FilterMap)
@@ -0,0 +1,52 @@
1
+ import { deepStrictEqual } from 'assert'
2
+
3
+ import { pipe } from 'hkt-ts/function'
4
+
5
+ import { Stream } from './Stream.js'
6
+ import { collect } from './drain.js'
7
+ import { fromCallback } from './fromCallback.js'
8
+ import { hold } from './hold.js'
9
+
10
+ import { provideService, runMain } from '@/Fx/index.js'
11
+ import { RootScheduler } from '@/Scheduler/RootScheduler.js'
12
+ import { Scheduler } from '@/Scheduler/index.js'
13
+ import { testSuite } from '@/_internal/suite.js'
14
+
15
+ testSuite(import.meta.url, () => {
16
+ describe(hold.name, () => {
17
+ const scheduler = RootScheduler()
18
+ const collectAll = <E, A>(stream: Stream<never, E, A>) =>
19
+ pipe(stream, collect, provideService(Scheduler, scheduler), runMain)
20
+
21
+ it('emits previous event to late subscribers', async () => {
22
+ let started = 0
23
+ const value = Math.random()
24
+ const stream = hold(
25
+ fromCallback<never, number>(async ({ event, end }) => {
26
+ started++
27
+ event(value)
28
+ await new Promise((resolve) => setTimeout(resolve, 50))
29
+ event(value + 1)
30
+ await new Promise((resolve) => setTimeout(resolve, 100))
31
+ event(value + 2)
32
+ end()
33
+ }),
34
+ )
35
+
36
+ const a = collectAll(stream)
37
+
38
+ await new Promise((resolve) => setTimeout(resolve, 25))
39
+
40
+ const b = collectAll(stream)
41
+
42
+ await new Promise((resolve) => setTimeout(resolve, 50))
43
+
44
+ const c = collectAll(stream)
45
+
46
+ deepStrictEqual(await a, [value, value + 1, value + 2])
47
+ deepStrictEqual(await b, [value, value + 1, value + 2])
48
+ deepStrictEqual(await c, [value + 1, value + 2])
49
+ deepStrictEqual(started, 1)
50
+ })
51
+ })
52
+ })
@@ -0,0 +1,117 @@
1
+ import { pipe } from 'hkt-ts'
2
+ import { Just, Maybe, Nothing, isJust, toArray } from 'hkt-ts/Maybe'
3
+
4
+ import { Stream } from './Stream.js'
5
+ import { MulticastStream } from './multicast.js'
6
+
7
+ import { Cause } from '@/Cause/index.js'
8
+ import { fromFiberRefs } from '@/Env/Env.js'
9
+ import { Fiber } from '@/Fiber/Fiber.js'
10
+ import { FiberContext } from '@/FiberContext/FiberContext.js'
11
+ import { FiberId } from '@/FiberId/FiberId.js'
12
+ import * as Fx from '@/Fx/index.js'
13
+ import { Scheduler } from '@/Scheduler/Scheduler.js'
14
+ import { Sink } from '@/Sink/Sink.js'
15
+
16
+ export function hold<R, E, A>(stream: Stream<R, E, A>) {
17
+ return new HoldStream(stream)
18
+ }
19
+
20
+ export class HoldStream<R, E, A> extends MulticastStream<R, E, A> implements Stream<R, E, A> {
21
+ protected value: Maybe<A> = Nothing
22
+ protected pendingSinks: Array<readonly [Sink<E, A, never>, FiberContext<FiberId.Live>, A[]]> = []
23
+ protected scheduledFiber: Fiber<any, any> | undefined
24
+
25
+ constructor(readonly stream: Stream<R, E, A>) {
26
+ super(stream)
27
+ }
28
+
29
+ fork<E2>(sink: Sink<E, A, E2>, scheduler: Scheduler, context: FiberContext<FiberId.Live>) {
30
+ if (this.shouldScheduleFlush()) {
31
+ return pipe(
32
+ this.scheduleFlush(sink, scheduler, context),
33
+ Fx.flatMap(() => super.fork(sink, scheduler, context)),
34
+ )
35
+ }
36
+
37
+ return super.fork(sink, scheduler, context)
38
+ }
39
+
40
+ event = (value: A) => {
41
+ this.addValue(value)
42
+
43
+ return pipe(
44
+ this.flushPending(),
45
+ Fx.flatMap(() => super.event(value)),
46
+ )
47
+ }
48
+
49
+ error = (cause: Cause<E>) => {
50
+ return pipe(
51
+ this.flushPending(),
52
+ Fx.flatMap(() => super.error(cause)),
53
+ )
54
+ }
55
+
56
+ get end() {
57
+ return pipe(
58
+ this.flushPending(),
59
+ Fx.flatMap(() => super.end),
60
+ )
61
+ }
62
+
63
+ shouldScheduleFlush() {
64
+ return isJust(this.value) && this.observers.length > 0
65
+ }
66
+
67
+ flushPending(): Fx.Of<unknown> {
68
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
69
+ const that = this
70
+
71
+ if (this.pendingSinks.length > 0) {
72
+ return Fx.zipAll(
73
+ this.pendingSinks.map((observer) =>
74
+ observer[2].length > 0
75
+ ? pipe(
76
+ Fx.Fx(function* () {
77
+ that.pendingSinks.splice(that.pendingSinks.indexOf(observer), 1)
78
+
79
+ for (const a of observer[2]) {
80
+ yield* Fx.forkJoinInContext(observer[1].fork())(observer[0].event(a))
81
+ }
82
+ }),
83
+ )
84
+ : Fx.unit,
85
+ ),
86
+ )
87
+ }
88
+
89
+ return Fx.unit
90
+ }
91
+
92
+ scheduleFlush<E2>(
93
+ sink: Sink<E, A, E2>,
94
+ scheduler: Scheduler,
95
+ context: FiberContext<FiberId.Live>,
96
+ ): Fx.Of<unknown> {
97
+ this.pendingSinks.push([sink as any, context, [...toArray(this.value)]])
98
+
99
+ const fx = this.scheduledFiber ? this.scheduledFiber.interruptAs(FiberId.None) : Fx.unit
100
+
101
+ this.scheduledFiber = scheduler.asap(
102
+ this.flushPending(),
103
+ fromFiberRefs(context.fiberRefs),
104
+ context.fork(),
105
+ )
106
+
107
+ return fx
108
+ }
109
+
110
+ protected addValue(value: A) {
111
+ this.value = Just(value)
112
+
113
+ this.pendingSinks.forEach(([, , values]) => {
114
+ values.push(value)
115
+ })
116
+ }
117
+ }
@@ -1,14 +1,27 @@
1
+ export * from './acquirePermit.js'
1
2
  export * from './bimap.js'
2
3
  export * from './continueWith.js'
4
+ export * from './delay.js'
3
5
  export * from './drain.js'
4
6
  export * from './empty.js'
5
7
  export * from './flatMap.js'
8
+ export * from './flatMapConcurrently.js'
6
9
  export * from './flatMapFx.js'
10
+ export * from './foldMap.js'
11
+ export * from './fromArray.js'
7
12
  export * from './fromCallback.js'
8
13
  export * from './fromFx.js'
9
14
  export * from './hkt.js'
10
- export * from './map.js'
15
+ export * from './hold.js'
16
+ export * from './lazy.js'
17
+ export * from './merge.js'
18
+ export * from './multicast.js'
11
19
  export * from './periodic.js'
20
+ export * from './provide.js'
21
+ export * from './run.js'
22
+ export * from './setFiberRefLocally.js'
23
+ export * from './skipRepeats.js'
24
+ export * from './scan.js'
12
25
  export * from './scheduled.js'
13
26
  export * from './Stream.js'
14
27
  export * from './tap.js'
@@ -0,0 +1,5 @@
1
+ import { Stream } from './Stream.js'
2
+
3
+ export function lazy<R, E, A>(f: () => Stream<R, E, A>): Stream<R, E, A> {
4
+ return Stream((sink, scheduler, context) => f().fork(sink, scheduler, context))
5
+ }
@@ -1,6 +1,7 @@
1
1
  import { flow, pipe } from 'hkt-ts'
2
2
 
3
3
  import { continueWith } from './continueWith.js'
4
+ import { flatMap } from './flatMap.js'
4
5
  import { now } from './fromFx.js'
5
6
  import { runMain } from './run.js'
6
7
  import { tap } from './tap.js'
@@ -13,9 +14,13 @@ testSuite(import.meta.url, () => {
13
14
  describe('Logging Streams', () => {
14
15
  it('should log the stream', async () => {
15
16
  const stream = pipe(
16
- now(1),
17
- continueWith(() => now(2)),
18
- tap(flow(String, Fx.log)),
17
+ now(1, 'now1'),
18
+ continueWith(() => now(2, 'now2'), 'continueWith'),
19
+ flatMap((n) => now(n + 1, 'now+1'), 'flatMap'),
20
+ tap(
21
+ flow((n) => String(n), Fx.log),
22
+ 'tap',
23
+ ),
19
24
  )
20
25
 
21
26
  await runMain(RootScheduler())(stream)
@@ -0,0 +1,31 @@
1
+ import { deepStrictEqual } from 'assert'
2
+
3
+ import { pipe } from 'hkt-ts/function'
4
+
5
+ import { collectAll } from './_internal.test.js'
6
+ import { fromCallback } from './fromCallback.js'
7
+ import { merge } from './merge.js'
8
+
9
+ import { testSuite } from '@/_internal/suite.js'
10
+
11
+ testSuite(import.meta.url, () => {
12
+ const stream = pipe(
13
+ fromCallback<never, number>(async ({ event, end }) => {
14
+ await event(1)
15
+ await event(2)
16
+ await event(3)
17
+ await end()
18
+ }),
19
+ merge(
20
+ fromCallback<never, number>(async ({ event, end }) => {
21
+ await event(4)
22
+ await event(5)
23
+ await end()
24
+ }),
25
+ ),
26
+ )
27
+
28
+ it('merges together 2 streams concurrenctly', async () => {
29
+ deepStrictEqual(await collectAll(stream), [1, 4, 2, 5, 3])
30
+ })
31
+ })
@@ -0,0 +1,76 @@
1
+ import { NonNegativeInteger } from 'hkt-ts/number'
2
+
3
+ import { ErrorsOf, OutputOf, ResourcesOf, Stream } from './Stream.js'
4
+ import { empty } from './empty.js'
5
+
6
+ import { AtomicCounter, decrement } from '@/Atomic/AtomicCounter.js'
7
+ import { Fiber } from '@/Fiber/Fiber.js'
8
+ import { both } from '@/Fiber/hkt.js'
9
+ import { FiberContext } from '@/FiberContext/FiberContext.js'
10
+ import { Live } from '@/FiberId/FiberId.js'
11
+ import * as Fx from '@/Fx/index.js'
12
+ import { Scheduler } from '@/Scheduler/Scheduler.js'
13
+ import { Sink, addTrace } from '@/Sink/Sink.js'
14
+
15
+ export function merge<R2, E2, B>(second: Stream<R2, E2, B>, __trace?: string) {
16
+ return <R, E, A>(first: Stream<R, E, A>): Stream<R | R2, E | E2, A | B> =>
17
+ MergeStream.make(first, second, __trace)
18
+ }
19
+
20
+ export function mergeArray<Streams extends ReadonlyArray<Stream<any, any, any>>>(
21
+ streams: readonly [...Streams],
22
+ __trace?: string,
23
+ ): Stream<ResourcesOf<Streams[number]>, ErrorsOf<Streams[number]>, OutputOf<Streams[number]>> {
24
+ if (streams.length === 0) {
25
+ return empty
26
+ }
27
+
28
+ const [first, ...rest] = streams
29
+
30
+ return rest.reduce((acc, stream) => merge(stream, __trace)(acc), first)
31
+ }
32
+
33
+ export class MergeStream<R, E, A, R2, E2, B> implements Stream<R | R2, E | E2, A | B> {
34
+ constructor(
35
+ readonly first: Stream<R, E, A>,
36
+ readonly second: Stream<R2, E2, B>,
37
+ readonly __trace?: string,
38
+ ) {}
39
+
40
+ fork = <E3>(sink: Sink<E | E2, A | B, E3>, scheduler: Scheduler, context: FiberContext<Live>) => {
41
+ const { first, second, __trace } = this
42
+ const mergeSink = addTrace(
43
+ new MergeSink<E | E2, A | B, E3>(sink, AtomicCounter(NonNegativeInteger(2))),
44
+ __trace,
45
+ )
46
+
47
+ return Fx.Fx(function* () {
48
+ const firstFiber: Fiber<E3, any> = yield* first.fork(mergeSink, scheduler, context)
49
+ const secondFiber: Fiber<E3, any> = yield* second.fork(mergeSink, scheduler, context.fork())
50
+
51
+ return both(secondFiber)(firstFiber)
52
+ })
53
+ }
54
+
55
+ static make<R, E, A, R2, E2, B>(
56
+ first: Stream<R, E, A>,
57
+ second: Stream<R2, E2, B>,
58
+ __trace?: string,
59
+ ): Stream<R | R2, E | E2, A | B> {
60
+ return new MergeStream(first, second, __trace)
61
+ }
62
+ }
63
+
64
+ class MergeSink<E, A, E2> implements Sink<E, A, E2> {
65
+ constructor(readonly sink: Sink<E, A, E2>, readonly refCount: AtomicCounter) {}
66
+
67
+ event = this.sink.event
68
+ error = this.sink.error
69
+ end = Fx.lazy(() => {
70
+ if (decrement(this.refCount) === 0) {
71
+ return this.sink.end
72
+ }
73
+
74
+ return Fx.unit
75
+ })
76
+ }
@@ -0,0 +1,32 @@
1
+ import { deepStrictEqual } from 'assert'
2
+
3
+ import { collectAll } from './_internal.test.js'
4
+ import { fromCallback } from './fromCallback.js'
5
+ import { multicast } from './multicast.js'
6
+
7
+ import { testSuite } from '@/_internal/suite.js'
8
+
9
+ testSuite(import.meta.url, () => {
10
+ describe(multicast.name, () => {
11
+ it('effeciently shares underlying stream', async () => {
12
+ let started = 0
13
+ const value = Math.random()
14
+ const stream = multicast(
15
+ fromCallback<never, number>(async ({ event, end }) => {
16
+ started++
17
+ event(value)
18
+ event(value + 1)
19
+ event(value + 2)
20
+ end()
21
+ }),
22
+ )
23
+
24
+ const a = collectAll(stream)
25
+ const b = collectAll(stream)
26
+
27
+ deepStrictEqual(await a, [value, value + 1, value + 2])
28
+ deepStrictEqual(await b, [value, value + 1, value + 2])
29
+ deepStrictEqual(started, 1)
30
+ })
31
+ })
32
+ })
@@ -0,0 +1,114 @@
1
+ import { pipe } from 'hkt-ts'
2
+
3
+ import { Stream } from './Stream.js'
4
+
5
+ import { Cause } from '@/Cause/Cause.js'
6
+ import { fromScope } from '@/Fiber/fromScope.js'
7
+ import { FiberContext } from '@/FiberContext/FiberContext.js'
8
+ import { FiberId } from '@/FiberId/index.js'
9
+ import * as Fx from '@/Fx/index.js'
10
+ import { Scheduler } from '@/Scheduler/index.js'
11
+ import { Sink } from '@/Sink/Sink.js'
12
+ import { Fiber } from '@/index.js'
13
+
14
+ export function multicast<R, E, A>(stream: Stream<R, E, A>) {
15
+ return new MulticastStream(stream)
16
+ }
17
+
18
+ type Observer<E, A, E2> = {
19
+ sink: Sink<E, A, E2>
20
+ scheduler: Scheduler
21
+ context: FiberContext<FiberId.Live>
22
+ }
23
+
24
+ // TODO: how to create a synthetic fiber around multicasted streams?
25
+
26
+ export class MulticastStream<R, E, A> implements Stream<R, E, A>, Sink<E, A, any> {
27
+ protected observers: Array<Observer<E, A, any>> = []
28
+ protected fiber: Fiber.Fiber<any, any> | undefined
29
+
30
+ constructor(readonly stream: Stream<R, E, A>) {
31
+ this.event = this.event.bind(this)
32
+ this.error = this.error.bind(this)
33
+ }
34
+
35
+ fork<E2>(sink: Sink<E, A, E2>, scheduler: Scheduler, context: FiberContext<FiberId.Live>) {
36
+ return Fx.lazy(() => {
37
+ const observer: Observer<E, A, E2> = {
38
+ sink,
39
+ scheduler,
40
+ context,
41
+ }
42
+
43
+ const l = this.observers.length
44
+
45
+ this.observers.push(observer)
46
+
47
+ if (l === 0) {
48
+ return pipe(
49
+ this.stream.fork(this, scheduler, context),
50
+ Fx.tapLazy((fiber) => (this.fiber = fiber)),
51
+ Fx.map(() => this.createFiber(observer)),
52
+ )
53
+ }
54
+
55
+ return Fx.fromLazy(() => this.createFiber(observer))
56
+ })
57
+ }
58
+
59
+ event(a: A) {
60
+ return Fx.zipAll(
61
+ this.observers.map(({ sink, context }) =>
62
+ pipe(sink.event(a), Fx.forkJoinInContext(context.fork())),
63
+ ),
64
+ )
65
+ }
66
+
67
+ error(cause: Cause<E>) {
68
+ return pipe(
69
+ Fx.zipAll(
70
+ this.observers.map(({ sink, context }) =>
71
+ pipe(sink.error(cause), Fx.forkJoinInContext(context.fork())),
72
+ ),
73
+ ),
74
+ Fx.tap((): Fx.Of<any> => (this.fiber ? this.fiber.interruptAs(FiberId.None) : Fx.unit)),
75
+ Fx.tapLazy(() => (this.observers = [])),
76
+ )
77
+ }
78
+
79
+ get end() {
80
+ return pipe(
81
+ Fx.lazy(() =>
82
+ Fx.zipAll(
83
+ this.observers.map(({ sink, context }) =>
84
+ pipe(sink.end, Fx.forkJoinInContext(context.fork())),
85
+ ),
86
+ ),
87
+ ),
88
+ Fx.tapLazy(() => (this.observers = [])),
89
+ )
90
+ }
91
+
92
+ protected createFiber = <E2>(observer: Observer<E, A, E2>) => {
93
+ const { context } = observer
94
+ context.scope.ensuring(() =>
95
+ pipe(
96
+ Fx.fromLazy(() => {
97
+ const index = this.observers.indexOf(observer)
98
+
99
+ if (index >= 0) {
100
+ this.observers.splice(index, 1)
101
+ }
102
+ }),
103
+ Fx.flatMap(
104
+ (): Fx.Of<any> =>
105
+ this.observers.length === 0 && this.fiber
106
+ ? this.fiber.interruptAs(context.id)
107
+ : Fx.unit,
108
+ ),
109
+ ),
110
+ )
111
+
112
+ return fromScope(new FiberId.Synthetic([context.id]), context.fiberRefs, context.scope)
113
+ }
114
+ }
@@ -0,0 +1,107 @@
1
+ import { pipe } from 'hkt-ts'
2
+
3
+ import { Stream } from './Stream.js'
4
+
5
+ import { AtomicCounter, decrement, increment } from '@/Atomic/AtomicCounter.js'
6
+ import { Cause } from '@/Cause/index.js'
7
+ import { Env } from '@/Env/Env.js'
8
+ import { FiberContext } from '@/FiberContext/FiberContext.js'
9
+ import { Live } from '@/FiberId/FiberId.js'
10
+ import * as Fx from '@/Fx/index.js'
11
+ import { access, lazy, unit } from '@/Fx/index.js'
12
+ import { Scheduler } from '@/Scheduler/Scheduler.js'
13
+ import * as Sink from '@/Sink/Sink.js'
14
+ import * as Supervisor from '@/Supervisor/index.js'
15
+
16
+ export function orElse<E, R2, E2, B>(
17
+ f: (cause: Cause<E>) => Stream<R2, E2, B>,
18
+ __trace?: string,
19
+ ): <R, A>(stream: Stream<R, E, A>) => Stream<R | R2, E2, A | B> {
20
+ return (stream) => OrElseStream.make(stream, f, __trace)
21
+ }
22
+
23
+ export class OrElseStream<R, E, A, R2, E2, B> implements Stream<R | R2, E2, A | B> {
24
+ constructor(
25
+ readonly stream: Stream<R, E, A>,
26
+ readonly f: (cause: Cause<E>) => Stream<R2, E2, B>,
27
+ readonly __trace?: string,
28
+ ) {}
29
+
30
+ fork<E3>(sink: Sink.Sink<E2, A | B, E3>, scheduler: Scheduler, context: FiberContext<Live>) {
31
+ const { stream, f } = this
32
+
33
+ return access((env: Env<R | R2>) =>
34
+ stream.fork(
35
+ new OrElseSink(sink, scheduler, context, f, env, this.__trace),
36
+ scheduler,
37
+ context,
38
+ ),
39
+ )
40
+ }
41
+
42
+ static make<R, E, A, R2, E2, B>(
43
+ stream: Stream<R, E, A>,
44
+ f: (cause: Cause<E>) => Stream<R2, E2, B>,
45
+ __trace?: string,
46
+ ): Stream<R | R2, E2, A | B> {
47
+ return new OrElseStream(stream, f, __trace)
48
+ }
49
+ }
50
+
51
+ class OrElseSink<R, E, A, R2, E2, B, E3> implements Sink.Sink<E, A, E3> {
52
+ protected _running = AtomicCounter()
53
+ protected _ended = false
54
+
55
+ constructor(
56
+ readonly sink: Sink.Sink<E2, A | B, E3>,
57
+ readonly scheduler: Scheduler,
58
+ readonly context: FiberContext<Live>,
59
+ readonly f: (cause: Cause<E>) => Stream<R2, E2, B>,
60
+ readonly env: Env<R | R2>,
61
+ readonly __trace?: string,
62
+ ) {}
63
+
64
+ event = this.sink.event
65
+
66
+ error = (cause: Cause<E>) => {
67
+ return lazy(() => {
68
+ const forked = this.context.fork({
69
+ supervisor: Supervisor.and(Supervisor.inheritFiberRefs)(this.context.supervisor),
70
+ })
71
+
72
+ increment(this._running)
73
+
74
+ return pipe(
75
+ this.f(cause).fork(Sink.addTrace(this.innerSink(), this.__trace), this.scheduler, forked),
76
+ Fx.provide(this.env),
77
+ )
78
+ })
79
+ }
80
+
81
+ end = lazy(() => {
82
+ this._ended = true
83
+
84
+ return this.endIfCompleted()
85
+ })
86
+
87
+ protected endIfCompleted() {
88
+ return lazy(() => {
89
+ if (this._ended && this._running.get() === 0) {
90
+ return this.sink.end
91
+ }
92
+
93
+ return unit
94
+ })
95
+ }
96
+
97
+ protected innerSink(): Sink.Sink<E2, B, E3> {
98
+ return {
99
+ event: this.sink.event,
100
+ error: this.sink.error,
101
+ end: lazy(() => {
102
+ decrement(this._running)
103
+ return this.endIfCompleted()
104
+ }),
105
+ }
106
+ }
107
+ }
@@ -5,5 +5,5 @@ import * as Fx from '@/Fx/Fx.js'
5
5
  import * as Schedule from '@/Schedule/index.js'
6
6
  import { Delay } from '@/Time/index.js'
7
7
 
8
- export const periodic = (period: Delay): Stream<never, never, void> =>
9
- scheduled(Schedule.periodic(period))(Fx.unit)
8
+ export const periodic = (period: Delay, __trace?: string): Stream<never, never, void> =>
9
+ scheduled(Schedule.periodic(period), __trace)(Fx.unit)