@clayroach/effect 3.19.14-source-capture.6 → 3.19.14-source-capture.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (371) hide show
  1. package/dist/cjs/Utils.js +1 -1
  2. package/dist/cjs/Utils.js.map +1 -1
  3. package/dist/cjs/internal/clock.js +1 -1
  4. package/dist/cjs/internal/clock.js.map +1 -1
  5. package/dist/esm/Utils.js +1 -1
  6. package/dist/esm/Utils.js.map +1 -1
  7. package/dist/esm/internal/clock.js +1 -1
  8. package/dist/esm/internal/clock.js.map +1 -1
  9. package/package.json +1 -1
  10. package/src/Arbitrary.ts +0 -1101
  11. package/src/Array.ts +0 -3589
  12. package/src/BigDecimal.ts +0 -1349
  13. package/src/BigInt.ts +0 -643
  14. package/src/Boolean.ts +0 -287
  15. package/src/Brand.ts +0 -360
  16. package/src/Cache.ts +0 -281
  17. package/src/Cause.ts +0 -1555
  18. package/src/Channel.ts +0 -2355
  19. package/src/ChildExecutorDecision.ts +0 -146
  20. package/src/Chunk.ts +0 -1495
  21. package/src/Clock.ts +0 -111
  22. package/src/Config.ts +0 -542
  23. package/src/ConfigError.ts +0 -270
  24. package/src/ConfigProvider.ts +0 -333
  25. package/src/ConfigProviderPathPatch.ts +0 -100
  26. package/src/Console.ts +0 -226
  27. package/src/Context.ts +0 -585
  28. package/src/Cron.ts +0 -706
  29. package/src/Data.ts +0 -596
  30. package/src/DateTime.ts +0 -1686
  31. package/src/DefaultServices.ts +0 -34
  32. package/src/Deferred.ts +0 -301
  33. package/src/Differ.ts +0 -450
  34. package/src/Duration.ts +0 -1000
  35. package/src/Effect.ts +0 -14839
  36. package/src/Effectable.ts +0 -107
  37. package/src/Either.ts +0 -1040
  38. package/src/Encoding.ts +0 -195
  39. package/src/Equal.ts +0 -98
  40. package/src/Equivalence.ts +0 -235
  41. package/src/ExecutionPlan.ts +0 -308
  42. package/src/ExecutionStrategy.ts +0 -119
  43. package/src/Exit.ts +0 -467
  44. package/src/FastCheck.ts +0 -9
  45. package/src/Fiber.ts +0 -744
  46. package/src/FiberHandle.ts +0 -540
  47. package/src/FiberId.ts +0 -195
  48. package/src/FiberMap.ts +0 -656
  49. package/src/FiberRef.ts +0 -431
  50. package/src/FiberRefs.ts +0 -204
  51. package/src/FiberRefsPatch.ts +0 -105
  52. package/src/FiberSet.ts +0 -491
  53. package/src/FiberStatus.ts +0 -108
  54. package/src/Function.ts +0 -1222
  55. package/src/GlobalValue.ts +0 -53
  56. package/src/Graph.ts +0 -3732
  57. package/src/GroupBy.ts +0 -103
  58. package/src/HKT.ts +0 -45
  59. package/src/Hash.ts +0 -195
  60. package/src/HashMap.ts +0 -519
  61. package/src/HashRing.ts +0 -317
  62. package/src/HashSet.ts +0 -2346
  63. package/src/Inspectable.ts +0 -287
  64. package/src/Iterable.ts +0 -1119
  65. package/src/JSONSchema.ts +0 -1044
  66. package/src/KeyedPool.ts +0 -167
  67. package/src/Layer.ts +0 -1251
  68. package/src/LayerMap.ts +0 -436
  69. package/src/List.ts +0 -977
  70. package/src/LogLevel.ts +0 -285
  71. package/src/LogSpan.ts +0 -25
  72. package/src/Logger.ts +0 -702
  73. package/src/Mailbox.ts +0 -268
  74. package/src/ManagedRuntime.ts +0 -180
  75. package/src/Match.ts +0 -1477
  76. package/src/MergeDecision.ts +0 -95
  77. package/src/MergeState.ts +0 -172
  78. package/src/MergeStrategy.ts +0 -107
  79. package/src/Metric.ts +0 -780
  80. package/src/MetricBoundaries.ts +0 -69
  81. package/src/MetricHook.ts +0 -151
  82. package/src/MetricKey.ts +0 -224
  83. package/src/MetricKeyType.ts +0 -262
  84. package/src/MetricLabel.ts +0 -47
  85. package/src/MetricPair.ts +0 -71
  86. package/src/MetricPolling.ts +0 -148
  87. package/src/MetricRegistry.ts +0 -48
  88. package/src/MetricState.ts +0 -257
  89. package/src/Micro.ts +0 -4405
  90. package/src/ModuleVersion.ts +0 -18
  91. package/src/MutableHashMap.ts +0 -411
  92. package/src/MutableHashSet.ts +0 -706
  93. package/src/MutableList.ts +0 -297
  94. package/src/MutableQueue.ts +0 -227
  95. package/src/MutableRef.ts +0 -202
  96. package/src/NonEmptyIterable.ts +0 -32
  97. package/src/Number.ts +0 -1071
  98. package/src/Option.ts +0 -2170
  99. package/src/Order.ts +0 -373
  100. package/src/Ordering.ts +0 -111
  101. package/src/ParseResult.ts +0 -2031
  102. package/src/PartitionedSemaphore.ts +0 -200
  103. package/src/Pipeable.ts +0 -566
  104. package/src/Pool.ts +0 -204
  105. package/src/Predicate.ts +0 -1405
  106. package/src/Pretty.ts +0 -205
  107. package/src/PrimaryKey.ts +0 -23
  108. package/src/PubSub.ts +0 -182
  109. package/src/Queue.ts +0 -644
  110. package/src/Random.ts +0 -204
  111. package/src/RateLimiter.ts +0 -138
  112. package/src/RcMap.ts +0 -141
  113. package/src/RcRef.ts +0 -122
  114. package/src/Readable.ts +0 -93
  115. package/src/Record.ts +0 -1274
  116. package/src/RedBlackTree.ts +0 -421
  117. package/src/Redacted.ts +0 -144
  118. package/src/Ref.ts +0 -180
  119. package/src/RegExp.ts +0 -38
  120. package/src/Reloadable.ts +0 -127
  121. package/src/Request.ts +0 -347
  122. package/src/RequestBlock.ts +0 -118
  123. package/src/RequestResolver.ts +0 -366
  124. package/src/Resource.ts +0 -119
  125. package/src/Runtime.ts +0 -383
  126. package/src/RuntimeFlags.ts +0 -368
  127. package/src/RuntimeFlagsPatch.ts +0 -183
  128. package/src/STM.ts +0 -2045
  129. package/src/Schedule.ts +0 -2219
  130. package/src/ScheduleDecision.ts +0 -62
  131. package/src/ScheduleInterval.ts +0 -151
  132. package/src/ScheduleIntervals.ts +0 -122
  133. package/src/Scheduler.ts +0 -353
  134. package/src/Schema.ts +0 -10914
  135. package/src/SchemaAST.ts +0 -3043
  136. package/src/Scope.ts +0 -204
  137. package/src/ScopedCache.ts +0 -151
  138. package/src/ScopedRef.ts +0 -117
  139. package/src/Secret.ts +0 -88
  140. package/src/SingleProducerAsyncInput.ts +0 -67
  141. package/src/Sink.ts +0 -1461
  142. package/src/SortedMap.ts +0 -287
  143. package/src/SortedSet.ts +0 -390
  144. package/src/Stream.ts +0 -6468
  145. package/src/StreamEmit.ts +0 -136
  146. package/src/StreamHaltStrategy.ts +0 -123
  147. package/src/Streamable.ts +0 -45
  148. package/src/String.ts +0 -778
  149. package/src/Struct.ts +0 -243
  150. package/src/Subscribable.ts +0 -100
  151. package/src/SubscriptionRef.ts +0 -298
  152. package/src/Supervisor.ts +0 -240
  153. package/src/Symbol.ts +0 -29
  154. package/src/SynchronizedRef.ts +0 -270
  155. package/src/TArray.ts +0 -495
  156. package/src/TDeferred.ts +0 -100
  157. package/src/TMap.ts +0 -515
  158. package/src/TPriorityQueue.ts +0 -223
  159. package/src/TPubSub.ts +0 -200
  160. package/src/TQueue.ts +0 -432
  161. package/src/TRandom.ts +0 -129
  162. package/src/TReentrantLock.ts +0 -224
  163. package/src/TRef.ts +0 -178
  164. package/src/TSemaphore.ts +0 -129
  165. package/src/TSet.ts +0 -365
  166. package/src/TSubscriptionRef.ts +0 -192
  167. package/src/Take.ts +0 -258
  168. package/src/TestAnnotation.ts +0 -158
  169. package/src/TestAnnotationMap.ts +0 -119
  170. package/src/TestAnnotations.ts +0 -117
  171. package/src/TestClock.ts +0 -556
  172. package/src/TestConfig.ts +0 -47
  173. package/src/TestContext.ts +0 -36
  174. package/src/TestLive.ts +0 -53
  175. package/src/TestServices.ts +0 -390
  176. package/src/TestSized.ts +0 -55
  177. package/src/Tracer.ts +0 -198
  178. package/src/Trie.ts +0 -840
  179. package/src/Tuple.ts +0 -305
  180. package/src/Types.ts +0 -353
  181. package/src/Unify.ts +0 -113
  182. package/src/UpstreamPullRequest.ts +0 -117
  183. package/src/UpstreamPullStrategy.ts +0 -121
  184. package/src/Utils.ts +0 -809
  185. package/src/index.ts +0 -1561
  186. package/src/internal/array.ts +0 -8
  187. package/src/internal/blockedRequests.ts +0 -520
  188. package/src/internal/cache.ts +0 -733
  189. package/src/internal/cause.ts +0 -1050
  190. package/src/internal/channel/channelExecutor.ts +0 -1200
  191. package/src/internal/channel/channelState.ts +0 -134
  192. package/src/internal/channel/childExecutorDecision.ts +0 -96
  193. package/src/internal/channel/continuation.ts +0 -200
  194. package/src/internal/channel/mergeDecision.ts +0 -113
  195. package/src/internal/channel/mergeState.ts +0 -120
  196. package/src/internal/channel/mergeStrategy.ts +0 -72
  197. package/src/internal/channel/singleProducerAsyncInput.ts +0 -259
  198. package/src/internal/channel/subexecutor.ts +0 -229
  199. package/src/internal/channel/upstreamPullRequest.ts +0 -84
  200. package/src/internal/channel/upstreamPullStrategy.ts +0 -87
  201. package/src/internal/channel.ts +0 -2603
  202. package/src/internal/clock.ts +0 -95
  203. package/src/internal/completedRequestMap.ts +0 -9
  204. package/src/internal/concurrency.ts +0 -54
  205. package/src/internal/config.ts +0 -716
  206. package/src/internal/configError.ts +0 -304
  207. package/src/internal/configProvider/pathPatch.ts +0 -97
  208. package/src/internal/configProvider.ts +0 -799
  209. package/src/internal/console.ts +0 -153
  210. package/src/internal/context.ts +0 -337
  211. package/src/internal/core-effect.ts +0 -2293
  212. package/src/internal/core-stream.ts +0 -998
  213. package/src/internal/core.ts +0 -3273
  214. package/src/internal/data.ts +0 -36
  215. package/src/internal/dataSource.ts +0 -327
  216. package/src/internal/dateTime.ts +0 -1277
  217. package/src/internal/defaultServices/console.ts +0 -100
  218. package/src/internal/defaultServices.ts +0 -163
  219. package/src/internal/deferred.ts +0 -46
  220. package/src/internal/differ/chunkPatch.ts +0 -211
  221. package/src/internal/differ/contextPatch.ts +0 -232
  222. package/src/internal/differ/hashMapPatch.ts +0 -220
  223. package/src/internal/differ/hashSetPatch.ts +0 -176
  224. package/src/internal/differ/orPatch.ts +0 -311
  225. package/src/internal/differ/readonlyArrayPatch.ts +0 -210
  226. package/src/internal/differ.ts +0 -200
  227. package/src/internal/doNotation.ts +0 -80
  228. package/src/internal/effect/circular.ts +0 -905
  229. package/src/internal/effectable.ts +0 -131
  230. package/src/internal/either.ts +0 -110
  231. package/src/internal/encoding/base64.ts +0 -286
  232. package/src/internal/encoding/base64Url.ts +0 -29
  233. package/src/internal/encoding/common.ts +0 -51
  234. package/src/internal/encoding/hex.ts +0 -315
  235. package/src/internal/errors.ts +0 -7
  236. package/src/internal/executionPlan.ts +0 -114
  237. package/src/internal/executionStrategy.ts +0 -74
  238. package/src/internal/fiber.ts +0 -388
  239. package/src/internal/fiberId.ts +0 -267
  240. package/src/internal/fiberMessage.ts +0 -82
  241. package/src/internal/fiberRefs/patch.ts +0 -144
  242. package/src/internal/fiberRefs.ts +0 -297
  243. package/src/internal/fiberRuntime.ts +0 -3915
  244. package/src/internal/fiberScope.ts +0 -71
  245. package/src/internal/fiberStatus.ts +0 -119
  246. package/src/internal/groupBy.ts +0 -530
  247. package/src/internal/hashMap/array.ts +0 -49
  248. package/src/internal/hashMap/bitwise.ts +0 -32
  249. package/src/internal/hashMap/config.ts +0 -14
  250. package/src/internal/hashMap/keySet.ts +0 -8
  251. package/src/internal/hashMap/node.ts +0 -391
  252. package/src/internal/hashMap.ts +0 -586
  253. package/src/internal/hashSet.ts +0 -323
  254. package/src/internal/keyedPool.ts +0 -244
  255. package/src/internal/layer/circular.ts +0 -228
  256. package/src/internal/layer.ts +0 -1487
  257. package/src/internal/logSpan.ts +0 -20
  258. package/src/internal/logger-circular.ts +0 -24
  259. package/src/internal/logger.ts +0 -485
  260. package/src/internal/mailbox.ts +0 -561
  261. package/src/internal/managedRuntime/circular.ts +0 -6
  262. package/src/internal/managedRuntime.ts +0 -134
  263. package/src/internal/matcher.ts +0 -652
  264. package/src/internal/metric/boundaries.ts +0 -75
  265. package/src/internal/metric/hook.ts +0 -483
  266. package/src/internal/metric/key.ts +0 -167
  267. package/src/internal/metric/keyType.ts +0 -238
  268. package/src/internal/metric/label.ts +0 -41
  269. package/src/internal/metric/pair.ts +0 -48
  270. package/src/internal/metric/polling.ts +0 -149
  271. package/src/internal/metric/registry.ts +0 -187
  272. package/src/internal/metric/state.ts +0 -290
  273. package/src/internal/metric.ts +0 -577
  274. package/src/internal/opCodes/cause.ts +0 -35
  275. package/src/internal/opCodes/channel.ts +0 -83
  276. package/src/internal/opCodes/channelChildExecutorDecision.ts +0 -17
  277. package/src/internal/opCodes/channelMergeDecision.ts +0 -11
  278. package/src/internal/opCodes/channelMergeState.ts +0 -17
  279. package/src/internal/opCodes/channelMergeStrategy.ts +0 -11
  280. package/src/internal/opCodes/channelState.ts +0 -23
  281. package/src/internal/opCodes/channelUpstreamPullRequest.ts +0 -11
  282. package/src/internal/opCodes/channelUpstreamPullStrategy.ts +0 -11
  283. package/src/internal/opCodes/config.ts +0 -65
  284. package/src/internal/opCodes/configError.ts +0 -35
  285. package/src/internal/opCodes/continuation.ts +0 -11
  286. package/src/internal/opCodes/deferred.ts +0 -11
  287. package/src/internal/opCodes/effect.ts +0 -89
  288. package/src/internal/opCodes/layer.ts +0 -59
  289. package/src/internal/opCodes/streamHaltStrategy.ts +0 -23
  290. package/src/internal/option.ts +0 -80
  291. package/src/internal/pool.ts +0 -432
  292. package/src/internal/pubsub.ts +0 -1762
  293. package/src/internal/query.ts +0 -204
  294. package/src/internal/queue.ts +0 -766
  295. package/src/internal/random.ts +0 -161
  296. package/src/internal/rateLimiter.ts +0 -93
  297. package/src/internal/rcMap.ts +0 -285
  298. package/src/internal/rcRef.ts +0 -192
  299. package/src/internal/redBlackTree/iterator.ts +0 -200
  300. package/src/internal/redBlackTree/node.ts +0 -68
  301. package/src/internal/redBlackTree.ts +0 -1245
  302. package/src/internal/redacted.ts +0 -73
  303. package/src/internal/ref.ts +0 -171
  304. package/src/internal/reloadable.ts +0 -140
  305. package/src/internal/request.ts +0 -177
  306. package/src/internal/resource.ts +0 -76
  307. package/src/internal/ringBuffer.ts +0 -68
  308. package/src/internal/runtime.ts +0 -558
  309. package/src/internal/runtimeFlags.ts +0 -188
  310. package/src/internal/runtimeFlagsPatch.ts +0 -103
  311. package/src/internal/schedule/decision.ts +0 -47
  312. package/src/internal/schedule/interval.ts +0 -101
  313. package/src/internal/schedule/intervals.ts +0 -180
  314. package/src/internal/schedule.ts +0 -2199
  315. package/src/internal/schema/errors.ts +0 -191
  316. package/src/internal/schema/schemaId.ts +0 -106
  317. package/src/internal/schema/util.ts +0 -50
  318. package/src/internal/scopedCache.ts +0 -644
  319. package/src/internal/scopedRef.ts +0 -118
  320. package/src/internal/secret.ts +0 -89
  321. package/src/internal/singleShotGen.ts +0 -35
  322. package/src/internal/sink.ts +0 -2120
  323. package/src/internal/stack.ts +0 -10
  324. package/src/internal/stm/core.ts +0 -817
  325. package/src/internal/stm/entry.ts +0 -59
  326. package/src/internal/stm/journal.ts +0 -123
  327. package/src/internal/stm/opCodes/stm.ts +0 -71
  328. package/src/internal/stm/opCodes/stmState.ts +0 -17
  329. package/src/internal/stm/opCodes/strategy.ts +0 -17
  330. package/src/internal/stm/opCodes/tExit.ts +0 -29
  331. package/src/internal/stm/opCodes/tryCommit.ts +0 -11
  332. package/src/internal/stm/stm.ts +0 -1453
  333. package/src/internal/stm/stmState.ts +0 -136
  334. package/src/internal/stm/tArray.ts +0 -550
  335. package/src/internal/stm/tDeferred.ts +0 -81
  336. package/src/internal/stm/tExit.ts +0 -190
  337. package/src/internal/stm/tMap.ts +0 -824
  338. package/src/internal/stm/tPriorityQueue.ts +0 -267
  339. package/src/internal/stm/tPubSub.ts +0 -551
  340. package/src/internal/stm/tQueue.ts +0 -393
  341. package/src/internal/stm/tRandom.ts +0 -140
  342. package/src/internal/stm/tReentrantLock.ts +0 -352
  343. package/src/internal/stm/tRef.ts +0 -195
  344. package/src/internal/stm/tSemaphore.ts +0 -113
  345. package/src/internal/stm/tSet.ts +0 -259
  346. package/src/internal/stm/tSubscriptionRef.ts +0 -286
  347. package/src/internal/stm/tryCommit.ts +0 -34
  348. package/src/internal/stm/txnId.ts +0 -14
  349. package/src/internal/stm/versioned.ts +0 -4
  350. package/src/internal/stream/debounceState.ts +0 -57
  351. package/src/internal/stream/emit.ts +0 -123
  352. package/src/internal/stream/haltStrategy.ts +0 -94
  353. package/src/internal/stream/handoff.ts +0 -187
  354. package/src/internal/stream/handoffSignal.ts +0 -59
  355. package/src/internal/stream/pull.ts +0 -34
  356. package/src/internal/stream/sinkEndReason.ts +0 -30
  357. package/src/internal/stream/zipAllState.ts +0 -88
  358. package/src/internal/stream/zipChunksState.ts +0 -56
  359. package/src/internal/stream.ts +0 -8801
  360. package/src/internal/string-utils.ts +0 -107
  361. package/src/internal/subscriptionRef.ts +0 -138
  362. package/src/internal/supervisor/patch.ts +0 -190
  363. package/src/internal/supervisor.ts +0 -303
  364. package/src/internal/synchronizedRef.ts +0 -114
  365. package/src/internal/take.ts +0 -199
  366. package/src/internal/testing/sleep.ts +0 -27
  367. package/src/internal/testing/suspendedWarningData.ts +0 -85
  368. package/src/internal/testing/warningData.ts +0 -94
  369. package/src/internal/tracer.ts +0 -293
  370. package/src/internal/trie.ts +0 -722
  371. package/src/internal/version.ts +0 -7
@@ -1,2199 +0,0 @@
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)))