MCPcopy Create free account
hub / github.com/Effect-TS/effect / fixed

Function fixed

packages/effect/src/internal/schedule.ts:763–795  ·  view source on GitHub ↗
(intervalInput: Duration.DurationInput)

Source from the content-addressed store, hash-verified

761
762/** @internal */
763export const fixed = (intervalInput: Duration.DurationInput): Schedule.Schedule<number> => {
764 const interval = Duration.decode(intervalInput)
765 const intervalMillis = Duration.toMillis(interval)
766 return makeWithState<[Option.Option<[number, number]>, number], unknown, number>(
767 [Option.none(), 0],
768 (now, _, [option, n]) =>
769 core.sync(() => {
770 switch (option._tag) {
771 case "None": {
772 return [
773 [Option.some([now, now + intervalMillis]), n + 1],
774 n,
775 ScheduleDecision.continueWith(Interval.after(now + intervalMillis))
776 ]
777 }
778 case "Some": {
779 const [startMillis, lastRun] = option.value
780 const runningBehind = now > (lastRun + intervalMillis)
781 const boundary = Equal.equals(interval, Duration.zero)
782 ? interval
783 : Duration.millis(intervalMillis - ((now - startMillis) % intervalMillis))
784 const sleepTime = Equal.equals(boundary, Duration.zero) ? interval : boundary
785 const nextRun = runningBehind ? now : now + Duration.toMillis(sleepTime)
786 return [
787 [Option.some([startMillis, nextRun]), n + 1],
788 n,
789 ScheduleDecision.continueWith(Interval.after(nextRun))
790 ]
791 }
792 }
793 })
794 )
795}
796
797/** @internal */
798export const fromDelay = (delay: Duration.DurationInput): Schedule.Schedule<Duration.Duration> => duration(delay)

Callers

nothing calls this directly

Calls 3

makeWithStateFunction · 0.85
decodeMethod · 0.80
syncMethod · 0.80

Tested by

no test coverage detected