diff --git a/src/Schedule.ts b/src/Schedule.ts index 305c18d2fd..74a61ae4c1 100644 --- a/src/Schedule.ts +++ b/src/Schedule.ts @@ -3,7 +3,7 @@ */ import * as Duration from "./Duration.js" import type { Effect } from "./Effect.js" -import { constant, dual, identity } from "./Function.js" +import { constant, constTrue, dual, identity } from "./Function.js" import * as core from "./internal/core.js" import { type Pipeable, pipeArguments } from "./Pipeable.js" import type { Predicate } from "./Predicate.js" @@ -336,6 +336,74 @@ export const bothWith: { }) ))) +/** + * Returns a new `Schedule` that always recurs, collecting all inputs of the + * schedule into an array. + * + * @since 2.0.0 + * @category utilities + */ +export const collectInputs = ( + self: Schedule +): Schedule, Input, Error, Env> => collectWhile(passthrough(self), constTrue) + +/** + * Returns a new `Schedule` that always recurs, collecting all outputs of the + * schedule into an array. + * + * @since 2.0.0 + * @category utilities + */ +export const collectOutputs = ( + self: Schedule +): Schedule, Input, Error, Env> => collectWhile(self, constTrue) + +/** + * Returns a new `Schedule` that recurs as long as the specified `predicate` + * returns `true`, collecting all outputs of the schedule into an array. + * + * @since 2.0.0 + * @category utilities + */ +export const collectWhile: { + ( + predicate: (metadata: Schedule.Metadata & { readonly output: Output }) => boolean + ): ( + self: Schedule + ) => Schedule, Input, Error, Env> + ( + self: Schedule, + predicate: (metadata: Schedule.Metadata & { readonly output: Output }) => boolean + ): Schedule, Input, Error, Env> +} = dual(2, ( + self: Schedule, + predicate: (metadata: Schedule.Metadata & { readonly output: Output }) => boolean +): Schedule, Input, Error, Env> => collectWhileEffect(self, (meta) => core.succeed(predicate(meta)))) + +/** + * Returns a new `Schedule` that recurs as long as the specified effectful + * `predicate` returns `true`, collecting all outputs of the schedule into an + * array. + * + * @since 2.0.0 + * @category utilities + */ +export const collectWhileEffect: { + ( + predicate: (metadata: Schedule.Metadata & { readonly output: Output }) => Effect + ): ( + self: Schedule + ) => Schedule, Input, Error | Error2, Env | Env2> + ( + self: Schedule, + predicate: (metadata: Schedule.Metadata & { readonly output: Output }) => Effect + ): Schedule, Input, Error | Error2, Env | Env2> +} = dual(2, ( + self: Schedule, + predicate: (metadata: Schedule.Metadata & { readonly output: Output }) => Effect +): Schedule, Input, Error | Error2, Env | Env2> => + reduce(whileEffect(self, predicate), [] as Array, (outputs, output) => [...outputs, output])) + /** * Returns a new schedule that outputs the delay between each occurence. * @@ -695,6 +763,72 @@ export const passthrough = ( */ export const recurs = (times: number): Schedule => while_(forever, ({ recurrence }) => recurrence < times) +/** + * Returns a new `Schedule` that combines the outputs of the provided schedule + * using the specified `combine` function and starting from the specified + * `initial` state. + * + * @since 2.0.0 + * @category utilities + */ +export const reduce: { + ( + initial: State, + combine: (state: State, output: Output) => State + ): ( + self: Schedule + ) => Schedule + ( + self: Schedule, + initial: State, + combine: (state: State, output: Output) => State + ): Schedule +} = dual(3, ( + self: Schedule, + initial: State, + combine: (state: State, output: Output) => State +): Schedule => + reduceEffect(self, initial, (state, output) => core.succeed(combine(state, output)))) + +/** + * Returns a new `Schedule` that combines the outputs of the provided schedule + * using the specified effectful `combine` function and starting from the + * specified `initial` state. + * + * @since 2.0.0 + * @category utilities + */ +export const reduceEffect: { + ( + initial: State, + combine: (state: State, output: Output) => Effect + ): ( + self: Schedule + ) => Schedule + ( + self: Schedule, + initial: State, + combine: (state: State, output: Output) => Effect + ): Schedule +} = dual(3, ( + self: Schedule, + initial: State, + combine: (state: State, output: Output) => Effect +): Schedule => + fromStep(core.map(toStep(self), (step) => { + let state = initial + return (now, input) => + Pull.matchEffect(step(now, input), { + onSuccess: ([output, delay]) => + core.map(combine(state, output), (nextState) => { + state = nextState + return [nextState, delay] + }), + onFailure: core.failCause, + onHalt: (output) => core.flatMap(combine(state, output), Pull.halt) + }) + }))) + /** * Returns a schedule that recurs continuously, each repetition spaced the * specified duration from the last run. diff --git a/test/Schedule.test.ts b/test/Schedule.test.ts index 3d24c43eb2..2c5ff9571f 100644 --- a/test/Schedule.test.ts +++ b/test/Schedule.test.ts @@ -3,10 +3,46 @@ import { constant, constUndefined } from "effect/Function" import { describe, expect, it } from "./utils/extend.js" describe("Schedule", () => { + describe("collecting", () => { + it.effect("collectInputs - should collect all schedule inputs", () => + Effect.gen(function*() { + const schedule = Schedule.collectInputs(Schedule.forever) + const inputs = Array.range(1, 5) + const outputs = yield* runLast(schedule, inputs) + expect(outputs).toEqual(inputs) + })) + + it.effect("collectOutputs - should collect all schedule outputs", () => + Effect.gen(function*() { + const schedule = Schedule.collectOutputs(Schedule.forever) + const inputs = Array.makeBy(5, constUndefined) + const outputs = yield* runLast(schedule, inputs) + expect(outputs).toEqual([0, 1, 2, 3, 4]) + })) + + it.effect("collectWhile - should collect while the predicate holds", () => + Effect.gen(function*() { + const schedule = Schedule.collectWhile(Schedule.forever, ({ output }) => output < 3) + const inputs = Array.makeBy(5, constUndefined) + const outputs = yield* runLast(schedule, inputs) + expect(outputs).toEqual([0, 1, 2, 3]) + })) + + it.effect("collectWhileEffect - should collect while the effectful predicate holds", () => + Effect.gen(function*() { + const schedule = Schedule.collectWhileEffect(Schedule.forever, ({ output }) => Effect.succeed(output < 3)) + const inputs = Array.makeBy(5, constUndefined) + const outputs = yield* runLast(schedule, inputs) + expect(outputs).toEqual([0, 1, 2, 3]) + })) + }) + describe("spaced", () => { it.effect("constant delays", () => Effect.gen(function*() { - const output = yield* runDelays(Schedule.spaced(Duration.seconds(1)), Array.makeBy(5, constUndefined)) + const schedule = Schedule.spaced(Duration.seconds(1)) + const inputs = Array.makeBy(5, constUndefined) + const output = yield* runDelays(schedule, inputs) expect(output).toEqual(Array.makeBy(5, constant(Duration.seconds(1)))) })) }) @@ -14,7 +50,9 @@ describe("Schedule", () => { describe("fixed", () => { it.effect("constant delays", () => Effect.gen(function*() { - const output = yield* runDelays(Schedule.fixed(Duration.seconds(1)), Array.makeBy(5, constUndefined)) + const schedule = Schedule.fixed(Duration.seconds(1)) + const inputs = Array.makeBy(5, constUndefined) + const output = yield* runDelays(schedule, inputs) expect(output).toEqual(Array.makeBy(5, constant(Duration.seconds(1)))) })) }) @@ -22,9 +60,37 @@ describe("Schedule", () => { describe("windowed", () => { it.effect("constant delays", () => Effect.gen(function*() { - const output = yield* runDelays(Schedule.windowed(Duration.seconds(1)), Array.makeBy(5, constUndefined)) + const schedule = Schedule.windowed(Duration.seconds(1)) + const inputs = Array.makeBy(5, constUndefined) + const output = yield* runDelays(schedule, inputs) expect(output).toEqual(Array.makeBy(5, constant(Duration.seconds(1)))) })) + + it.effect("delays until the nearest window boundary", () => + Effect.gen(function*() { + const delays: Array = [] + const schedule = Schedule.windowed("1 seconds").pipe( + Schedule.while(({ recurrence }) => recurrence < 5), + Schedule.delays, + Schedule.map((delay) => { + delays.push(delay) + return delays + }) + ) + yield* Effect.sleep("1.5 seconds").pipe( + Effect.schedule(schedule), + Effect.fork + ) + yield* TestClock.setTime(Number.POSITIVE_INFINITY) + expect(delays).toEqual([ + Duration.millis(1000), + Duration.millis(500), + Duration.millis(500), + Duration.millis(500), + Duration.millis(500), + Duration.zero + ]) + })) }) }) @@ -55,3 +121,11 @@ const runDelays = ( schedule: Schedule.Schedule, input: Iterable ) => runCollect(Schedule.delays(schedule), input) + +const runLast = ( + schedule: Schedule.Schedule, + input: Iterable +) => + runCollect(schedule, input).pipe( + Effect.map((outputs) => outputs[outputs.length - 1]) + )