@clayroach/effect 3.19.14-source-capture.8 → 3.19.14-source-trace.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (452) hide show
  1. package/SourceLocation/package.json +6 -0
  2. package/dist/cjs/Effect.js +2 -28
  3. package/dist/cjs/Effect.js.map +1 -1
  4. package/dist/cjs/FiberRef.js +12 -1
  5. package/dist/cjs/FiberRef.js.map +1 -1
  6. package/dist/cjs/Layer.js +2 -24
  7. package/dist/cjs/Layer.js.map +1 -1
  8. package/dist/cjs/RuntimeFlags.js +1 -29
  9. package/dist/cjs/RuntimeFlags.js.map +1 -1
  10. package/dist/cjs/SourceLocation.js +60 -0
  11. package/dist/cjs/SourceLocation.js.map +1 -0
  12. package/dist/cjs/Tracer.js +1 -15
  13. package/dist/cjs/Tracer.js.map +1 -1
  14. package/dist/cjs/Utils.js +1 -1
  15. package/dist/cjs/Utils.js.map +1 -1
  16. package/dist/cjs/index.js +3 -1
  17. package/dist/cjs/index.js.map +1 -1
  18. package/dist/cjs/internal/clock.js +1 -1
  19. package/dist/cjs/internal/clock.js.map +1 -1
  20. package/dist/cjs/internal/core.js +17 -50
  21. package/dist/cjs/internal/core.js.map +1 -1
  22. package/dist/cjs/internal/effect/circular.js +18 -30
  23. package/dist/cjs/internal/effect/circular.js.map +1 -1
  24. package/dist/cjs/internal/fiberRuntime.js +16 -65
  25. package/dist/cjs/internal/fiberRuntime.js.map +1 -1
  26. package/dist/cjs/internal/layer/circular.js +1 -5
  27. package/dist/cjs/internal/layer/circular.js.map +1 -1
  28. package/dist/cjs/internal/layer.js +1 -3
  29. package/dist/cjs/internal/layer.js.map +1 -1
  30. package/dist/cjs/internal/logger.js +25 -2
  31. package/dist/cjs/internal/logger.js.map +1 -1
  32. package/dist/cjs/internal/runtimeFlags.js +2 -11
  33. package/dist/cjs/internal/runtimeFlags.js.map +1 -1
  34. package/dist/cjs/internal/tracer.js +1 -114
  35. package/dist/cjs/internal/tracer.js.map +1 -1
  36. package/dist/dts/Config.d.ts +2 -2
  37. package/dist/dts/Config.d.ts.map +1 -1
  38. package/dist/dts/Effect.d.ts +8 -29
  39. package/dist/dts/Effect.d.ts.map +1 -1
  40. package/dist/dts/FiberRef.d.ts +12 -0
  41. package/dist/dts/FiberRef.d.ts.map +1 -1
  42. package/dist/dts/Layer.d.ts +0 -22
  43. package/dist/dts/Layer.d.ts.map +1 -1
  44. package/dist/dts/RuntimeFlags.d.ts +0 -28
  45. package/dist/dts/RuntimeFlags.d.ts.map +1 -1
  46. package/dist/dts/SourceLocation.d.ts +88 -0
  47. package/dist/dts/SourceLocation.d.ts.map +1 -0
  48. package/dist/dts/Tracer.d.ts +0 -15
  49. package/dist/dts/Tracer.d.ts.map +1 -1
  50. package/dist/dts/index.d.ts +6 -0
  51. package/dist/dts/index.d.ts.map +1 -1
  52. package/dist/dts/internal/core.d.ts.map +1 -1
  53. package/dist/dts/internal/layer.d.ts.map +1 -1
  54. package/dist/dts/internal/runtimeFlags.d.ts.map +1 -1
  55. package/dist/esm/Effect.js +0 -26
  56. package/dist/esm/Effect.js.map +1 -1
  57. package/dist/esm/FiberRef.js +11 -0
  58. package/dist/esm/FiberRef.js.map +1 -1
  59. package/dist/esm/Layer.js +0 -22
  60. package/dist/esm/Layer.js.map +1 -1
  61. package/dist/esm/RuntimeFlags.js +0 -28
  62. package/dist/esm/RuntimeFlags.js.map +1 -1
  63. package/dist/esm/SourceLocation.js +51 -0
  64. package/dist/esm/SourceLocation.js.map +1 -0
  65. package/dist/esm/Tracer.js +0 -14
  66. package/dist/esm/Tracer.js.map +1 -1
  67. package/dist/esm/Utils.js +1 -1
  68. package/dist/esm/Utils.js.map +1 -1
  69. package/dist/esm/index.js +6 -0
  70. package/dist/esm/index.js.map +1 -1
  71. package/dist/esm/internal/clock.js +1 -1
  72. package/dist/esm/internal/clock.js.map +1 -1
  73. package/dist/esm/internal/core.js +12 -45
  74. package/dist/esm/internal/core.js.map +1 -1
  75. package/dist/esm/internal/effect/circular.js +18 -30
  76. package/dist/esm/internal/effect/circular.js.map +1 -1
  77. package/dist/esm/internal/fiberRuntime.js +13 -60
  78. package/dist/esm/internal/fiberRuntime.js.map +1 -1
  79. package/dist/esm/internal/layer/circular.js +0 -4
  80. package/dist/esm/internal/layer/circular.js.map +1 -1
  81. package/dist/esm/internal/layer.js +0 -2
  82. package/dist/esm/internal/layer.js.map +1 -1
  83. package/dist/esm/internal/logger.js +25 -2
  84. package/dist/esm/internal/logger.js.map +1 -1
  85. package/dist/esm/internal/runtimeFlags.js +1 -9
  86. package/dist/esm/internal/runtimeFlags.js.map +1 -1
  87. package/dist/esm/internal/tracer.js +0 -111
  88. package/dist/esm/internal/tracer.js.map +1 -1
  89. package/package.json +12 -1
  90. package/src/Arbitrary.ts +1101 -0
  91. package/src/Array.ts +3589 -0
  92. package/src/BigDecimal.ts +1349 -0
  93. package/src/BigInt.ts +643 -0
  94. package/src/Boolean.ts +287 -0
  95. package/src/Brand.ts +360 -0
  96. package/src/Cache.ts +281 -0
  97. package/src/Cause.ts +1555 -0
  98. package/src/Channel.ts +2355 -0
  99. package/src/ChildExecutorDecision.ts +146 -0
  100. package/src/Chunk.ts +1495 -0
  101. package/src/Clock.ts +111 -0
  102. package/src/Config.ts +542 -0
  103. package/src/ConfigError.ts +270 -0
  104. package/src/ConfigProvider.ts +333 -0
  105. package/src/ConfigProviderPathPatch.ts +100 -0
  106. package/src/Console.ts +226 -0
  107. package/src/Context.ts +585 -0
  108. package/src/Cron.ts +706 -0
  109. package/src/Data.ts +596 -0
  110. package/src/DateTime.ts +1686 -0
  111. package/src/DefaultServices.ts +34 -0
  112. package/src/Deferred.ts +301 -0
  113. package/src/Differ.ts +450 -0
  114. package/src/Duration.ts +1000 -0
  115. package/src/Effect.ts +14817 -0
  116. package/src/Effectable.ts +107 -0
  117. package/src/Either.ts +1040 -0
  118. package/src/Encoding.ts +195 -0
  119. package/src/Equal.ts +98 -0
  120. package/src/Equivalence.ts +235 -0
  121. package/src/ExecutionPlan.ts +308 -0
  122. package/src/ExecutionStrategy.ts +119 -0
  123. package/src/Exit.ts +467 -0
  124. package/src/FastCheck.ts +9 -0
  125. package/src/Fiber.ts +744 -0
  126. package/src/FiberHandle.ts +540 -0
  127. package/src/FiberId.ts +195 -0
  128. package/src/FiberMap.ts +656 -0
  129. package/src/FiberRef.ts +444 -0
  130. package/src/FiberRefs.ts +204 -0
  131. package/src/FiberRefsPatch.ts +105 -0
  132. package/src/FiberSet.ts +491 -0
  133. package/src/FiberStatus.ts +108 -0
  134. package/src/Function.ts +1222 -0
  135. package/src/GlobalValue.ts +53 -0
  136. package/src/Graph.ts +3732 -0
  137. package/src/GroupBy.ts +103 -0
  138. package/src/HKT.ts +45 -0
  139. package/src/Hash.ts +195 -0
  140. package/src/HashMap.ts +519 -0
  141. package/src/HashRing.ts +317 -0
  142. package/src/HashSet.ts +2346 -0
  143. package/src/Inspectable.ts +287 -0
  144. package/src/Iterable.ts +1119 -0
  145. package/src/JSONSchema.ts +1044 -0
  146. package/src/KeyedPool.ts +167 -0
  147. package/src/Layer.ts +1228 -0
  148. package/src/LayerMap.ts +436 -0
  149. package/src/List.ts +977 -0
  150. package/src/LogLevel.ts +285 -0
  151. package/src/LogSpan.ts +25 -0
  152. package/src/Logger.ts +702 -0
  153. package/src/Mailbox.ts +268 -0
  154. package/src/ManagedRuntime.ts +180 -0
  155. package/src/Match.ts +1477 -0
  156. package/src/MergeDecision.ts +95 -0
  157. package/src/MergeState.ts +172 -0
  158. package/src/MergeStrategy.ts +107 -0
  159. package/src/Metric.ts +780 -0
  160. package/src/MetricBoundaries.ts +69 -0
  161. package/src/MetricHook.ts +151 -0
  162. package/src/MetricKey.ts +224 -0
  163. package/src/MetricKeyType.ts +262 -0
  164. package/src/MetricLabel.ts +47 -0
  165. package/src/MetricPair.ts +71 -0
  166. package/src/MetricPolling.ts +148 -0
  167. package/src/MetricRegistry.ts +48 -0
  168. package/src/MetricState.ts +257 -0
  169. package/src/Micro.ts +4405 -0
  170. package/src/ModuleVersion.ts +18 -0
  171. package/src/MutableHashMap.ts +411 -0
  172. package/src/MutableHashSet.ts +706 -0
  173. package/src/MutableList.ts +297 -0
  174. package/src/MutableQueue.ts +227 -0
  175. package/src/MutableRef.ts +202 -0
  176. package/src/NonEmptyIterable.ts +32 -0
  177. package/src/Number.ts +1071 -0
  178. package/src/Option.ts +2170 -0
  179. package/src/Order.ts +373 -0
  180. package/src/Ordering.ts +111 -0
  181. package/src/ParseResult.ts +2031 -0
  182. package/src/PartitionedSemaphore.ts +200 -0
  183. package/src/Pipeable.ts +566 -0
  184. package/src/Pool.ts +204 -0
  185. package/src/Predicate.ts +1405 -0
  186. package/src/Pretty.ts +205 -0
  187. package/src/PrimaryKey.ts +23 -0
  188. package/src/PubSub.ts +182 -0
  189. package/src/Queue.ts +644 -0
  190. package/src/Random.ts +204 -0
  191. package/src/RateLimiter.ts +138 -0
  192. package/src/RcMap.ts +141 -0
  193. package/src/RcRef.ts +122 -0
  194. package/src/Readable.ts +93 -0
  195. package/src/Record.ts +1274 -0
  196. package/src/RedBlackTree.ts +421 -0
  197. package/src/Redacted.ts +144 -0
  198. package/src/Ref.ts +180 -0
  199. package/src/RegExp.ts +38 -0
  200. package/src/Reloadable.ts +127 -0
  201. package/src/Request.ts +347 -0
  202. package/src/RequestBlock.ts +118 -0
  203. package/src/RequestResolver.ts +366 -0
  204. package/src/Resource.ts +119 -0
  205. package/src/Runtime.ts +383 -0
  206. package/src/RuntimeFlags.ts +336 -0
  207. package/src/RuntimeFlagsPatch.ts +183 -0
  208. package/src/STM.ts +2045 -0
  209. package/src/Schedule.ts +2219 -0
  210. package/src/ScheduleDecision.ts +62 -0
  211. package/src/ScheduleInterval.ts +151 -0
  212. package/src/ScheduleIntervals.ts +122 -0
  213. package/src/Scheduler.ts +353 -0
  214. package/src/Schema.ts +10914 -0
  215. package/src/SchemaAST.ts +3043 -0
  216. package/src/Scope.ts +204 -0
  217. package/src/ScopedCache.ts +151 -0
  218. package/src/ScopedRef.ts +117 -0
  219. package/src/Secret.ts +88 -0
  220. package/src/SingleProducerAsyncInput.ts +67 -0
  221. package/src/Sink.ts +1461 -0
  222. package/src/SortedMap.ts +287 -0
  223. package/src/SortedSet.ts +390 -0
  224. package/src/SourceLocation.ts +108 -0
  225. package/src/Stream.ts +6468 -0
  226. package/src/StreamEmit.ts +136 -0
  227. package/src/StreamHaltStrategy.ts +123 -0
  228. package/src/Streamable.ts +45 -0
  229. package/src/String.ts +778 -0
  230. package/src/Struct.ts +243 -0
  231. package/src/Subscribable.ts +100 -0
  232. package/src/SubscriptionRef.ts +298 -0
  233. package/src/Supervisor.ts +240 -0
  234. package/src/Symbol.ts +29 -0
  235. package/src/SynchronizedRef.ts +270 -0
  236. package/src/TArray.ts +495 -0
  237. package/src/TDeferred.ts +100 -0
  238. package/src/TMap.ts +515 -0
  239. package/src/TPriorityQueue.ts +223 -0
  240. package/src/TPubSub.ts +200 -0
  241. package/src/TQueue.ts +432 -0
  242. package/src/TRandom.ts +129 -0
  243. package/src/TReentrantLock.ts +224 -0
  244. package/src/TRef.ts +178 -0
  245. package/src/TSemaphore.ts +129 -0
  246. package/src/TSet.ts +365 -0
  247. package/src/TSubscriptionRef.ts +192 -0
  248. package/src/Take.ts +258 -0
  249. package/src/TestAnnotation.ts +158 -0
  250. package/src/TestAnnotationMap.ts +119 -0
  251. package/src/TestAnnotations.ts +117 -0
  252. package/src/TestClock.ts +556 -0
  253. package/src/TestConfig.ts +47 -0
  254. package/src/TestContext.ts +36 -0
  255. package/src/TestLive.ts +53 -0
  256. package/src/TestServices.ts +390 -0
  257. package/src/TestSized.ts +55 -0
  258. package/src/Tracer.ts +182 -0
  259. package/src/Trie.ts +840 -0
  260. package/src/Tuple.ts +305 -0
  261. package/src/Types.ts +353 -0
  262. package/src/Unify.ts +113 -0
  263. package/src/UpstreamPullRequest.ts +117 -0
  264. package/src/UpstreamPullStrategy.ts +121 -0
  265. package/src/Utils.ts +809 -0
  266. package/src/index.ts +1568 -0
  267. package/src/internal/array.ts +8 -0
  268. package/src/internal/blockedRequests.ts +520 -0
  269. package/src/internal/cache.ts +733 -0
  270. package/src/internal/cause.ts +1050 -0
  271. package/src/internal/channel/channelExecutor.ts +1200 -0
  272. package/src/internal/channel/channelState.ts +134 -0
  273. package/src/internal/channel/childExecutorDecision.ts +96 -0
  274. package/src/internal/channel/continuation.ts +200 -0
  275. package/src/internal/channel/mergeDecision.ts +113 -0
  276. package/src/internal/channel/mergeState.ts +120 -0
  277. package/src/internal/channel/mergeStrategy.ts +72 -0
  278. package/src/internal/channel/singleProducerAsyncInput.ts +259 -0
  279. package/src/internal/channel/subexecutor.ts +229 -0
  280. package/src/internal/channel/upstreamPullRequest.ts +84 -0
  281. package/src/internal/channel/upstreamPullStrategy.ts +87 -0
  282. package/src/internal/channel.ts +2603 -0
  283. package/src/internal/clock.ts +95 -0
  284. package/src/internal/completedRequestMap.ts +9 -0
  285. package/src/internal/concurrency.ts +54 -0
  286. package/src/internal/config.ts +716 -0
  287. package/src/internal/configError.ts +304 -0
  288. package/src/internal/configProvider/pathPatch.ts +97 -0
  289. package/src/internal/configProvider.ts +799 -0
  290. package/src/internal/console.ts +153 -0
  291. package/src/internal/context.ts +337 -0
  292. package/src/internal/core-effect.ts +2293 -0
  293. package/src/internal/core-stream.ts +998 -0
  294. package/src/internal/core.ts +3189 -0
  295. package/src/internal/data.ts +36 -0
  296. package/src/internal/dataSource.ts +327 -0
  297. package/src/internal/dateTime.ts +1277 -0
  298. package/src/internal/defaultServices/console.ts +100 -0
  299. package/src/internal/defaultServices.ts +163 -0
  300. package/src/internal/deferred.ts +46 -0
  301. package/src/internal/differ/chunkPatch.ts +211 -0
  302. package/src/internal/differ/contextPatch.ts +232 -0
  303. package/src/internal/differ/hashMapPatch.ts +220 -0
  304. package/src/internal/differ/hashSetPatch.ts +176 -0
  305. package/src/internal/differ/orPatch.ts +311 -0
  306. package/src/internal/differ/readonlyArrayPatch.ts +210 -0
  307. package/src/internal/differ.ts +200 -0
  308. package/src/internal/doNotation.ts +80 -0
  309. package/src/internal/effect/circular.ts +895 -0
  310. package/src/internal/effectable.ts +131 -0
  311. package/src/internal/either.ts +110 -0
  312. package/src/internal/encoding/base64.ts +286 -0
  313. package/src/internal/encoding/base64Url.ts +29 -0
  314. package/src/internal/encoding/common.ts +51 -0
  315. package/src/internal/encoding/hex.ts +315 -0
  316. package/src/internal/errors.ts +7 -0
  317. package/src/internal/executionPlan.ts +114 -0
  318. package/src/internal/executionStrategy.ts +74 -0
  319. package/src/internal/fiber.ts +388 -0
  320. package/src/internal/fiberId.ts +267 -0
  321. package/src/internal/fiberMessage.ts +82 -0
  322. package/src/internal/fiberRefs/patch.ts +144 -0
  323. package/src/internal/fiberRefs.ts +297 -0
  324. package/src/internal/fiberRuntime.ts +3842 -0
  325. package/src/internal/fiberScope.ts +71 -0
  326. package/src/internal/fiberStatus.ts +119 -0
  327. package/src/internal/groupBy.ts +530 -0
  328. package/src/internal/hashMap/array.ts +49 -0
  329. package/src/internal/hashMap/bitwise.ts +32 -0
  330. package/src/internal/hashMap/config.ts +14 -0
  331. package/src/internal/hashMap/keySet.ts +8 -0
  332. package/src/internal/hashMap/node.ts +391 -0
  333. package/src/internal/hashMap.ts +586 -0
  334. package/src/internal/hashSet.ts +323 -0
  335. package/src/internal/keyedPool.ts +244 -0
  336. package/src/internal/layer/circular.ts +214 -0
  337. package/src/internal/layer.ts +1483 -0
  338. package/src/internal/logSpan.ts +20 -0
  339. package/src/internal/logger-circular.ts +24 -0
  340. package/src/internal/logger.ts +522 -0
  341. package/src/internal/mailbox.ts +561 -0
  342. package/src/internal/managedRuntime/circular.ts +6 -0
  343. package/src/internal/managedRuntime.ts +134 -0
  344. package/src/internal/matcher.ts +652 -0
  345. package/src/internal/metric/boundaries.ts +75 -0
  346. package/src/internal/metric/hook.ts +483 -0
  347. package/src/internal/metric/key.ts +167 -0
  348. package/src/internal/metric/keyType.ts +238 -0
  349. package/src/internal/metric/label.ts +41 -0
  350. package/src/internal/metric/pair.ts +48 -0
  351. package/src/internal/metric/polling.ts +149 -0
  352. package/src/internal/metric/registry.ts +187 -0
  353. package/src/internal/metric/state.ts +290 -0
  354. package/src/internal/metric.ts +577 -0
  355. package/src/internal/opCodes/cause.ts +35 -0
  356. package/src/internal/opCodes/channel.ts +83 -0
  357. package/src/internal/opCodes/channelChildExecutorDecision.ts +17 -0
  358. package/src/internal/opCodes/channelMergeDecision.ts +11 -0
  359. package/src/internal/opCodes/channelMergeState.ts +17 -0
  360. package/src/internal/opCodes/channelMergeStrategy.ts +11 -0
  361. package/src/internal/opCodes/channelState.ts +23 -0
  362. package/src/internal/opCodes/channelUpstreamPullRequest.ts +11 -0
  363. package/src/internal/opCodes/channelUpstreamPullStrategy.ts +11 -0
  364. package/src/internal/opCodes/config.ts +65 -0
  365. package/src/internal/opCodes/configError.ts +35 -0
  366. package/src/internal/opCodes/continuation.ts +11 -0
  367. package/src/internal/opCodes/deferred.ts +11 -0
  368. package/src/internal/opCodes/effect.ts +89 -0
  369. package/src/internal/opCodes/layer.ts +59 -0
  370. package/src/internal/opCodes/streamHaltStrategy.ts +23 -0
  371. package/src/internal/option.ts +80 -0
  372. package/src/internal/pool.ts +432 -0
  373. package/src/internal/pubsub.ts +1762 -0
  374. package/src/internal/query.ts +204 -0
  375. package/src/internal/queue.ts +766 -0
  376. package/src/internal/random.ts +161 -0
  377. package/src/internal/rateLimiter.ts +93 -0
  378. package/src/internal/rcMap.ts +285 -0
  379. package/src/internal/rcRef.ts +192 -0
  380. package/src/internal/redBlackTree/iterator.ts +200 -0
  381. package/src/internal/redBlackTree/node.ts +68 -0
  382. package/src/internal/redBlackTree.ts +1245 -0
  383. package/src/internal/redacted.ts +73 -0
  384. package/src/internal/ref.ts +171 -0
  385. package/src/internal/reloadable.ts +140 -0
  386. package/src/internal/request.ts +177 -0
  387. package/src/internal/resource.ts +76 -0
  388. package/src/internal/ringBuffer.ts +68 -0
  389. package/src/internal/runtime.ts +558 -0
  390. package/src/internal/runtimeFlags.ts +178 -0
  391. package/src/internal/runtimeFlagsPatch.ts +103 -0
  392. package/src/internal/schedule/decision.ts +47 -0
  393. package/src/internal/schedule/interval.ts +101 -0
  394. package/src/internal/schedule/intervals.ts +180 -0
  395. package/src/internal/schedule.ts +2199 -0
  396. package/src/internal/schema/errors.ts +191 -0
  397. package/src/internal/schema/schemaId.ts +106 -0
  398. package/src/internal/schema/util.ts +50 -0
  399. package/src/internal/scopedCache.ts +644 -0
  400. package/src/internal/scopedRef.ts +118 -0
  401. package/src/internal/secret.ts +89 -0
  402. package/src/internal/singleShotGen.ts +35 -0
  403. package/src/internal/sink.ts +2120 -0
  404. package/src/internal/stack.ts +10 -0
  405. package/src/internal/stm/core.ts +817 -0
  406. package/src/internal/stm/entry.ts +59 -0
  407. package/src/internal/stm/journal.ts +123 -0
  408. package/src/internal/stm/opCodes/stm.ts +71 -0
  409. package/src/internal/stm/opCodes/stmState.ts +17 -0
  410. package/src/internal/stm/opCodes/strategy.ts +17 -0
  411. package/src/internal/stm/opCodes/tExit.ts +29 -0
  412. package/src/internal/stm/opCodes/tryCommit.ts +11 -0
  413. package/src/internal/stm/stm.ts +1453 -0
  414. package/src/internal/stm/stmState.ts +136 -0
  415. package/src/internal/stm/tArray.ts +550 -0
  416. package/src/internal/stm/tDeferred.ts +81 -0
  417. package/src/internal/stm/tExit.ts +190 -0
  418. package/src/internal/stm/tMap.ts +824 -0
  419. package/src/internal/stm/tPriorityQueue.ts +267 -0
  420. package/src/internal/stm/tPubSub.ts +551 -0
  421. package/src/internal/stm/tQueue.ts +393 -0
  422. package/src/internal/stm/tRandom.ts +140 -0
  423. package/src/internal/stm/tReentrantLock.ts +352 -0
  424. package/src/internal/stm/tRef.ts +195 -0
  425. package/src/internal/stm/tSemaphore.ts +113 -0
  426. package/src/internal/stm/tSet.ts +259 -0
  427. package/src/internal/stm/tSubscriptionRef.ts +286 -0
  428. package/src/internal/stm/tryCommit.ts +34 -0
  429. package/src/internal/stm/txnId.ts +14 -0
  430. package/src/internal/stm/versioned.ts +4 -0
  431. package/src/internal/stream/debounceState.ts +57 -0
  432. package/src/internal/stream/emit.ts +123 -0
  433. package/src/internal/stream/haltStrategy.ts +94 -0
  434. package/src/internal/stream/handoff.ts +187 -0
  435. package/src/internal/stream/handoffSignal.ts +59 -0
  436. package/src/internal/stream/pull.ts +34 -0
  437. package/src/internal/stream/sinkEndReason.ts +30 -0
  438. package/src/internal/stream/zipAllState.ts +88 -0
  439. package/src/internal/stream/zipChunksState.ts +56 -0
  440. package/src/internal/stream.ts +8801 -0
  441. package/src/internal/string-utils.ts +107 -0
  442. package/src/internal/subscriptionRef.ts +138 -0
  443. package/src/internal/supervisor/patch.ts +190 -0
  444. package/src/internal/supervisor.ts +303 -0
  445. package/src/internal/synchronizedRef.ts +114 -0
  446. package/src/internal/take.ts +199 -0
  447. package/src/internal/testing/sleep.ts +27 -0
  448. package/src/internal/testing/suspendedWarningData.ts +85 -0
  449. package/src/internal/testing/warningData.ts +94 -0
  450. package/src/internal/tracer.ts +150 -0
  451. package/src/internal/trie.ts +722 -0
  452. package/src/internal/version.ts +7 -0
@@ -0,0 +1,2199 @@
1
+ import type * as Cause from "../Cause.js"
2
+ import * as Chunk from "../Chunk.js"
3
+ import * as Clock from "../Clock.js"
4
+ import * as Context from "../Context.js"
5
+ import * as Cron from "../Cron.js"
6
+ import type * as DateTime from "../DateTime.js"
7
+ import * as Duration from "../Duration.js"
8
+ import type * as Effect from "../Effect.js"
9
+ import * as Either from "../Either.js"
10
+ import * as Equal from "../Equal.js"
11
+ import type * as Fiber from "../Fiber.js"
12
+ import type { LazyArg } from "../Function.js"
13
+ import { constVoid, dual, pipe } from "../Function.js"
14
+ import * as Option from "../Option.js"
15
+ import { pipeArguments } from "../Pipeable.js"
16
+ import { hasProperty, type Predicate } from "../Predicate.js"
17
+ import * as Random from "../Random.js"
18
+ import type * as Ref from "../Ref.js"
19
+ import type * as Schedule from "../Schedule.js"
20
+ import * as ScheduleDecision from "../ScheduleDecision.js"
21
+ import * as Interval from "../ScheduleInterval.js"
22
+ import * as Intervals from "../ScheduleIntervals.js"
23
+ import type { Scope } from "../Scope.js"
24
+ import type * as Types from "../Types.js"
25
+ import * as internalCause from "./cause.js"
26
+ import * as effect from "./core-effect.js"
27
+ import * as core from "./core.js"
28
+ import { forkScoped } from "./effect/circular.js"
29
+ import * as ref from "./ref.js"
30
+
31
+ /** @internal */
32
+ const ScheduleSymbolKey = "effect/Schedule"
33
+
34
+ /** @internal */
35
+ export const ScheduleTypeId: Schedule.ScheduleTypeId = Symbol.for(
36
+ ScheduleSymbolKey
37
+ ) as Schedule.ScheduleTypeId
38
+
39
+ /** @internal */
40
+ export const isSchedule = (u: unknown): u is Schedule.Schedule<unknown, never, unknown> =>
41
+ hasProperty(u, ScheduleTypeId)
42
+
43
+ /** @internal */
44
+ const ScheduleDriverSymbolKey = "effect/ScheduleDriver"
45
+
46
+ /** @internal */
47
+ export const ScheduleDriverTypeId: Schedule.ScheduleDriverTypeId = Symbol.for(
48
+ ScheduleDriverSymbolKey
49
+ ) as Schedule.ScheduleDriverTypeId
50
+
51
+ /** @internal */
52
+ const defaultIterationMetadata: Schedule.IterationMetadata = {
53
+ start: 0,
54
+ now: 0,
55
+ input: undefined,
56
+ output: undefined,
57
+ elapsed: Duration.zero,
58
+ elapsedSincePrevious: Duration.zero,
59
+ recurrence: 0
60
+ }
61
+
62
+ /** @internal */
63
+ export const CurrentIterationMetadata = Context.Reference<Schedule.CurrentIterationMetadata>()(
64
+ "effect/Schedule/CurrentIterationMetadata",
65
+ { defaultValue: () => defaultIterationMetadata }
66
+ )
67
+
68
+ const scheduleVariance = {
69
+ /* c8 ignore next */
70
+ _Out: (_: never) => _,
71
+ /* c8 ignore next */
72
+ _In: (_: unknown) => _,
73
+ /* c8 ignore next */
74
+ _R: (_: never) => _
75
+ }
76
+
77
+ const scheduleDriverVariance = {
78
+ /* c8 ignore next */
79
+ _Out: (_: never) => _,
80
+ /* c8 ignore next */
81
+ _In: (_: unknown) => _,
82
+ /* c8 ignore next */
83
+ _R: (_: never) => _
84
+ }
85
+
86
+ /** @internal */
87
+ class ScheduleImpl<S, Out, In, R> implements Schedule.Schedule<Out, In, R> {
88
+ [ScheduleTypeId] = scheduleVariance
89
+ constructor(
90
+ readonly initial: S,
91
+ readonly step: (
92
+ now: number,
93
+ input: In,
94
+ state: S
95
+ ) => Effect.Effect<readonly [S, Out, ScheduleDecision.ScheduleDecision], never, R>
96
+ ) {
97
+ }
98
+ pipe() {
99
+ return pipeArguments(this, arguments)
100
+ }
101
+ }
102
+
103
+ /** @internal */
104
+ const updateInfo = (
105
+ iterationMetaRef: Ref.Ref<Schedule.IterationMetadata>,
106
+ now: number,
107
+ input: unknown,
108
+ output: unknown
109
+ ) =>
110
+ ref.update(iterationMetaRef, (prev) =>
111
+ (prev.recurrence === 0) ?
112
+ {
113
+ now,
114
+ input,
115
+ output,
116
+ recurrence: prev.recurrence + 1,
117
+ elapsed: Duration.zero,
118
+ elapsedSincePrevious: Duration.zero,
119
+ start: now
120
+ } :
121
+ {
122
+ now,
123
+ input,
124
+ output,
125
+ recurrence: prev.recurrence + 1,
126
+ elapsed: Duration.millis(now - prev.start),
127
+ elapsedSincePrevious: Duration.millis(now - prev.now),
128
+ start: prev.start
129
+ })
130
+
131
+ /** @internal */
132
+ class ScheduleDriverImpl<Out, In, R> implements Schedule.ScheduleDriver<Out, In, R> {
133
+ [ScheduleDriverTypeId] = scheduleDriverVariance
134
+
135
+ constructor(
136
+ readonly schedule: Schedule.Schedule<Out, In, R>,
137
+ readonly ref: Ref.Ref<readonly [Option.Option<Out>, any]>
138
+ ) {}
139
+
140
+ get state(): Effect.Effect<unknown> {
141
+ return core.map(ref.get(this.ref), (tuple) => tuple[1])
142
+ }
143
+
144
+ get last(): Effect.Effect<Out, Cause.NoSuchElementException> {
145
+ return core.flatMap(ref.get(this.ref), ([element, _]) => {
146
+ switch (element._tag) {
147
+ case "None": {
148
+ return core.failSync(() => new core.NoSuchElementException())
149
+ }
150
+ case "Some": {
151
+ return core.succeed(element.value)
152
+ }
153
+ }
154
+ })
155
+ }
156
+
157
+ iterationMeta = ref.unsafeMake(defaultIterationMetadata)
158
+
159
+ get reset(): Effect.Effect<void> {
160
+ return ref.set(this.ref, [Option.none(), this.schedule.initial]).pipe(
161
+ core.zipLeft(ref.set(this.iterationMeta, defaultIterationMetadata))
162
+ )
163
+ }
164
+
165
+ next(input: In): Effect.Effect<Out, Option.Option<never>, R> {
166
+ return pipe(
167
+ core.map(ref.get(this.ref), (tuple) => tuple[1]),
168
+ core.flatMap((state) =>
169
+ pipe(
170
+ Clock.currentTimeMillis,
171
+ core.flatMap((now) =>
172
+ pipe(
173
+ core.suspend(() => this.schedule.step(now, input, state)),
174
+ core.flatMap(([state, out, decision]) => {
175
+ const setState = ref.set(this.ref, [Option.some(out), state] as const)
176
+ if (ScheduleDecision.isDone(decision)) {
177
+ return setState.pipe(
178
+ core.zipRight(core.fail(Option.none()))
179
+ )
180
+ }
181
+ const millis = Intervals.start(decision.intervals) - now
182
+ if (millis <= 0) {
183
+ return setState.pipe(
184
+ core.zipRight(updateInfo(this.iterationMeta, now, input, out)),
185
+ core.as(out)
186
+ )
187
+ }
188
+ const duration = Duration.millis(millis)
189
+ return pipe(
190
+ setState,
191
+ core.zipRight(updateInfo(this.iterationMeta, now, input, out)),
192
+ core.zipRight(effect.sleep(duration)),
193
+ core.as(out)
194
+ )
195
+ })
196
+ )
197
+ )
198
+ )
199
+ )
200
+ )
201
+ }
202
+ }
203
+
204
+ /** @internal */
205
+ export const makeWithState = <S, In, Out, R = never>(
206
+ initial: S,
207
+ step: (
208
+ now: number,
209
+ input: In,
210
+ state: S
211
+ ) => Effect.Effect<readonly [S, Out, ScheduleDecision.ScheduleDecision], never, R>
212
+ ): Schedule.Schedule<Out, In, R> => new ScheduleImpl(initial, step)
213
+
214
+ /** @internal */
215
+ export const addDelay = dual<
216
+ <Out>(
217
+ f: (out: Out) => Duration.DurationInput
218
+ ) => <In, R>(self: Schedule.Schedule<Out, In, R>) => Schedule.Schedule<Out, In, R>,
219
+ <Out, In, R>(
220
+ self: Schedule.Schedule<Out, In, R>,
221
+ f: (out: Out) => Duration.DurationInput
222
+ ) => Schedule.Schedule<Out, In, R>
223
+ >(2, (self, f) => addDelayEffect(self, (out) => core.sync(() => f(out))))
224
+
225
+ /** @internal */
226
+ export const addDelayEffect = dual<
227
+ <Out, R2>(
228
+ f: (out: Out) => Effect.Effect<Duration.DurationInput, never, R2>
229
+ ) => <In, R>(self: Schedule.Schedule<Out, In, R>) => Schedule.Schedule<Out, In, R | R2>,
230
+ <Out, In, R, R2>(
231
+ self: Schedule.Schedule<Out, In, R>,
232
+ f: (out: Out) => Effect.Effect<Duration.DurationInput, never, R2>
233
+ ) => Schedule.Schedule<Out, In, R | R2>
234
+ >(2, (self, f) =>
235
+ modifyDelayEffect(self, (out, duration) =>
236
+ core.map(
237
+ f(out),
238
+ (delay) => Duration.sum(duration, Duration.decode(delay))
239
+ )))
240
+
241
+ /** @internal */
242
+ export const andThen = dual<
243
+ <Out2, In2, R2>(
244
+ that: Schedule.Schedule<Out2, In2, R2>
245
+ ) => <Out, In, R>(self: Schedule.Schedule<Out, In, R>) => Schedule.Schedule<
246
+ Out | Out2,
247
+ In & In2,
248
+ R | R2
249
+ >,
250
+ <Out, In, R, Out2, In2, R2>(
251
+ self: Schedule.Schedule<Out, In, R>,
252
+ that: Schedule.Schedule<Out2, In2, R2>
253
+ ) => Schedule.Schedule<
254
+ Out | Out2,
255
+ In & In2,
256
+ R | R2
257
+ >
258
+ >(2, (self, that) => map(andThenEither(self, that), Either.merge))
259
+
260
+ /** @internal */
261
+ export const andThenEither = dual<
262
+ <Out2, In2, R2>(
263
+ that: Schedule.Schedule<Out2, In2, R2>
264
+ ) => <Out, In, R>(
265
+ self: Schedule.Schedule<Out, In, R>
266
+ ) => Schedule.Schedule<Either.Either<Out2, Out>, In & In2, R | R2>,
267
+ <Out, In, R, Out2, In2, R2>(
268
+ self: Schedule.Schedule<Out, In, R>,
269
+ that: Schedule.Schedule<Out2, In2, R2>
270
+ ) => Schedule.Schedule<Either.Either<Out2, Out>, In & In2, R | R2>
271
+ >(2, <Out, In, R, Out2, In2, R2>(
272
+ self: Schedule.Schedule<Out, In, R>,
273
+ that: Schedule.Schedule<Out2, In2, R2>
274
+ ): Schedule.Schedule<Either.Either<Out2, Out>, In & In2, R | R2> =>
275
+ makeWithState(
276
+ [self.initial, that.initial, true as boolean] as const,
277
+ (now, input, state) =>
278
+ state[2] ?
279
+ core.flatMap(self.step(now, input, state[0]), ([lState, out, decision]) => {
280
+ if (ScheduleDecision.isDone(decision)) {
281
+ return core.map(that.step(now, input, state[1]), ([rState, out, decision]) =>
282
+ [
283
+ [lState, rState, false as boolean] as const,
284
+ Either.right(out) as Either.Either<Out2, Out>,
285
+ decision as ScheduleDecision.ScheduleDecision
286
+ ] as const)
287
+ }
288
+ return core.succeed(
289
+ [
290
+ [lState, state[1], true as boolean] as const,
291
+ Either.left(out),
292
+ decision
293
+ ] as const
294
+ )
295
+ }) :
296
+ core.map(that.step(now, input, state[1]), ([rState, out, decision]) =>
297
+ [
298
+ [state[0], rState, false as boolean] as const,
299
+ Either.right(out) as Either.Either<Out2, Out>,
300
+ decision
301
+ ] as const)
302
+ ))
303
+
304
+ /** @internal */
305
+ export const as = dual<
306
+ <Out2>(out: Out2) => <Out, In, R>(self: Schedule.Schedule<Out, In, R>) => Schedule.Schedule<Out2, In, R>,
307
+ <Out, In, R, Out2>(self: Schedule.Schedule<Out, In, R>, out: Out2) => Schedule.Schedule<Out2, In, R>
308
+ >(2, (self, out) => map(self, () => out))
309
+
310
+ /** @internal */
311
+ export const asVoid = <Out, In, R>(
312
+ self: Schedule.Schedule<Out, In, R>
313
+ ): Schedule.Schedule<void, In, R> => map(self, constVoid)
314
+
315
+ /** @internal */
316
+ export const bothInOut = dual<
317
+ <Out2, In2, R2>(
318
+ that: Schedule.Schedule<Out2, In2, R2>
319
+ ) => <Out, In, R>(
320
+ self: Schedule.Schedule<Out, In, R>
321
+ ) => Schedule.Schedule<[Out, Out2], readonly [In, In2], R | R2>,
322
+ <Out, In, R, Out2, In2, R2>(
323
+ self: Schedule.Schedule<Out, In, R>,
324
+ that: Schedule.Schedule<Out2, In2, R2>
325
+ ) => Schedule.Schedule<[Out, Out2], readonly [In, In2], R | R2>
326
+ >(2, (self, that) =>
327
+ makeWithState([self.initial, that.initial], (now, [in1, in2], state) =>
328
+ core.zipWith(
329
+ self.step(now, in1, state[0]),
330
+ that.step(now, in2, state[1]),
331
+ ([lState, out, lDecision], [rState, out2, rDecision]) => {
332
+ if (ScheduleDecision.isContinue(lDecision) && ScheduleDecision.isContinue(rDecision)) {
333
+ const interval = pipe(lDecision.intervals, Intervals.union(rDecision.intervals))
334
+ return [
335
+ [lState, rState],
336
+ [out, out2],
337
+ ScheduleDecision.continue(interval)
338
+ ]
339
+ }
340
+ return [[lState, rState], [out, out2], ScheduleDecision.done]
341
+ }
342
+ )))
343
+
344
+ /** @internal */
345
+ export const check = dual<
346
+ <In, Out>(
347
+ test: (input: In, output: Out) => boolean
348
+ ) => <R>(self: Schedule.Schedule<Out, In, R>) => Schedule.Schedule<Out, In, R>,
349
+ <Out, In, R>(
350
+ self: Schedule.Schedule<Out, In, R>,
351
+ test: (input: In, output: Out) => boolean
352
+ ) => Schedule.Schedule<Out, In, R>
353
+ >(2, (self, test) => checkEffect(self, (input, out) => core.sync(() => test(input, out))))
354
+
355
+ /** @internal */
356
+ export const checkEffect = dual<
357
+ <In, Out, R2>(
358
+ test: (input: In, output: Out) => Effect.Effect<boolean, never, R2>
359
+ ) => <R>(self: Schedule.Schedule<Out, In, R>) => Schedule.Schedule<Out, In, R | R2>,
360
+ <Out, In, R, R2>(
361
+ self: Schedule.Schedule<Out, In, R>,
362
+ test: (input: In, output: Out) => Effect.Effect<boolean, never, R2>
363
+ ) => Schedule.Schedule<Out, In, R | R2>
364
+ >(2, (self, test) =>
365
+ makeWithState(
366
+ self.initial,
367
+ (now, input, state) =>
368
+ core.flatMap(self.step(now, input, state), ([state, out, decision]) => {
369
+ if (ScheduleDecision.isDone(decision)) {
370
+ return core.succeed([state, out, ScheduleDecision.done] as const)
371
+ }
372
+ return core.map(test(input, out), (cont) =>
373
+ cont ?
374
+ [state, out, decision] as const :
375
+ [state, out, ScheduleDecision.done] as const)
376
+ })
377
+ ))
378
+ /** @internal */
379
+ export const collectAllInputs = <A>(): Schedule.Schedule<Chunk.Chunk<A>, A> => collectAllOutputs(identity<A>())
380
+
381
+ /** @internal */
382
+ export const collectAllOutputs = <Out, In, R>(
383
+ self: Schedule.Schedule<Out, In, R>
384
+ ): Schedule.Schedule<Chunk.Chunk<Out>, In, R> =>
385
+ reduce(self, Chunk.empty<Out>(), (outs, out) => pipe(outs, Chunk.append(out)))
386
+
387
+ /** @internal */
388
+ export const collectUntil = <A>(f: Predicate<A>): Schedule.Schedule<Chunk.Chunk<A>, A> =>
389
+ collectAllOutputs(recurUntil(f))
390
+
391
+ /** @internal */
392
+ export const collectUntilEffect = <A, R>(
393
+ f: (a: A) => Effect.Effect<boolean, never, R>
394
+ ): Schedule.Schedule<Chunk.Chunk<A>, A, R> => collectAllOutputs(recurUntilEffect(f))
395
+
396
+ /** @internal */
397
+ export const collectWhile = <A>(f: Predicate<A>): Schedule.Schedule<Chunk.Chunk<A>, A> =>
398
+ collectAllOutputs(recurWhile(f))
399
+
400
+ /** @internal */
401
+ export const collectWhileEffect = <A, R>(
402
+ f: (a: A) => Effect.Effect<boolean, never, R>
403
+ ): Schedule.Schedule<Chunk.Chunk<A>, A, R> => collectAllOutputs(recurWhileEffect(f))
404
+
405
+ /** @internal */
406
+ export const compose = dual<
407
+ <Out2, Out, R2>(
408
+ that: Schedule.Schedule<Out2, Out, R2>
409
+ ) => <In, R>(self: Schedule.Schedule<Out, In, R>) => Schedule.Schedule<Out2, In, R | R2>,
410
+ <Out, In, R, Out2, R2>(
411
+ self: Schedule.Schedule<Out, In, R>,
412
+ that: Schedule.Schedule<Out2, Out, R2>
413
+ ) => Schedule.Schedule<Out2, In, R | R2>
414
+ >(2, (self, that) =>
415
+ makeWithState(
416
+ [self.initial, that.initial] as const,
417
+ (now, input, state) =>
418
+ core.flatMap(
419
+ self.step(now, input, state[0]),
420
+ ([lState, out, lDecision]) =>
421
+ core.map(that.step(now, out, state[1]), ([rState, out2, rDecision]) =>
422
+ ScheduleDecision.isDone(lDecision)
423
+ ? [[lState, rState] as const, out2, ScheduleDecision.done] as const
424
+ : ScheduleDecision.isDone(rDecision)
425
+ ? [[lState, rState] as const, out2, ScheduleDecision.done] as const
426
+ : [
427
+ [lState, rState] as const,
428
+ out2,
429
+ ScheduleDecision.continue(pipe(lDecision.intervals, Intervals.max(rDecision.intervals)))
430
+ ] as const)
431
+ )
432
+ ))
433
+
434
+ /** @internal */
435
+ export const mapInput = dual<
436
+ <In, In2>(
437
+ f: (in2: In2) => In
438
+ ) => <Out, R>(self: Schedule.Schedule<Out, In, R>) => Schedule.Schedule<Out, In2, R>,
439
+ <Out, In, R, In2>(
440
+ self: Schedule.Schedule<Out, In, R>,
441
+ f: (in2: In2) => In
442
+ ) => Schedule.Schedule<Out, In2, R>
443
+ >(2, (self, f) => mapInputEffect(self, (input2) => core.sync(() => f(input2))))
444
+
445
+ /** @internal */
446
+ export const mapInputContext = dual<
447
+ <R0, R>(
448
+ f: (env0: Context.Context<R0>) => Context.Context<R>
449
+ ) => <Out, In>(self: Schedule.Schedule<Out, In, R>) => Schedule.Schedule<Out, In, R0>,
450
+ <Out, In, R, R0>(
451
+ self: Schedule.Schedule<Out, In, R>,
452
+ f: (env0: Context.Context<R0>) => Context.Context<R>
453
+ ) => Schedule.Schedule<Out, In, R0>
454
+ >(2, (self, f) =>
455
+ makeWithState(
456
+ self.initial,
457
+ (now, input, state) => core.mapInputContext(self.step(now, input, state), f)
458
+ ))
459
+
460
+ /** @internal */
461
+ export const mapInputEffect = dual<
462
+ <In2, In, R2>(
463
+ f: (in2: In2) => Effect.Effect<In, never, R2>
464
+ ) => <Out, R>(self: Schedule.Schedule<Out, In, R>) => Schedule.Schedule<Out, In2, R | R2>,
465
+ <Out, In, R, In2, R2>(
466
+ self: Schedule.Schedule<Out, In, R>,
467
+ f: (in2: In2) => Effect.Effect<In, never, R2>
468
+ ) => Schedule.Schedule<Out, In2, R | R2>
469
+ >(2, (self, f) =>
470
+ makeWithState(self.initial, (now, input2, state) =>
471
+ core.flatMap(
472
+ f(input2),
473
+ (input) => self.step(now, input, state)
474
+ )))
475
+
476
+ /** @internal */
477
+ export const cron: {
478
+ (expression: Cron.Cron): Schedule.Schedule<[number, number]>
479
+ (expression: string, tz?: DateTime.TimeZone | string): Schedule.Schedule<[number, number]>
480
+ } = (expression: string | Cron.Cron, tz?: DateTime.TimeZone | string): Schedule.Schedule<[number, number]> => {
481
+ const parsed = Cron.isCron(expression) ? Either.right(expression) : Cron.parse(expression, tz)
482
+ return makeWithState<[boolean, [number, number, number]], unknown, [number, number]>(
483
+ [true, [Number.MIN_SAFE_INTEGER, 0, 0]],
484
+ (now, _, [initial, previous]) => {
485
+ if (now < previous[0]) {
486
+ return core.succeed([
487
+ [false, previous],
488
+ [previous[1], previous[2]],
489
+ ScheduleDecision.continueWith(Interval.make(previous[1], previous[2]))
490
+ ])
491
+ }
492
+
493
+ if (Either.isLeft(parsed)) {
494
+ return core.die(parsed.left)
495
+ }
496
+
497
+ const cron = parsed.right
498
+ const date = new Date(now)
499
+
500
+ let next: number
501
+ if (initial && Cron.match(cron, date)) {
502
+ next = now
503
+ }
504
+
505
+ next = Cron.next(cron, date).getTime()
506
+ const start = beginningOfSecond(next)
507
+ const end = endOfSecond(next)
508
+ return core.succeed([
509
+ [false, [next, start, end]],
510
+ [start, end],
511
+ ScheduleDecision.continueWith(Interval.make(start, end))
512
+ ])
513
+ }
514
+ )
515
+ }
516
+
517
+ /** @internal */
518
+ export const dayOfMonth = (day: number): Schedule.Schedule<number> => {
519
+ return makeWithState<[number, number], unknown, number>(
520
+ [Number.NEGATIVE_INFINITY, 0],
521
+ (now, _, state) => {
522
+ if (!Number.isInteger(day) || day < 1 || 31 < day) {
523
+ return core.dieSync(() =>
524
+ new core.IllegalArgumentException(
525
+ `Invalid argument in: dayOfMonth(${day}). Must be in range 1...31`
526
+ )
527
+ )
528
+ }
529
+ const n = state[1]
530
+ const initial = n === 0
531
+ const day0 = nextDayOfMonth(now, day, initial)
532
+ const start = beginningOfDay(day0)
533
+ const end = endOfDay(day0)
534
+ const interval = Interval.make(start, end)
535
+ return core.succeed(
536
+ [
537
+ [end, n + 1],
538
+ n,
539
+ ScheduleDecision.continueWith(interval)
540
+ ]
541
+ )
542
+ }
543
+ )
544
+ }
545
+
546
+ /** @internal */
547
+ export const dayOfWeek = (day: number): Schedule.Schedule<number> => {
548
+ return makeWithState<[number, number], unknown, number>(
549
+ [Number.MIN_SAFE_INTEGER, 0],
550
+ (now, _, state) => {
551
+ if (!Number.isInteger(day) || day < 1 || 7 < day) {
552
+ return core.dieSync(() =>
553
+ new core.IllegalArgumentException(
554
+ `Invalid argument in: dayOfWeek(${day}). Must be in range 1 (Monday)...7 (Sunday)`
555
+ )
556
+ )
557
+ }
558
+ const n = state[1]
559
+ const initial = n === 0
560
+ const day0 = nextDay(now, day, initial)
561
+ const start = beginningOfDay(day0)
562
+ const end = endOfDay(day0)
563
+ const interval = Interval.make(start, end)
564
+ return core.succeed(
565
+ [
566
+ [end, n + 1],
567
+ n,
568
+ ScheduleDecision.continueWith(interval)
569
+ ]
570
+ )
571
+ }
572
+ )
573
+ }
574
+
575
+ /** @internal */
576
+ export const delayed = dual<
577
+ (
578
+ f: (duration: Duration.Duration) => Duration.DurationInput
579
+ ) => <Out, In, R>(self: Schedule.Schedule<Out, In, R>) => Schedule.Schedule<Out, In, R>,
580
+ <Out, In, R>(
581
+ self: Schedule.Schedule<Out, In, R>,
582
+ f: (duration: Duration.Duration) => Duration.DurationInput
583
+ ) => Schedule.Schedule<Out, In, R>
584
+ >(2, (self, f) => delayedEffect(self, (duration) => core.sync(() => f(duration))))
585
+
586
+ /** @internal */
587
+ export const delayedEffect = dual<
588
+ <R2>(
589
+ f: (duration: Duration.Duration) => Effect.Effect<Duration.DurationInput, never, R2>
590
+ ) => <Out, In, R>(self: Schedule.Schedule<Out, In, R>) => Schedule.Schedule<Out, In, R | R2>,
591
+ <Out, In, R, R2>(
592
+ self: Schedule.Schedule<Out, In, R>,
593
+ f: (duration: Duration.Duration) => Effect.Effect<Duration.DurationInput, never, R2>
594
+ ) => Schedule.Schedule<Out, In, R | R2>
595
+ >(2, (self, f) => modifyDelayEffect(self, (_, delay) => f(delay)))
596
+
597
+ /** @internal */
598
+ export const delayedSchedule = <In, R>(
599
+ schedule: Schedule.Schedule<Duration.Duration, In, R>
600
+ ): Schedule.Schedule<Duration.Duration, In, R> => addDelay(schedule, (x) => x)
601
+
602
+ /** @internal */
603
+ export const delays = <Out, In, R>(
604
+ self: Schedule.Schedule<Out, In, R>
605
+ ): Schedule.Schedule<Duration.Duration, In, R> =>
606
+ makeWithState(self.initial, (now, input, state) =>
607
+ pipe(
608
+ self.step(now, input, state),
609
+ core.flatMap((
610
+ [state, _, decision]
611
+ ): Effect.Effect<[any, Duration.Duration, ScheduleDecision.ScheduleDecision]> => {
612
+ if (ScheduleDecision.isDone(decision)) {
613
+ return core.succeed([state, Duration.zero, decision])
614
+ }
615
+ return core.succeed(
616
+ [
617
+ state,
618
+ Duration.millis(Intervals.start(decision.intervals) - now),
619
+ decision
620
+ ]
621
+ )
622
+ })
623
+ ))
624
+
625
+ /** @internal */
626
+ export const mapBoth = dual<
627
+ <In2, In, Out, Out2>(
628
+ options: {
629
+ readonly onInput: (in2: In2) => In
630
+ readonly onOutput: (out: Out) => Out2
631
+ }
632
+ ) => <R>(self: Schedule.Schedule<Out, In, R>) => Schedule.Schedule<Out2, In2, R>,
633
+ <Out, In, R, In2, Out2>(
634
+ self: Schedule.Schedule<Out, In, R>,
635
+ options: {
636
+ readonly onInput: (in2: In2) => In
637
+ readonly onOutput: (out: Out) => Out2
638
+ }
639
+ ) => Schedule.Schedule<Out2, In2, R>
640
+ >(2, (self, { onInput, onOutput }) => map(mapInput(self, onInput), onOutput))
641
+
642
+ /** @internal */
643
+ export const mapBothEffect = dual<
644
+ <In2, In, R2, Out, R3, Out2>(
645
+ options: {
646
+ readonly onInput: (input: In2) => Effect.Effect<In, never, R2>
647
+ readonly onOutput: (out: Out) => Effect.Effect<Out2, never, R3>
648
+ }
649
+ ) => <R>(self: Schedule.Schedule<Out, In, R>) => Schedule.Schedule<Out2, In2, R | R2 | R3>,
650
+ <Out, In, R, In2, R2, Out2, R3>(
651
+ self: Schedule.Schedule<Out, In, R>,
652
+ options: {
653
+ readonly onInput: (input: In2) => Effect.Effect<In, never, R2>
654
+ readonly onOutput: (out: Out) => Effect.Effect<Out2, never, R3>
655
+ }
656
+ ) => Schedule.Schedule<Out2, In2, R | R2 | R3>
657
+ >(2, (self, { onInput, onOutput }) => mapEffect(mapInputEffect(self, onInput), onOutput))
658
+
659
+ /** @internal */
660
+ export const driver = <Out, In, R>(
661
+ self: Schedule.Schedule<Out, In, R>
662
+ ): Effect.Effect<Schedule.ScheduleDriver<Out, In, R>> =>
663
+ pipe(
664
+ ref.make<readonly [Option.Option<Out>, any]>([Option.none(), self.initial]),
665
+ core.map((ref) => new ScheduleDriverImpl(self, ref))
666
+ )
667
+
668
+ /** @internal */
669
+ export const duration = (
670
+ durationInput: Duration.DurationInput
671
+ ): Schedule.Schedule<Duration.Duration> => {
672
+ const duration = Duration.decode(durationInput)
673
+ const durationMillis = Duration.toMillis(duration)
674
+ return makeWithState(true as boolean, (now, _, state) =>
675
+ core.succeed(
676
+ state
677
+ ? [
678
+ false,
679
+ duration,
680
+ ScheduleDecision.continueWith(Interval.after(now + durationMillis))
681
+ ] as const
682
+ : [false, Duration.zero, ScheduleDecision.done] as const
683
+ ))
684
+ }
685
+
686
+ /** @internal */
687
+ export const either = dual<
688
+ <Out2, In2, R2>(
689
+ that: Schedule.Schedule<Out2, In2, R2>
690
+ ) => <Out, In, R>(self: Schedule.Schedule<Out, In, R>) => Schedule.Schedule<[Out, Out2], In & In2, R | R2>,
691
+ <Out, In, R, Out2, In2, R2>(
692
+ self: Schedule.Schedule<Out, In, R>,
693
+ that: Schedule.Schedule<Out2, In2, R2>
694
+ ) => Schedule.Schedule<[Out, Out2], In & In2, R | R2>
695
+ >(2, (self, that) => union(self, that))
696
+
697
+ /** @internal */
698
+ export const eitherWith = dual<
699
+ <Out2, In2, R2>(
700
+ that: Schedule.Schedule<Out2, In2, R2>,
701
+ f: (x: Intervals.Intervals, y: Intervals.Intervals) => Intervals.Intervals
702
+ ) => <Out, In, R>(self: Schedule.Schedule<Out, In, R>) => Schedule.Schedule<[Out, Out2], In & In2, R | R2>,
703
+ <Out, In, R, Out2, In2, R2>(
704
+ self: Schedule.Schedule<Out, In, R>,
705
+ that: Schedule.Schedule<Out2, In2, R2>,
706
+ f: (x: Intervals.Intervals, y: Intervals.Intervals) => Intervals.Intervals
707
+ ) => Schedule.Schedule<[Out, Out2], In & In2, R | R2>
708
+ >(3, (self, that, f) => unionWith(self, that, f))
709
+
710
+ /** @internal */
711
+ export const ensuring = dual<
712
+ <X>(
713
+ finalizer: Effect.Effect<X>
714
+ ) => <Out, In, R>(self: Schedule.Schedule<Out, In, R>) => Schedule.Schedule<Out, In, R>,
715
+ <Out, In, R, X>(
716
+ self: Schedule.Schedule<Out, In, R>,
717
+ finalizer: Effect.Effect<X>
718
+ ) => Schedule.Schedule<Out, In, R>
719
+ >(2, (self, finalizer) =>
720
+ makeWithState(
721
+ self.initial,
722
+ (now, input, state) =>
723
+ core.flatMap(self.step(now, input, state), ([state, out, decision]) =>
724
+ ScheduleDecision.isDone(decision)
725
+ ? core.as(finalizer, [state, out, decision as ScheduleDecision.ScheduleDecision] as const)
726
+ : core.succeed([state, out, decision] as const))
727
+ ))
728
+
729
+ /** @internal */
730
+ export const exponential = (
731
+ baseInput: Duration.DurationInput,
732
+ factor = 2.0
733
+ ): Schedule.Schedule<Duration.Duration> => {
734
+ const base = Duration.decode(baseInput)
735
+ return delayedSchedule(
736
+ map(forever, (i) => Duration.times(base, Math.pow(factor, i)))
737
+ )
738
+ }
739
+
740
+ /** @internal */
741
+ export const fibonacci = (oneInput: Duration.DurationInput): Schedule.Schedule<Duration.Duration> => {
742
+ const one = Duration.decode(oneInput)
743
+ return delayedSchedule(
744
+ pipe(
745
+ unfold(
746
+ [one, one] as const,
747
+ ([a, b]) => [b, Duration.sum(a, b)] as const
748
+ ),
749
+ map((out) => out[0])
750
+ )
751
+ )
752
+ }
753
+
754
+ /** @internal */
755
+ export const fixed = (intervalInput: Duration.DurationInput): Schedule.Schedule<number> => {
756
+ const interval = Duration.decode(intervalInput)
757
+ const intervalMillis = Duration.toMillis(interval)
758
+ return makeWithState<[Option.Option<[number, number]>, number], unknown, number>(
759
+ [Option.none(), 0],
760
+ (now, _, [option, n]) =>
761
+ core.sync(() => {
762
+ switch (option._tag) {
763
+ case "None": {
764
+ return [
765
+ [Option.some([now, now + intervalMillis]), n + 1],
766
+ n,
767
+ ScheduleDecision.continueWith(Interval.after(now + intervalMillis))
768
+ ]
769
+ }
770
+ case "Some": {
771
+ const [startMillis, lastRun] = option.value
772
+ const runningBehind = now > (lastRun + intervalMillis)
773
+ const boundary = Equal.equals(interval, Duration.zero)
774
+ ? interval
775
+ : Duration.millis(intervalMillis - ((now - startMillis) % intervalMillis))
776
+ const sleepTime = Equal.equals(boundary, Duration.zero) ? interval : boundary
777
+ const nextRun = runningBehind ? now : now + Duration.toMillis(sleepTime)
778
+ return [
779
+ [Option.some([startMillis, nextRun]), n + 1],
780
+ n,
781
+ ScheduleDecision.continueWith(Interval.after(nextRun))
782
+ ]
783
+ }
784
+ }
785
+ })
786
+ )
787
+ }
788
+
789
+ /** @internal */
790
+ export const fromDelay = (delay: Duration.DurationInput): Schedule.Schedule<Duration.Duration> => duration(delay)
791
+
792
+ /** @internal */
793
+ export const fromDelays = (
794
+ delay: Duration.DurationInput,
795
+ ...delays: Array<Duration.DurationInput>
796
+ ): Schedule.Schedule<Duration.Duration> =>
797
+ makeWithState(
798
+ [[delay, ...delays].map((_) => Duration.decode(_)) as Array<Duration.Duration>, true as boolean] as const,
799
+ (now, _, [durations, cont]) =>
800
+ core.sync(() => {
801
+ if (cont) {
802
+ const x = durations[0]!
803
+ const interval = Interval.after(now + Duration.toMillis(x))
804
+ if (durations.length >= 2) {
805
+ return [
806
+ [durations.slice(1), true] as const,
807
+ x,
808
+ ScheduleDecision.continueWith(interval)
809
+ ] as const
810
+ }
811
+ const y = durations.slice(1)
812
+ return [
813
+ [[x, ...y] as Array<Duration.Duration>, false] as const,
814
+ x,
815
+ ScheduleDecision.continueWith(interval)
816
+ ] as const
817
+ }
818
+ return [[durations, false] as const, Duration.zero, ScheduleDecision.done] as const
819
+ })
820
+ )
821
+
822
+ /** @internal */
823
+ export const fromFunction = <A, B>(f: (a: A) => B): Schedule.Schedule<B, A> => map(identity<A>(), f)
824
+
825
+ /** @internal */
826
+ export const hourOfDay = (hour: number): Schedule.Schedule<number> =>
827
+ makeWithState<[number, number], unknown, number>(
828
+ [Number.NEGATIVE_INFINITY, 0],
829
+ (now, _, state) => {
830
+ if (!Number.isInteger(hour) || hour < 0 || 23 < hour) {
831
+ return core.dieSync(() =>
832
+ new core.IllegalArgumentException(
833
+ `Invalid argument in: hourOfDay(${hour}). Must be in range 0...23`
834
+ )
835
+ )
836
+ }
837
+ const n = state[1]
838
+ const initial = n === 0
839
+ const hour0 = nextHour(now, hour, initial)
840
+ const start = beginningOfHour(hour0)
841
+ const end = endOfHour(hour0)
842
+ const interval = Interval.make(start, end)
843
+ return core.succeed(
844
+ [
845
+ [end, n + 1],
846
+ n,
847
+ ScheduleDecision.continueWith(interval)
848
+ ]
849
+ )
850
+ }
851
+ )
852
+
853
+ /** @internal */
854
+ export const identity = <A>(): Schedule.Schedule<A, A> =>
855
+ makeWithState(void 0, (now, input, state) =>
856
+ core.succeed(
857
+ [
858
+ state,
859
+ input,
860
+ ScheduleDecision.continueWith(Interval.after(now))
861
+ ] as const
862
+ ))
863
+
864
+ /** @internal */
865
+ export const intersect = dual<
866
+ <Out2, In2, R2>(
867
+ that: Schedule.Schedule<Out2, In2, R2>
868
+ ) => <Out, In, R>(self: Schedule.Schedule<Out, In, R>) => Schedule.Schedule<[Out, Out2], In & In2, R | R2>,
869
+ <Out, In, R, Out2, In2, R2>(
870
+ self: Schedule.Schedule<Out, In, R>,
871
+ that: Schedule.Schedule<Out2, In2, R2>
872
+ ) => Schedule.Schedule<[Out, Out2], In & In2, R | R2>
873
+ >(2, (self, that) => intersectWith(self, that, Intervals.intersect))
874
+
875
+ /** @internal */
876
+ export const intersectWith = dual<
877
+ <Out2, In2, R2>(
878
+ that: Schedule.Schedule<Out2, In2, R2>,
879
+ f: (x: Intervals.Intervals, y: Intervals.Intervals) => Intervals.Intervals
880
+ ) => <Out, In, R>(self: Schedule.Schedule<Out, In, R>) => Schedule.Schedule<[Out, Out2], In & In2, R | R2>,
881
+ <Out, In, R, Out2, In2, R2>(
882
+ self: Schedule.Schedule<Out, In, R>,
883
+ that: Schedule.Schedule<Out2, In2, R2>,
884
+ f: (x: Intervals.Intervals, y: Intervals.Intervals) => Intervals.Intervals
885
+ ) => Schedule.Schedule<[Out, Out2], In & In2, R | R2>
886
+ >(3, <Env, In, Out, Env2, In2, Out2>(
887
+ self: Schedule.Schedule<Out, In, Env>,
888
+ that: Schedule.Schedule<Out2, In2, Env2>,
889
+ f: (x: Intervals.Intervals, y: Intervals.Intervals) => Intervals.Intervals
890
+ ): Schedule.Schedule<[Out, Out2], In & In2, Env | Env2> =>
891
+ makeWithState<[any, any], In & In2, [Out, Out2], Env | Env2>(
892
+ [self.initial, that.initial],
893
+ (now, input: In & In2, state) =>
894
+ pipe(
895
+ core.zipWith(
896
+ self.step(now, input, state[0]),
897
+ that.step(now, input, state[1]),
898
+ (a, b) => [a, b] as const
899
+ ),
900
+ core.flatMap(([
901
+ [lState, out, lDecision],
902
+ [rState, out2, rDecision]
903
+ ]) => {
904
+ if (ScheduleDecision.isContinue(lDecision) && ScheduleDecision.isContinue(rDecision)) {
905
+ return intersectWithLoop(
906
+ self,
907
+ that,
908
+ input,
909
+ lState,
910
+ out,
911
+ lDecision.intervals,
912
+ rState,
913
+ out2,
914
+ rDecision.intervals,
915
+ f
916
+ )
917
+ }
918
+ return core.succeed(
919
+ [
920
+ [lState, rState],
921
+ [out, out2],
922
+ ScheduleDecision.done
923
+ ]
924
+ )
925
+ })
926
+ )
927
+ ))
928
+
929
+ /** @internal */
930
+ const intersectWithLoop = <State, State1, Env, In, Out, Env1, In1, Out2>(
931
+ self: Schedule.Schedule<Out, In, Env>,
932
+ that: Schedule.Schedule<Out2, In1, Env1>,
933
+ input: In & In1,
934
+ lState: State,
935
+ out: Out,
936
+ lInterval: Intervals.Intervals,
937
+ rState: State1,
938
+ out2: Out2,
939
+ rInterval: Intervals.Intervals,
940
+ f: (x: Intervals.Intervals, y: Intervals.Intervals) => Intervals.Intervals
941
+ ): Effect.Effect<
942
+ [[State, State1], [Out, Out2], ScheduleDecision.ScheduleDecision],
943
+ never,
944
+ Env | Env1
945
+ > => {
946
+ const combined = f(lInterval, rInterval)
947
+ if (Intervals.isNonEmpty(combined)) {
948
+ return core.succeed([
949
+ [lState, rState],
950
+ [out, out2],
951
+ ScheduleDecision.continue(combined)
952
+ ])
953
+ }
954
+
955
+ if (pipe(lInterval, Intervals.lessThan(rInterval))) {
956
+ return core.flatMap(self.step(Intervals.end(lInterval), input, lState), ([lState, out, decision]) => {
957
+ if (ScheduleDecision.isDone(decision)) {
958
+ return core.succeed([
959
+ [lState, rState],
960
+ [out, out2],
961
+ ScheduleDecision.done
962
+ ])
963
+ }
964
+ return intersectWithLoop(
965
+ self,
966
+ that,
967
+ input,
968
+ lState,
969
+ out,
970
+ decision.intervals,
971
+ rState,
972
+ out2,
973
+ rInterval,
974
+ f
975
+ )
976
+ })
977
+ }
978
+ return core.flatMap(that.step(Intervals.end(rInterval), input, rState), ([rState, out2, decision]) => {
979
+ if (ScheduleDecision.isDone(decision)) {
980
+ return core.succeed([
981
+ [lState, rState],
982
+ [out, out2],
983
+ ScheduleDecision.done
984
+ ])
985
+ }
986
+ return intersectWithLoop(
987
+ self,
988
+ that,
989
+ input,
990
+ lState,
991
+ out,
992
+ lInterval,
993
+ rState,
994
+ out2,
995
+ decision.intervals,
996
+ f
997
+ )
998
+ })
999
+ }
1000
+
1001
+ /** @internal */
1002
+ export const jittered = <Out, In, R>(self: Schedule.Schedule<Out, In, R>): Schedule.Schedule<Out, In, R> =>
1003
+ jitteredWith(self, { min: 0.8, max: 1.2 })
1004
+
1005
+ /** @internal */
1006
+ export const jitteredWith = dual<
1007
+ (options: { min?: number | undefined; max?: number | undefined }) => <Out, In, R>(
1008
+ self: Schedule.Schedule<Out, In, R>
1009
+ ) => Schedule.Schedule<Out, In, R>,
1010
+ <Out, In, R>(
1011
+ self: Schedule.Schedule<Out, In, R>,
1012
+ options: { min?: number | undefined; max?: number | undefined }
1013
+ ) => Schedule.Schedule<Out, In, R>
1014
+ >(2, (self, options) => {
1015
+ const { max, min } = Object.assign({ min: 0.8, max: 1.2 }, options)
1016
+ return delayedEffect(self, (duration) =>
1017
+ core.map(Random.next, (random) => {
1018
+ const d = Duration.toMillis(duration)
1019
+ const jittered = d * min * (1 - random) + d * max * random
1020
+ return Duration.millis(jittered)
1021
+ }))
1022
+ })
1023
+
1024
+ /** @internal */
1025
+ export const linear = (baseInput: Duration.DurationInput): Schedule.Schedule<Duration.Duration> => {
1026
+ const base = Duration.decode(baseInput)
1027
+ return delayedSchedule(map(forever, (i) => Duration.times(base, i + 1)))
1028
+ }
1029
+
1030
+ /** @internal */
1031
+ export const map = dual<
1032
+ <Out, Out2>(
1033
+ f: (out: Out) => Out2
1034
+ ) => <In, R>(self: Schedule.Schedule<Out, In, R>) => Schedule.Schedule<Out2, In, R>,
1035
+ <Out, In, R, Out2>(
1036
+ self: Schedule.Schedule<Out, In, R>,
1037
+ f: (out: Out) => Out2
1038
+ ) => Schedule.Schedule<Out2, In, R>
1039
+ >(2, (self, f) => mapEffect(self, (out) => core.sync(() => f(out))))
1040
+
1041
+ /** @internal */
1042
+ export const mapEffect = dual<
1043
+ <Out, Out2, R2>(
1044
+ f: (out: Out) => Effect.Effect<Out2, never, R2>
1045
+ ) => <In, R>(self: Schedule.Schedule<Out, In, R>) => Schedule.Schedule<Out2, In, R | R2>,
1046
+ <Out, In, R, Out2, R2>(
1047
+ self: Schedule.Schedule<Out, In, R>,
1048
+ f: (out: Out) => Effect.Effect<Out2, never, R2>
1049
+ ) => Schedule.Schedule<Out2, In, R | R2>
1050
+ >(2, (self, f) =>
1051
+ makeWithState(
1052
+ self.initial,
1053
+ (now, input, state) =>
1054
+ core.flatMap(self.step(now, input, state), ([state, out, decision]) =>
1055
+ core.map(
1056
+ f(out),
1057
+ (out2) => [state, out2, decision] as const
1058
+ ))
1059
+ ))
1060
+
1061
+ /** @internal */
1062
+ export const minuteOfHour = (minute: number): Schedule.Schedule<number> =>
1063
+ makeWithState<[number, number], unknown, number>(
1064
+ [Number.MIN_SAFE_INTEGER, 0],
1065
+ (now, _, state) => {
1066
+ if (!Number.isInteger(minute) || minute < 0 || 59 < minute) {
1067
+ return core.dieSync(() =>
1068
+ new core.IllegalArgumentException(
1069
+ `Invalid argument in: minuteOfHour(${minute}). Must be in range 0...59`
1070
+ )
1071
+ )
1072
+ }
1073
+ const n = state[1]
1074
+ const initial = n === 0
1075
+ const minute0 = nextMinute(now, minute, initial)
1076
+ const start = beginningOfMinute(minute0)
1077
+ const end = endOfMinute(minute0)
1078
+ const interval = Interval.make(start, end)
1079
+ return core.succeed(
1080
+ [
1081
+ [end, n + 1],
1082
+ n,
1083
+ ScheduleDecision.continueWith(interval)
1084
+ ]
1085
+ )
1086
+ }
1087
+ )
1088
+
1089
+ /** @internal */
1090
+ export const modifyDelay = dual<
1091
+ <Out>(
1092
+ f: (out: Out, duration: Duration.Duration) => Duration.DurationInput
1093
+ ) => <In, R>(self: Schedule.Schedule<Out, In, R>) => Schedule.Schedule<Out, In, R>,
1094
+ <Out, In, R>(
1095
+ self: Schedule.Schedule<Out, In, R>,
1096
+ f: (out: Out, duration: Duration.Duration) => Duration.DurationInput
1097
+ ) => Schedule.Schedule<Out, In, R>
1098
+ >(2, (self, f) => modifyDelayEffect(self, (out, duration) => core.sync(() => f(out, duration))))
1099
+
1100
+ /** @internal */
1101
+ export const modifyDelayEffect = dual<
1102
+ <Out, R2>(
1103
+ f: (out: Out, duration: Duration.Duration) => Effect.Effect<Duration.DurationInput, never, R2>
1104
+ ) => <In, R>(self: Schedule.Schedule<Out, In, R>) => Schedule.Schedule<Out, In, R | R2>,
1105
+ <Out, In, R, R2>(
1106
+ self: Schedule.Schedule<Out, In, R>,
1107
+ f: (out: Out, duration: Duration.Duration) => Effect.Effect<Duration.DurationInput, never, R2>
1108
+ ) => Schedule.Schedule<Out, In, R | R2>
1109
+ >(2, (self, f) =>
1110
+ makeWithState(
1111
+ self.initial,
1112
+ (now, input, state) =>
1113
+ core.flatMap(self.step(now, input, state), ([state, out, decision]) => {
1114
+ if (ScheduleDecision.isDone(decision)) {
1115
+ return core.succeed([state, out, decision] as const)
1116
+ }
1117
+ const intervals = decision.intervals
1118
+ const delay = Interval.size(Interval.make(now, Intervals.start(intervals)))
1119
+ return core.map(f(out, delay), (durationInput) => {
1120
+ const duration = Duration.decode(durationInput)
1121
+ const oldStart = Intervals.start(intervals)
1122
+ const newStart = now + Duration.toMillis(duration)
1123
+ const delta = newStart - oldStart
1124
+ const newEnd = Math.max(0, Intervals.end(intervals) + delta)
1125
+ const newInterval = Interval.make(newStart, newEnd)
1126
+ return [state, out, ScheduleDecision.continueWith(newInterval)] as const
1127
+ })
1128
+ })
1129
+ ))
1130
+
1131
+ /** @internal */
1132
+ export const onDecision = dual<
1133
+ <Out, X, R2>(
1134
+ f: (out: Out, decision: ScheduleDecision.ScheduleDecision) => Effect.Effect<X, never, R2>
1135
+ ) => <In, R>(self: Schedule.Schedule<Out, In, R>) => Schedule.Schedule<Out, In, R | R2>,
1136
+ <Out, In, R, X, R2>(
1137
+ self: Schedule.Schedule<Out, In, R>,
1138
+ f: (out: Out, decision: ScheduleDecision.ScheduleDecision) => Effect.Effect<X, never, R2>
1139
+ ) => Schedule.Schedule<Out, In, R | R2>
1140
+ >(2, (self, f) =>
1141
+ makeWithState(
1142
+ self.initial,
1143
+ (now, input, state) =>
1144
+ core.flatMap(
1145
+ self.step(now, input, state),
1146
+ ([state, out, decision]) => core.as(f(out, decision), [state, out, decision] as const)
1147
+ )
1148
+ ))
1149
+
1150
+ /** @internal */
1151
+ export const passthrough = <Out, In, R>(
1152
+ self: Schedule.Schedule<Out, In, R>
1153
+ ): Schedule.Schedule<In, In, R> =>
1154
+ makeWithState(self.initial, (now, input, state) =>
1155
+ pipe(
1156
+ self.step(now, input, state),
1157
+ core.map(([state, _, decision]) => [state, input, decision] as const)
1158
+ ))
1159
+
1160
+ /** @internal */
1161
+ export const provideContext = dual<
1162
+ <R>(
1163
+ context: Context.Context<R>
1164
+ ) => <Out, In>(self: Schedule.Schedule<Out, In, R>) => Schedule.Schedule<Out, In>,
1165
+ <Out, In, R>(
1166
+ self: Schedule.Schedule<Out, In, R>,
1167
+ context: Context.Context<R>
1168
+ ) => Schedule.Schedule<Out, In>
1169
+ >(2, (self, context) =>
1170
+ makeWithState(self.initial, (now, input, state) =>
1171
+ core.provideContext(
1172
+ self.step(now, input, state),
1173
+ context
1174
+ )))
1175
+
1176
+ /** @internal */
1177
+ export const provideService = dual<
1178
+ <I, S>(
1179
+ tag: Context.Tag<I, S>,
1180
+ service: Types.NoInfer<S>
1181
+ ) => <Out, In, R>(
1182
+ self: Schedule.Schedule<Out, In, R>
1183
+ ) => Schedule.Schedule<Out, In, Exclude<R, I>>,
1184
+ <Out, In, R, I, S>(
1185
+ self: Schedule.Schedule<Out, In, R>,
1186
+ tag: Context.Tag<I, S>,
1187
+ service: Types.NoInfer<S>
1188
+ ) => Schedule.Schedule<Out, In, Exclude<R, I>>
1189
+ >(3, <Out, In, R, I, S>(
1190
+ self: Schedule.Schedule<Out, In, R>,
1191
+ tag: Context.Tag<I, S>,
1192
+ service: Types.NoInfer<S>
1193
+ ): Schedule.Schedule<Out, In, Exclude<R, I>> =>
1194
+ makeWithState(self.initial, (now, input, state) =>
1195
+ core.contextWithEffect((env) =>
1196
+ core.provideContext(
1197
+ // @ts-expect-error
1198
+ self.step(now, input, state),
1199
+ Context.add(env, tag, service)
1200
+ )
1201
+ )))
1202
+
1203
+ /** @internal */
1204
+ export const recurUntil = <A>(f: Predicate<A>): Schedule.Schedule<A, A> => untilInput(identity<A>(), f)
1205
+
1206
+ /** @internal */
1207
+ export const recurUntilEffect = <A, R>(
1208
+ f: (a: A) => Effect.Effect<boolean, never, R>
1209
+ ): Schedule.Schedule<A, A, R> => untilInputEffect(identity<A>(), f)
1210
+
1211
+ /** @internal */
1212
+ export const recurUntilOption = <A, B>(pf: (a: A) => Option.Option<B>): Schedule.Schedule<Option.Option<B>, A> =>
1213
+ untilOutput(map(identity<A>(), pf), Option.isSome)
1214
+
1215
+ /** @internal */
1216
+ export const recurUpTo = (
1217
+ durationInput: Duration.DurationInput
1218
+ ): Schedule.Schedule<Duration.Duration> => {
1219
+ const duration = Duration.decode(durationInput)
1220
+ return whileOutput(elapsed, (elapsed) => Duration.lessThan(elapsed, duration))
1221
+ }
1222
+
1223
+ /** @internal */
1224
+ export const recurWhile = <A>(f: Predicate<A>): Schedule.Schedule<A, A> => whileInput(identity<A>(), f)
1225
+
1226
+ /** @internal */
1227
+ export const recurWhileEffect = <A, R>(
1228
+ f: (a: A) => Effect.Effect<boolean, never, R>
1229
+ ): Schedule.Schedule<A, A, R> => whileInputEffect(identity<A>(), f)
1230
+
1231
+ /** @internal */
1232
+ export const recurs = (n: number): Schedule.Schedule<number> => whileOutput(forever, (out) => out < n)
1233
+
1234
+ /** @internal */
1235
+ export const reduce = dual<
1236
+ <Out, Z>(
1237
+ zero: Z,
1238
+ f: (z: Z, out: Out) => Z
1239
+ ) => <In, R>(self: Schedule.Schedule<Out, In, R>) => Schedule.Schedule<Z, In, R>,
1240
+ <Out, In, R, Z>(
1241
+ self: Schedule.Schedule<Out, In, R>,
1242
+ zero: Z,
1243
+ f: (z: Z, out: Out) => Z
1244
+ ) => Schedule.Schedule<Z, In, R>
1245
+ >(3, (self, zero, f) => reduceEffect(self, zero, (z, out) => core.sync(() => f(z, out))))
1246
+
1247
+ /** @internal */
1248
+ export const reduceEffect = dual<
1249
+ <Z, Out, R2>(
1250
+ zero: Z,
1251
+ f: (z: Z, out: Out) => Effect.Effect<Z, never, R2>
1252
+ ) => <In, R>(self: Schedule.Schedule<Out, In, R>) => Schedule.Schedule<Z, In, R | R2>,
1253
+ <Out, In, R, Z, R2>(
1254
+ self: Schedule.Schedule<Out, In, R>,
1255
+ zero: Z,
1256
+ f: (z: Z, out: Out) => Effect.Effect<Z, never, R2>
1257
+ ) => Schedule.Schedule<Z, In, R | R2>
1258
+ >(3, (self, zero, f) =>
1259
+ makeWithState(
1260
+ [self.initial, zero] as const,
1261
+ (now, input, [s, z]) =>
1262
+ core.flatMap(self.step(now, input, s), ([s, out, decision]) =>
1263
+ ScheduleDecision.isDone(decision)
1264
+ ? core.succeed([[s, z], z, decision as ScheduleDecision.ScheduleDecision] as const)
1265
+ : core.map(f(z, out), (z2) => [[s, z2], z, decision] as const))
1266
+ ))
1267
+
1268
+ /** @internal */
1269
+ export const repeatForever = <Env, In, Out>(self: Schedule.Schedule<Out, In, Env>): Schedule.Schedule<Out, In, Env> =>
1270
+ makeWithState(self.initial, (now, input, state) => {
1271
+ const step = (
1272
+ now: number,
1273
+ input: In,
1274
+ state: any
1275
+ ): Effect.Effect<[any, Out, ScheduleDecision.ScheduleDecision], never, Env> =>
1276
+ core.flatMap(
1277
+ self.step(now, input, state),
1278
+ ([state, out, decision]) =>
1279
+ ScheduleDecision.isDone(decision)
1280
+ ? step(now, input, self.initial)
1281
+ : core.succeed([state, out, decision])
1282
+ )
1283
+ return step(now, input, state)
1284
+ })
1285
+
1286
+ /** @internal */
1287
+ export const repetitions = <Out, In, R>(self: Schedule.Schedule<Out, In, R>): Schedule.Schedule<number, In, R> =>
1288
+ reduce(self, 0, (n, _) => n + 1)
1289
+
1290
+ /** @internal */
1291
+ export const resetAfter = dual<
1292
+ (
1293
+ duration: Duration.DurationInput
1294
+ ) => <Out, In, R>(
1295
+ self: Schedule.Schedule<Out, In, R>
1296
+ ) => Schedule.Schedule<Out, In, R>,
1297
+ <Out, In, R>(
1298
+ self: Schedule.Schedule<Out, In, R>,
1299
+ duration: Duration.DurationInput
1300
+ ) => Schedule.Schedule<Out, In, R>
1301
+ >(2, (self, durationInput) => {
1302
+ const duration = Duration.decode(durationInput)
1303
+ return pipe(
1304
+ self,
1305
+ intersect(elapsed),
1306
+ resetWhen(([, time]) => Duration.greaterThanOrEqualTo(time, duration)),
1307
+ map((out) => out[0])
1308
+ )
1309
+ })
1310
+
1311
+ /** @internal */
1312
+ export const resetWhen = dual<
1313
+ <Out>(f: Predicate<Out>) => <In, R>(self: Schedule.Schedule<Out, In, R>) => Schedule.Schedule<Out, In, R>,
1314
+ <Out, In, R>(self: Schedule.Schedule<Out, In, R>, f: Predicate<Out>) => Schedule.Schedule<Out, In, R>
1315
+ >(2, (self, f) =>
1316
+ makeWithState(
1317
+ self.initial,
1318
+ (now, input, state) =>
1319
+ core.flatMap(self.step(now, input, state), ([state, out, decision]) =>
1320
+ f(out)
1321
+ ? self.step(now, input, self.initial)
1322
+ : core.succeed([state, out, decision] as const))
1323
+ ))
1324
+
1325
+ /** @internal */
1326
+ export const run = dual<
1327
+ <In>(
1328
+ now: number,
1329
+ input: Iterable<In>
1330
+ ) => <Out, R>(self: Schedule.Schedule<Out, In, R>) => Effect.Effect<Chunk.Chunk<Out>, never, R>,
1331
+ <Out, In, R>(
1332
+ self: Schedule.Schedule<Out, In, R>,
1333
+ now: number,
1334
+ input: Iterable<In>
1335
+ ) => Effect.Effect<Chunk.Chunk<Out>, never, R>
1336
+ >(3, (self, now, input) =>
1337
+ pipe(
1338
+ runLoop(self, now, Chunk.fromIterable(input), self.initial, Chunk.empty()),
1339
+ core.map((list) => Chunk.reverse(list))
1340
+ ))
1341
+
1342
+ /** @internal */
1343
+ const runLoop = <Env, In, Out>(
1344
+ self: Schedule.Schedule<Out, In, Env>,
1345
+ now: number,
1346
+ inputs: Chunk.Chunk<In>,
1347
+ state: any,
1348
+ acc: Chunk.Chunk<Out>
1349
+ ): Effect.Effect<Chunk.Chunk<Out>, never, Env> => {
1350
+ if (!Chunk.isNonEmpty(inputs)) {
1351
+ return core.succeed(acc)
1352
+ }
1353
+ const input = Chunk.headNonEmpty(inputs)
1354
+ const nextInputs = Chunk.tailNonEmpty(inputs)
1355
+ return core.flatMap(self.step(now, input, state), ([state, out, decision]) => {
1356
+ if (ScheduleDecision.isDone(decision)) {
1357
+ return core.sync(() => pipe(acc, Chunk.prepend(out)))
1358
+ }
1359
+ return runLoop(
1360
+ self,
1361
+ Intervals.start(decision.intervals),
1362
+ nextInputs,
1363
+ state,
1364
+ Chunk.prepend(acc, out)
1365
+ )
1366
+ })
1367
+ }
1368
+
1369
+ /** @internal */
1370
+ export const secondOfMinute = (second: number): Schedule.Schedule<number> =>
1371
+ makeWithState<[number, number], unknown, number>(
1372
+ [Number.NEGATIVE_INFINITY, 0],
1373
+ (now, _, state) => {
1374
+ if (!Number.isInteger(second) || second < 0 || 59 < second) {
1375
+ return core.dieSync(() =>
1376
+ new core.IllegalArgumentException(
1377
+ `Invalid argument in: secondOfMinute(${second}). Must be in range 0...59`
1378
+ )
1379
+ )
1380
+ }
1381
+ const n = state[1]
1382
+ const initial = n === 0
1383
+ const second0 = nextSecond(now, second, initial)
1384
+ const start = beginningOfSecond(second0)
1385
+ const end = endOfSecond(second0)
1386
+ const interval = Interval.make(start, end)
1387
+ return core.succeed(
1388
+ [
1389
+ [end, n + 1],
1390
+ n,
1391
+ ScheduleDecision.continueWith(interval)
1392
+ ]
1393
+ )
1394
+ }
1395
+ )
1396
+
1397
+ /** @internal */
1398
+ export const spaced = (duration: Duration.DurationInput): Schedule.Schedule<number> => addDelay(forever, () => duration)
1399
+
1400
+ /** @internal */
1401
+ export const succeed = <A>(value: A): Schedule.Schedule<A> => map(forever, () => value)
1402
+
1403
+ /** @internal */
1404
+ export const sync = <A>(evaluate: LazyArg<A>): Schedule.Schedule<A> => map(forever, evaluate)
1405
+
1406
+ /** @internal */
1407
+ export const tapInput = dual<
1408
+ <In2, X, R2>(
1409
+ f: (input: In2) => Effect.Effect<X, never, R2>
1410
+ ) => <Out, In, R>(self: Schedule.Schedule<Out, In, R>) => Schedule.Schedule<Out, In & In2, R | R2>,
1411
+ <Out, In, R, In2, X, R2>(
1412
+ self: Schedule.Schedule<Out, In, R>,
1413
+ f: (input: In2) => Effect.Effect<X, never, R2>
1414
+ ) => Schedule.Schedule<Out, In & In2, R | R2>
1415
+ >(2, (self, f) =>
1416
+ makeWithState(self.initial, (now, input, state) =>
1417
+ core.zipRight(
1418
+ f(input),
1419
+ self.step(now, input, state)
1420
+ )))
1421
+
1422
+ /** @internal */
1423
+ export const tapOutput = dual<
1424
+ <X, R2, Out>(
1425
+ f: (out: Types.NoInfer<Out>) => Effect.Effect<X, never, R2>
1426
+ ) => <In, R>(self: Schedule.Schedule<Out, In, R>) => Schedule.Schedule<Out, In, R2 | R>,
1427
+ <Out, In, R, X, R2>(
1428
+ self: Schedule.Schedule<Out, In, R>,
1429
+ f: (out: Out) => Effect.Effect<X, never, R2>
1430
+ ) => Schedule.Schedule<Out, In, R | R2>
1431
+ >(
1432
+ 2,
1433
+ <Out, In, R, X, R2>(
1434
+ self: Schedule.Schedule<Out, In, R>,
1435
+ f: (out: Out) => Effect.Effect<X, never, R2>
1436
+ ): Schedule.Schedule<Out, In, R | R2> =>
1437
+ makeWithState(self.initial, (now, input, state) =>
1438
+ core.tap(
1439
+ self.step(now, input, state),
1440
+ ([, out]) => f(out)
1441
+ ))
1442
+ )
1443
+
1444
+ /** @internal */
1445
+ export const unfold = <A>(initial: A, f: (a: A) => A): Schedule.Schedule<A> =>
1446
+ makeWithState(initial, (now, _, state) =>
1447
+ core.sync(() =>
1448
+ [
1449
+ f(state),
1450
+ state,
1451
+ ScheduleDecision.continueWith(Interval.after(now))
1452
+ ] as const
1453
+ ))
1454
+
1455
+ /** @internal */
1456
+ export const union = dual<
1457
+ <Out2, In2, R2>(
1458
+ that: Schedule.Schedule<Out2, In2, R2>
1459
+ ) => <Out, In, R>(self: Schedule.Schedule<Out, In, R>) => Schedule.Schedule<[Out, Out2], In & In2, R | R2>,
1460
+ <Out, In, R, Out2, In2, R2>(
1461
+ self: Schedule.Schedule<Out, In, R>,
1462
+ that: Schedule.Schedule<Out2, In2, R2>
1463
+ ) => Schedule.Schedule<[Out, Out2], In & In2, R | R2>
1464
+ >(2, (self, that) => unionWith(self, that, Intervals.union))
1465
+
1466
+ /** @internal */
1467
+ export const unionWith = dual<
1468
+ <Out2, In2, R2>(
1469
+ that: Schedule.Schedule<Out2, In2, R2>,
1470
+ f: (x: Intervals.Intervals, y: Intervals.Intervals) => Intervals.Intervals
1471
+ ) => <Out, In, R>(self: Schedule.Schedule<Out, In, R>) => Schedule.Schedule<[Out, Out2], In & In2, R | R2>,
1472
+ <Out, In, R, Out2, In2, R2>(
1473
+ self: Schedule.Schedule<Out, In, R>,
1474
+ that: Schedule.Schedule<Out2, In2, R2>,
1475
+ f: (x: Intervals.Intervals, y: Intervals.Intervals) => Intervals.Intervals
1476
+ ) => Schedule.Schedule<[Out, Out2], In & In2, R | R2>
1477
+ >(3, (self, that, f) =>
1478
+ makeWithState([self.initial, that.initial], (now, input, state) =>
1479
+ core.zipWith(
1480
+ self.step(now, input, state[0]),
1481
+ that.step(now, input, state[1]),
1482
+ ([lState, l, lDecision], [rState, r, rDecision]) => {
1483
+ if (ScheduleDecision.isDone(lDecision) && ScheduleDecision.isDone(rDecision)) {
1484
+ return [[lState, rState], [l, r], ScheduleDecision.done]
1485
+ }
1486
+ if (ScheduleDecision.isDone(lDecision) && ScheduleDecision.isContinue(rDecision)) {
1487
+ return [
1488
+ [lState, rState],
1489
+ [l, r],
1490
+ ScheduleDecision.continue(rDecision.intervals)
1491
+ ]
1492
+ }
1493
+ if (ScheduleDecision.isContinue(lDecision) && ScheduleDecision.isDone(rDecision)) {
1494
+ return [
1495
+ [lState, rState],
1496
+ [l, r],
1497
+ ScheduleDecision.continue(lDecision.intervals)
1498
+ ]
1499
+ }
1500
+ if (ScheduleDecision.isContinue(lDecision) && ScheduleDecision.isContinue(rDecision)) {
1501
+ const combined = f(lDecision.intervals, rDecision.intervals)
1502
+ return [
1503
+ [lState, rState],
1504
+ [l, r],
1505
+ ScheduleDecision.continue(combined)
1506
+ ]
1507
+ }
1508
+ throw new Error(
1509
+ "BUG: Schedule.unionWith - please report an issue at https://github.com/Effect-TS/effect/issues"
1510
+ )
1511
+ }
1512
+ )))
1513
+
1514
+ /** @internal */
1515
+ export const untilInput = dual<
1516
+ <In>(f: Predicate<In>) => <Out, R>(self: Schedule.Schedule<Out, In, R>) => Schedule.Schedule<Out, In, R>,
1517
+ <Out, In, R>(self: Schedule.Schedule<Out, In, R>, f: Predicate<In>) => Schedule.Schedule<Out, In, R>
1518
+ >(2, (self, f) => check(self, (input, _) => !f(input)))
1519
+
1520
+ /** @internal */
1521
+ export const untilInputEffect = dual<
1522
+ <In, R2>(
1523
+ f: (input: In) => Effect.Effect<boolean, never, R2>
1524
+ ) => <Out, R>(self: Schedule.Schedule<Out, In, R>) => Schedule.Schedule<Out, In, R | R2>,
1525
+ <Out, In, R, R2>(
1526
+ self: Schedule.Schedule<Out, In, R>,
1527
+ f: (input: In) => Effect.Effect<boolean, never, R2>
1528
+ ) => Schedule.Schedule<Out, In, R | R2>
1529
+ >(2, (self, f) => checkEffect(self, (input, _) => effect.negate(f(input))))
1530
+
1531
+ /** @internal */
1532
+ export const untilOutput = dual<
1533
+ <Out>(f: Predicate<Out>) => <In, R>(self: Schedule.Schedule<Out, In, R>) => Schedule.Schedule<Out, In, R>,
1534
+ <Out, In, R>(self: Schedule.Schedule<Out, In, R>, f: Predicate<Out>) => Schedule.Schedule<Out, In, R>
1535
+ >(2, (self, f) => check(self, (_, out) => !f(out)))
1536
+
1537
+ /** @internal */
1538
+ export const untilOutputEffect = dual<
1539
+ <Out, R2>(
1540
+ f: (out: Out) => Effect.Effect<boolean, never, R2>
1541
+ ) => <In, R>(self: Schedule.Schedule<Out, In, R>) => Schedule.Schedule<Out, In, R | R2>,
1542
+ <Out, In, R, R2>(
1543
+ self: Schedule.Schedule<Out, In, R>,
1544
+ f: (out: Out) => Effect.Effect<boolean, never, R2>
1545
+ ) => Schedule.Schedule<Out, In, R | R2>
1546
+ >(2, (self, f) => checkEffect(self, (_, out) => effect.negate(f(out))))
1547
+
1548
+ /** @internal */
1549
+ export const upTo = dual<
1550
+ (duration: Duration.DurationInput) => <Out, In, R>(
1551
+ self: Schedule.Schedule<Out, In, R>
1552
+ ) => Schedule.Schedule<Out, In, R>,
1553
+ <Out, In, R>(
1554
+ self: Schedule.Schedule<Out, In, R>,
1555
+ duration: Duration.DurationInput
1556
+ ) => Schedule.Schedule<Out, In, R>
1557
+ >(2, (self, duration) => zipLeft(self, recurUpTo(duration)))
1558
+
1559
+ /** @internal */
1560
+ export const whileInput = dual<
1561
+ <In>(f: Predicate<In>) => <Out, R>(self: Schedule.Schedule<Out, In, R>) => Schedule.Schedule<Out, In, R>,
1562
+ <Out, In, R>(self: Schedule.Schedule<Out, In, R>, f: Predicate<In>) => Schedule.Schedule<Out, In, R>
1563
+ >(2, (self, f) => check(self, (input, _) => f(input)))
1564
+
1565
+ /** @internal */
1566
+ export const whileInputEffect = dual<
1567
+ <In, R2>(
1568
+ f: (input: In) => Effect.Effect<boolean, never, R2>
1569
+ ) => <Out, R>(self: Schedule.Schedule<Out, In, R>) => Schedule.Schedule<Out, In, R | R2>,
1570
+ <Out, In, R, R2>(
1571
+ self: Schedule.Schedule<Out, In, R>,
1572
+ f: (input: In) => Effect.Effect<boolean, never, R2>
1573
+ ) => Schedule.Schedule<Out, In, R | R2>
1574
+ >(2, (self, f) => checkEffect(self, (input, _) => f(input)))
1575
+
1576
+ /** @internal */
1577
+ export const whileOutput = dual<
1578
+ <Out>(f: Predicate<Out>) => <In, R>(self: Schedule.Schedule<Out, In, R>) => Schedule.Schedule<Out, In, R>,
1579
+ <Out, In, R>(self: Schedule.Schedule<Out, In, R>, f: Predicate<Out>) => Schedule.Schedule<Out, In, R>
1580
+ >(2, (self, f) => check(self, (_, out) => f(out)))
1581
+
1582
+ /** @internal */
1583
+ export const whileOutputEffect = dual<
1584
+ <Out, R2>(
1585
+ f: (out: Out) => Effect.Effect<boolean, never, R2>
1586
+ ) => <In, R>(self: Schedule.Schedule<Out, In, R>) => Schedule.Schedule<Out, In, R | R2>,
1587
+ <Out, In, R, R2>(
1588
+ self: Schedule.Schedule<Out, In, R>,
1589
+ f: (out: Out) => Effect.Effect<boolean, never, R2>
1590
+ ) => Schedule.Schedule<Out, In, R | R2>
1591
+ >(2, (self, f) => checkEffect(self, (_, out) => f(out)))
1592
+
1593
+ /** @internal */
1594
+ export const windowed = (intervalInput: Duration.DurationInput): Schedule.Schedule<number> => {
1595
+ const interval = Duration.decode(intervalInput)
1596
+ const millis = Duration.toMillis(interval)
1597
+ return makeWithState<[Option.Option<number>, number], unknown, number>(
1598
+ [Option.none(), 0],
1599
+ (now, _, [option, n]) => {
1600
+ switch (option._tag) {
1601
+ case "None": {
1602
+ return core.succeed(
1603
+ [
1604
+ [Option.some(now), n + 1],
1605
+ n,
1606
+ ScheduleDecision.continueWith(Interval.after(now + millis))
1607
+ ]
1608
+ )
1609
+ }
1610
+ case "Some": {
1611
+ return core.succeed(
1612
+ [
1613
+ [Option.some(option.value), n + 1],
1614
+ n,
1615
+ ScheduleDecision.continueWith(
1616
+ Interval.after(now + (millis - ((now - option.value) % millis)))
1617
+ )
1618
+ ]
1619
+ )
1620
+ }
1621
+ }
1622
+ }
1623
+ )
1624
+ }
1625
+
1626
+ /** @internal */
1627
+ export const zipLeft = dual<
1628
+ <Out2, In2, R2>(
1629
+ that: Schedule.Schedule<Out2, In2, R2>
1630
+ ) => <Out, In, R>(self: Schedule.Schedule<Out, In, R>) => Schedule.Schedule<Out, In & In2, R | R2>,
1631
+ <Out, In, R, Out2, In2, R2>(
1632
+ self: Schedule.Schedule<Out, In, R>,
1633
+ that: Schedule.Schedule<Out2, In2, R2>
1634
+ ) => Schedule.Schedule<Out, In & In2, R | R2>
1635
+ >(2, (self, that) => map(intersect(self, that), (out) => out[0]))
1636
+
1637
+ /** @internal */
1638
+ export const zipRight = dual<
1639
+ <Out2, In2, R2>(
1640
+ that: Schedule.Schedule<Out2, In2, R2>
1641
+ ) => <Out, In, R>(self: Schedule.Schedule<Out, In, R>) => Schedule.Schedule<Out2, In & In2, R | R2>,
1642
+ <Out, In, R, Out2, In2, R2>(
1643
+ self: Schedule.Schedule<Out, In, R>,
1644
+ that: Schedule.Schedule<Out2, In2, R2>
1645
+ ) => Schedule.Schedule<Out2, In & In2, R | R2>
1646
+ >(2, (self, that) => map(intersect(self, that), (out) => out[1]))
1647
+
1648
+ /** @internal */
1649
+ export const zipWith = dual<
1650
+ <Out2, In2, R2, Out, Out3>(
1651
+ that: Schedule.Schedule<Out2, In2, R2>,
1652
+ f: (out: Out, out2: Out2) => Out3
1653
+ ) => <In, R>(self: Schedule.Schedule<Out, In, R>) => Schedule.Schedule<Out3, In & In2, R | R2>,
1654
+ <Out, In, R, Out2, In2, R2, Out3>(
1655
+ self: Schedule.Schedule<Out, In, R>,
1656
+ that: Schedule.Schedule<Out2, In2, R2>,
1657
+ f: (out: Out, out2: Out2) => Out3
1658
+ ) => Schedule.Schedule<Out3, In & In2, R | R2>
1659
+ >(3, (self, that, f) => map(intersect(self, that), ([out, out2]) => f(out, out2)))
1660
+
1661
+ // -----------------------------------------------------------------------------
1662
+ // Seconds
1663
+ // -----------------------------------------------------------------------------
1664
+
1665
+ /** @internal */
1666
+ export const beginningOfSecond = (now: number): number => {
1667
+ const date = new Date(now)
1668
+ return new Date(
1669
+ date.getFullYear(),
1670
+ date.getMonth(),
1671
+ date.getDate(),
1672
+ date.getHours(),
1673
+ date.getMinutes(),
1674
+ date.getSeconds(),
1675
+ 0
1676
+ ).getTime()
1677
+ }
1678
+
1679
+ /** @internal */
1680
+ export const endOfSecond = (now: number): number => {
1681
+ const date = new Date(beginningOfSecond(now))
1682
+ return date.setSeconds(date.getSeconds() + 1)
1683
+ }
1684
+
1685
+ /** @internal */
1686
+ export const nextSecond = (now: number, second: number, initial: boolean): number => {
1687
+ const date = new Date(now)
1688
+ if (date.getSeconds() === second && initial) {
1689
+ return now
1690
+ }
1691
+ if (date.getSeconds() < second) {
1692
+ return date.setSeconds(second)
1693
+ }
1694
+ // Set seconds to the provided value and add one minute
1695
+ const newDate = new Date(date.setSeconds(second))
1696
+ return newDate.setTime(newDate.getTime() + 1000 * 60)
1697
+ }
1698
+
1699
+ // -----------------------------------------------------------------------------
1700
+ // Minutes
1701
+ // -----------------------------------------------------------------------------
1702
+
1703
+ /** @internal */
1704
+ export const beginningOfMinute = (now: number): number => {
1705
+ const date = new Date(now)
1706
+ return new Date(
1707
+ date.getFullYear(),
1708
+ date.getMonth(),
1709
+ date.getDate(),
1710
+ date.getHours(),
1711
+ date.getMinutes(),
1712
+ 0,
1713
+ 0
1714
+ ).getTime()
1715
+ }
1716
+
1717
+ /** @internal */
1718
+ export const endOfMinute = (now: number): number => {
1719
+ const date = new Date(beginningOfMinute(now))
1720
+ return date.setMinutes(date.getMinutes() + 1)
1721
+ }
1722
+
1723
+ /** @internal */
1724
+ export const nextMinute = (now: number, minute: number, initial: boolean): number => {
1725
+ const date = new Date(now)
1726
+ if (date.getMinutes() === minute && initial) {
1727
+ return now
1728
+ }
1729
+ if (date.getMinutes() < minute) {
1730
+ return date.setMinutes(minute)
1731
+ }
1732
+ // Set minutes to the provided value and add one hour
1733
+ const newDate = new Date(date.setMinutes(minute))
1734
+ return newDate.setTime(newDate.getTime() + 1000 * 60 * 60)
1735
+ }
1736
+
1737
+ // -----------------------------------------------------------------------------
1738
+ // Hours
1739
+ // -----------------------------------------------------------------------------
1740
+
1741
+ /** @internal */
1742
+ export const beginningOfHour = (now: number): number => {
1743
+ const date = new Date(now)
1744
+ return new Date(
1745
+ date.getFullYear(),
1746
+ date.getMonth(),
1747
+ date.getDate(),
1748
+ date.getHours(),
1749
+ 0,
1750
+ 0,
1751
+ 0
1752
+ ).getTime()
1753
+ }
1754
+
1755
+ /** @internal */
1756
+ export const endOfHour = (now: number): number => {
1757
+ const date = new Date(beginningOfHour(now))
1758
+ return date.setHours(date.getHours() + 1)
1759
+ }
1760
+
1761
+ /** @internal */
1762
+ export const nextHour = (now: number, hour: number, initial: boolean): number => {
1763
+ const date = new Date(now)
1764
+ if (date.getHours() === hour && initial) {
1765
+ return now
1766
+ }
1767
+ if (date.getHours() < hour) {
1768
+ return date.setHours(hour)
1769
+ }
1770
+ // Set hours to the provided value and add one day
1771
+ const newDate = new Date(date.setHours(hour))
1772
+ return newDate.setTime(newDate.getTime() + 1000 * 60 * 60 * 24)
1773
+ }
1774
+
1775
+ // -----------------------------------------------------------------------------
1776
+ // Days
1777
+ // -----------------------------------------------------------------------------
1778
+
1779
+ /** @internal */
1780
+ export const beginningOfDay = (now: number): number => {
1781
+ const date = new Date(now)
1782
+ return new Date(
1783
+ date.getFullYear(),
1784
+ date.getMonth(),
1785
+ date.getDate(),
1786
+ 0,
1787
+ 0,
1788
+ 0,
1789
+ 0
1790
+ ).getTime()
1791
+ }
1792
+
1793
+ /** @internal */
1794
+ export const endOfDay = (now: number): number => {
1795
+ const date = new Date(beginningOfDay(now))
1796
+ return date.setDate(date.getDate() + 1)
1797
+ }
1798
+
1799
+ /** @internal */
1800
+ export const nextDay = (now: number, dayOfWeek: number, initial: boolean): number => {
1801
+ const date = new Date(now)
1802
+ if (date.getDay() === dayOfWeek && initial) {
1803
+ return now
1804
+ }
1805
+ const nextDayOfWeek = (7 + dayOfWeek - date.getDay()) % 7
1806
+ return date.setDate(date.getDate() + (nextDayOfWeek === 0 ? 7 : nextDayOfWeek))
1807
+ }
1808
+
1809
+ /** @internal */
1810
+ export const nextDayOfMonth = (now: number, day: number, initial: boolean): number => {
1811
+ const date = new Date(now)
1812
+ if (date.getDate() === day && initial) {
1813
+ return now
1814
+ }
1815
+ if (date.getDate() < day) {
1816
+ return date.setDate(day)
1817
+ }
1818
+ return findNextMonth(now, day, 1)
1819
+ }
1820
+
1821
+ /** @internal */
1822
+ export const findNextMonth = (now: number, day: number, months: number): number => {
1823
+ const d = new Date(now)
1824
+ const tmp1 = new Date(d.setDate(day))
1825
+ const tmp2 = new Date(tmp1.setMonth(tmp1.getMonth() + months))
1826
+ if (tmp2.getDate() === day) {
1827
+ const d2 = new Date(now)
1828
+ const tmp3 = new Date(d2.setDate(day))
1829
+ return tmp3.setMonth(tmp3.getMonth() + months)
1830
+ }
1831
+ return findNextMonth(now, day, months + 1)
1832
+ }
1833
+
1834
+ // circular with Effect
1835
+
1836
+ const ScheduleDefectTypeId = Symbol.for("effect/Schedule/ScheduleDefect")
1837
+ class ScheduleDefect<E> {
1838
+ readonly [ScheduleDefectTypeId]: typeof ScheduleDefectTypeId
1839
+ constructor(readonly error: E) {
1840
+ this[ScheduleDefectTypeId] = ScheduleDefectTypeId
1841
+ }
1842
+ }
1843
+ const isScheduleDefect = <E = unknown>(u: unknown): u is ScheduleDefect<E> => hasProperty(u, ScheduleDefectTypeId)
1844
+ const scheduleDefectWrap = <A, E, R>(self: Effect.Effect<A, E, R>) =>
1845
+ core.catchAll(self, (e) => core.die(new ScheduleDefect(e)))
1846
+
1847
+ /** @internal */
1848
+ export const scheduleDefectRefailCause = <E>(cause: Cause.Cause<E>) =>
1849
+ Option.match(
1850
+ internalCause.find(
1851
+ cause,
1852
+ (_) => internalCause.isDieType(_) && isScheduleDefect<E>(_.defect) ? Option.some(_.defect) : Option.none()
1853
+ ),
1854
+ {
1855
+ onNone: () => cause,
1856
+ onSome: (error) => internalCause.fail(error.error)
1857
+ }
1858
+ )
1859
+
1860
+ /** @internal */
1861
+ export const scheduleDefectRefail = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
1862
+ core.catchAllCause(effect, (cause) => core.failCause(scheduleDefectRefailCause(cause)))
1863
+
1864
+ /** @internal */
1865
+ export const repeat_Effect = dual<
1866
+ <R1, A, B>(
1867
+ schedule: Schedule.Schedule<B, A, R1>
1868
+ ) => <E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<B, E, R | R1>,
1869
+ <A, E, R, R1, B>(
1870
+ self: Effect.Effect<A, E, R>,
1871
+ schedule: Schedule.Schedule<B, A, R1>
1872
+ ) => Effect.Effect<B, E, R | R1>
1873
+ >(2, (self, schedule) => repeatOrElse_Effect(self, schedule, (e, _) => core.fail(e)))
1874
+
1875
+ /** @internal */
1876
+ export const repeat_combined = dual<{
1877
+ <O extends Types.NoExcessProperties<Effect.Repeat.Options<A>, O>, A>(
1878
+ options: O
1879
+ ): <E, R>(self: Effect.Effect<A, E, R>) => Effect.Repeat.Return<R, E, A, O>
1880
+ <B, A, R1>(
1881
+ schedule: Schedule.Schedule<B, A, R1>
1882
+ ): <E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<B, E, R | R1>
1883
+ }, {
1884
+ <A, E, R, O extends Types.NoExcessProperties<Effect.Repeat.Options<A>, O>>(
1885
+ self: Effect.Effect<A, E, R>,
1886
+ options: O
1887
+ ): Effect.Repeat.Return<R, E, A, O>
1888
+ <A, E, R, B, R1>(
1889
+ self: Effect.Effect<A, E, R>,
1890
+ schedule: Schedule.Schedule<B, A, R1>
1891
+ ): Effect.Effect<B, E, R | R1>
1892
+ }>(
1893
+ 2,
1894
+ (self: Effect.Effect<any, any, any>, options: Effect.Repeat.Options<any> | Schedule.Schedule<any, any, any>) => {
1895
+ if (isSchedule(options)) {
1896
+ return repeat_Effect(self, options)
1897
+ }
1898
+
1899
+ const base = options.schedule ?? passthrough(forever)
1900
+ const withWhile = options.while ?
1901
+ whileInputEffect(base, (a) => {
1902
+ const applied = options.while!(a)
1903
+ if (typeof applied === "boolean") {
1904
+ return core.succeed(applied)
1905
+ }
1906
+ return scheduleDefectWrap(applied)
1907
+ }) :
1908
+ base
1909
+ const withUntil = options.until ?
1910
+ untilInputEffect(withWhile, (a) => {
1911
+ const applied = options.until!(a)
1912
+ if (typeof applied === "boolean") {
1913
+ return core.succeed(applied)
1914
+ }
1915
+ return scheduleDefectWrap(applied)
1916
+ }) :
1917
+ withWhile
1918
+ const withTimes = options.times ?
1919
+ intersect(withUntil, recurs(options.times)).pipe(map((intersectionPair) => intersectionPair[0])) :
1920
+ withUntil
1921
+ return scheduleDefectRefail(repeat_Effect(self, withTimes))
1922
+ }
1923
+ )
1924
+
1925
+ /** @internal */
1926
+ export const repeatOrElse_Effect = dual<
1927
+ <R2, A, B, E, E2, R3>(
1928
+ schedule: Schedule.Schedule<B, A, R2>,
1929
+ orElse: (error: E, option: Option.Option<B>) => Effect.Effect<B, E2, R3>
1930
+ ) => <R>(self: Effect.Effect<A, E, R>) => Effect.Effect<B, E2, R | R2 | R3>,
1931
+ <A, E, R, R2, B, E2, R3>(
1932
+ self: Effect.Effect<A, E, R>,
1933
+ schedule: Schedule.Schedule<B, A, R2>,
1934
+ orElse: (error: E, option: Option.Option<B>) => Effect.Effect<B, E2, R3>
1935
+ ) => Effect.Effect<B, E2, R | R2 | R3>
1936
+ >(3, (self, schedule, orElse) =>
1937
+ core.flatMap(driver(schedule), (driver) =>
1938
+ core.matchEffect(self, {
1939
+ onFailure: (error) => orElse(error, Option.none()),
1940
+ onSuccess: (value) =>
1941
+ repeatOrElseEffectLoop(
1942
+ effect.provideServiceEffect(
1943
+ self,
1944
+ CurrentIterationMetadata,
1945
+ ref.get(driver.iterationMeta)
1946
+ ),
1947
+ driver,
1948
+ (error, option) =>
1949
+ effect.provideServiceEffect(
1950
+ orElse(error, option),
1951
+ CurrentIterationMetadata,
1952
+ ref.get(driver.iterationMeta)
1953
+ ),
1954
+ value
1955
+ )
1956
+ })))
1957
+
1958
+ /** @internal */
1959
+ const repeatOrElseEffectLoop = <A, E, R, R1, B, C, E2, R2>(
1960
+ self: Effect.Effect<A, E, R>,
1961
+ driver: Schedule.ScheduleDriver<B, A, R1>,
1962
+ orElse: (error: E, option: Option.Option<B>) => Effect.Effect<C, E2, R2>,
1963
+ value: A
1964
+ ): Effect.Effect<B | C, E2, R | R1 | R2> =>
1965
+ core.matchEffect(driver.next(value), {
1966
+ onFailure: () => core.orDie(driver.last),
1967
+ onSuccess: (b) =>
1968
+ core.matchEffect(self, {
1969
+ onFailure: (error) => orElse(error, Option.some(b)),
1970
+ onSuccess: (value) => repeatOrElseEffectLoop(self, driver, orElse, value)
1971
+ })
1972
+ })
1973
+
1974
+ /** @internal */
1975
+ export const retry_Effect = dual<
1976
+ <B, E, R1>(
1977
+ policy: Schedule.Schedule<B, E, R1>
1978
+ ) => <A, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R | R1>,
1979
+ <A, E, R, B, R1>(
1980
+ self: Effect.Effect<A, E, R>,
1981
+ policy: Schedule.Schedule<B, E, R1>
1982
+ ) => Effect.Effect<A, E, R | R1>
1983
+ >(2, (self, policy) => retryOrElse_Effect(self, policy, (e, _) => core.fail(e)))
1984
+
1985
+ /** @internal */
1986
+ export const retry_combined: {
1987
+ <E, O extends Types.NoExcessProperties<Effect.Retry.Options<E>, O>>(
1988
+ options: O
1989
+ ): <A, R>(
1990
+ self: Effect.Effect<A, E, R>
1991
+ ) => Effect.Retry.Return<R, E, A, O>
1992
+ <B, E, R1>(
1993
+ policy: Schedule.Schedule<B, Types.NoInfer<E>, R1>
1994
+ ): <A, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R1 | R>
1995
+ <A, E, R, O extends Types.NoExcessProperties<Effect.Retry.Options<E>, O>>(
1996
+ self: Effect.Effect<A, E, R>,
1997
+ options: O
1998
+ ): Effect.Retry.Return<R, E, A, O>
1999
+ <A, E, R, B, R1>(
2000
+ self: Effect.Effect<A, E, R>,
2001
+ policy: Schedule.Schedule<B, Types.NoInfer<E>, R1>
2002
+ ): Effect.Effect<A, E, R1 | R>
2003
+ } = dual(
2004
+ 2,
2005
+ (
2006
+ self: Effect.Effect<any, any, any>,
2007
+ options: Effect.Retry.Options<any> | Schedule.Schedule<any, any, any>
2008
+ ) => {
2009
+ if (isSchedule(options)) {
2010
+ return retry_Effect(self, options)
2011
+ }
2012
+ return scheduleDefectRefail(retry_Effect(self, fromRetryOptions(options)))
2013
+ }
2014
+ )
2015
+
2016
+ /** @internal */
2017
+ export const fromRetryOptions = (options: Effect.Retry.Options<any>): Schedule.Schedule<any, any, any> => {
2018
+ const base = options.schedule ?? forever
2019
+ const withWhile = options.while ?
2020
+ whileInputEffect(base, (e) => {
2021
+ const applied = options.while!(e)
2022
+ if (typeof applied === "boolean") {
2023
+ return core.succeed(applied)
2024
+ }
2025
+ return scheduleDefectWrap(applied)
2026
+ }) :
2027
+ base
2028
+ const withUntil = options.until ?
2029
+ untilInputEffect(withWhile, (e) => {
2030
+ const applied = options.until!(e)
2031
+ if (typeof applied === "boolean") {
2032
+ return core.succeed(applied)
2033
+ }
2034
+ return scheduleDefectWrap(applied)
2035
+ }) :
2036
+ withWhile
2037
+ return options.times !== undefined ?
2038
+ intersect(withUntil, recurs(options.times)) :
2039
+ withUntil
2040
+ }
2041
+
2042
+ /** @internal */
2043
+ export const retryOrElse_Effect = dual<
2044
+ <A1, E, R1, A2, E2, R2>(
2045
+ policy: Schedule.Schedule<A1, Types.NoInfer<E>, R1>,
2046
+ orElse: (e: Types.NoInfer<E>, out: A1) => Effect.Effect<A2, E2, R2>
2047
+ ) => <A, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<A | A2, E2, R | R1 | R2>,
2048
+ <A, E, R, A1, R1, A2, E2, R2>(
2049
+ self: Effect.Effect<A, E, R>,
2050
+ policy: Schedule.Schedule<A1, Types.NoInfer<E>, R1>,
2051
+ orElse: (e: Types.NoInfer<E>, out: A1) => Effect.Effect<A2, E2, R2>
2052
+ ) => Effect.Effect<A | A2, E2, R | R1 | R2>
2053
+ >(3, (self, policy, orElse) =>
2054
+ core.flatMap(
2055
+ driver(policy),
2056
+ (driver) =>
2057
+ retryOrElse_EffectLoop(
2058
+ effect.provideServiceEffect(
2059
+ self,
2060
+ CurrentIterationMetadata,
2061
+ ref.get(driver.iterationMeta)
2062
+ ),
2063
+ driver,
2064
+ (e, out) =>
2065
+ effect.provideServiceEffect(
2066
+ orElse(e, out),
2067
+ CurrentIterationMetadata,
2068
+ ref.get(driver.iterationMeta)
2069
+ )
2070
+ )
2071
+ ))
2072
+
2073
+ /** @internal */
2074
+ const retryOrElse_EffectLoop = <A, E, R, R1, A1, A2, E2, R2>(
2075
+ self: Effect.Effect<A, E, R>,
2076
+ driver: Schedule.ScheduleDriver<A1, E, R1>,
2077
+ orElse: (e: E, out: A1) => Effect.Effect<A2, E2, R2>
2078
+ ): Effect.Effect<A | A2, E2, R | R1 | R2> => {
2079
+ return core.catchAll(
2080
+ self,
2081
+ (e) =>
2082
+ core.matchEffect(driver.next(e), {
2083
+ onFailure: () =>
2084
+ pipe(
2085
+ driver.last,
2086
+ core.orDie,
2087
+ core.flatMap((out) => orElse(e, out))
2088
+ ),
2089
+ onSuccess: () => retryOrElse_EffectLoop(self, driver, orElse)
2090
+ })
2091
+ )
2092
+ }
2093
+
2094
+ /** @internal */
2095
+ export const schedule_Effect = dual<
2096
+ <A, R2, Out>(
2097
+ schedule: Schedule.Schedule<Out, NoInfer<A> | undefined, R2>
2098
+ ) => <E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<Out, E, R | R2>,
2099
+ <A, E, R, R2, Out>(
2100
+ self: Effect.Effect<A, E, R>,
2101
+ schedule: Schedule.Schedule<Out, A | undefined, R2>
2102
+ ) => Effect.Effect<Out, E, R | R2>
2103
+ >(2, <A, E, R, R2, Out>(
2104
+ self: Effect.Effect<A, E, R>,
2105
+ schedule: Schedule.Schedule<Out, A | undefined, R2>
2106
+ ) => scheduleFrom_Effect(self, void 0, schedule))
2107
+
2108
+ /** @internal */
2109
+ export const scheduleFrom_Effect = dual<
2110
+ <R2, In, Out>(
2111
+ initial: In,
2112
+ schedule: Schedule.Schedule<Out, In, R2>
2113
+ ) => <E, R>(self: Effect.Effect<In, E, R>) => Effect.Effect<Out, E, R | R2>,
2114
+ <In, E, R, R2, Out>(
2115
+ self: Effect.Effect<In, E, R>,
2116
+ initial: In,
2117
+ schedule: Schedule.Schedule<Out, In, R2>
2118
+ ) => Effect.Effect<Out, E, R | R2>
2119
+ >(3, (self, initial, schedule) =>
2120
+ core.flatMap(
2121
+ driver(schedule),
2122
+ (driver) =>
2123
+ scheduleFrom_EffectLoop(
2124
+ effect.provideServiceEffect(
2125
+ self,
2126
+ CurrentIterationMetadata,
2127
+ ref.get(driver.iterationMeta)
2128
+ ),
2129
+ initial,
2130
+ driver
2131
+ )
2132
+ ))
2133
+
2134
+ /** @internal */
2135
+ const scheduleFrom_EffectLoop = <In, E, R, R2, Out>(
2136
+ self: Effect.Effect<In, E, R>,
2137
+ initial: In,
2138
+ driver: Schedule.ScheduleDriver<Out, In, R2>
2139
+ ): Effect.Effect<Out, E, R | R2> =>
2140
+ core.matchEffect(driver.next(initial), {
2141
+ onFailure: () => core.orDie(driver.last),
2142
+ onSuccess: () =>
2143
+ core.flatMap(
2144
+ self,
2145
+ (a) => scheduleFrom_EffectLoop(self, a, driver)
2146
+ )
2147
+ })
2148
+
2149
+ /** @internal */
2150
+ export const count: Schedule.Schedule<number> = unfold(0, (n) => n + 1)
2151
+
2152
+ /** @internal */
2153
+ export const elapsed: Schedule.Schedule<Duration.Duration> = makeWithState(
2154
+ Option.none() as Option.Option<number>,
2155
+ (now, _, state) => {
2156
+ switch (state._tag) {
2157
+ case "None": {
2158
+ return core.succeed(
2159
+ [
2160
+ Option.some(now),
2161
+ Duration.zero,
2162
+ ScheduleDecision.continueWith(Interval.after(now))
2163
+ ] as const
2164
+ )
2165
+ }
2166
+ case "Some": {
2167
+ return core.succeed(
2168
+ [
2169
+ Option.some(state.value),
2170
+ Duration.millis(now - state.value),
2171
+ ScheduleDecision.continueWith(Interval.after(now))
2172
+ ] as const
2173
+ )
2174
+ }
2175
+ }
2176
+ }
2177
+ )
2178
+
2179
+ /** @internal */
2180
+ export const forever: Schedule.Schedule<number> = unfold(0, (n) => n + 1)
2181
+
2182
+ /** @internal */
2183
+ export const once: Schedule.Schedule<void> = asVoid(recurs(1))
2184
+
2185
+ /** @internal */
2186
+ export const stop: Schedule.Schedule<void> = asVoid(recurs(0))
2187
+
2188
+ /** @internal */
2189
+ export const scheduleForked = dual<
2190
+ <Out, R2>(
2191
+ schedule: Schedule.Schedule<Out, unknown, R2>
2192
+ ) => <A, E, R>(
2193
+ self: Effect.Effect<A, E, R>
2194
+ ) => Effect.Effect<Fiber.RuntimeFiber<Out, E>, never, R | R2 | Scope>,
2195
+ <A, E, R, Out, R2>(
2196
+ self: Effect.Effect<A, E, R>,
2197
+ schedule: Schedule.Schedule<Out, unknown, R2>
2198
+ ) => Effect.Effect<Fiber.RuntimeFiber<Out, E>, never, R | R2 | Scope>
2199
+ >(2, (self, schedule) => forkScoped(schedule_Effect(self, schedule)))