More schedule methods (#24)

* add more schedule methods

* fix collect methods

* add tests
This commit is contained in:
Maxwell Brown
2025-01-11 15:18:13 -05:00
committed by GitHub
parent 2fd9f52542
commit fc7c1fe110
2 changed files with 212 additions and 4 deletions
+135 -1
View File
@@ -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 = <Output, Input, Error, Env>(
self: Schedule<Output, Input, Error, Env>
): Schedule<Array<Input>, 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 = <Output, Input, Error, Env>(
self: Schedule<Output, Input, Error, Env>
): Schedule<Array<Output>, 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: {
<Input, Output>(
predicate: (metadata: Schedule.Metadata<Input> & { readonly output: Output }) => boolean
): <Error, Env>(
self: Schedule<Output, Input, Error, Env>
) => Schedule<Array<Output>, Input, Error, Env>
<Output, Input, Error, Env>(
self: Schedule<Output, Input, Error, Env>,
predicate: (metadata: Schedule.Metadata<Input> & { readonly output: Output }) => boolean
): Schedule<Array<Output>, Input, Error, Env>
} = dual(2, <Output, Input, Error, Env>(
self: Schedule<Output, Input, Error, Env>,
predicate: (metadata: Schedule.Metadata<Input> & { readonly output: Output }) => boolean
): Schedule<Array<Output>, 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: {
<Input, Output, Error2, Env2>(
predicate: (metadata: Schedule.Metadata<Input> & { readonly output: Output }) => Effect<boolean, Error2, Env2>
): <Error, Env>(
self: Schedule<Output, Input, Error, Env>
) => Schedule<Array<Output>, Input, Error | Error2, Env | Env2>
<Output, Input, Error, Env, Error2, Env2>(
self: Schedule<Output, Input, Error, Env>,
predicate: (metadata: Schedule.Metadata<Input> & { readonly output: Output }) => Effect<boolean, Error2, Env2>
): Schedule<Array<Output>, Input, Error | Error2, Env | Env2>
} = dual(2, <Output, Input, Error, Env, Error2, Env2>(
self: Schedule<Output, Input, Error, Env>,
predicate: (metadata: Schedule.Metadata<Input> & { readonly output: Output }) => Effect<boolean, Error2, Env2>
): Schedule<Array<Output>, Input, Error | Error2, Env | Env2> =>
reduce(whileEffect(self, predicate), [] as Array<Output>, (outputs, output) => [...outputs, output]))
/**
* Returns a new schedule that outputs the delay between each occurence.
*
@@ -695,6 +763,72 @@ export const passthrough = <Output, Input, Error, Env>(
*/
export const recurs = (times: number): Schedule<number> => 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: {
<State, Output>(
initial: State,
combine: (state: State, output: Output) => State
): <Input, Error, Env>(
self: Schedule<Output, Input, Error, Env>
) => Schedule<State, Input, Error, Env>
<Output, Input, Error, Env, State>(
self: Schedule<Output, Input, Error, Env>,
initial: State,
combine: (state: State, output: Output) => State
): Schedule<State, Input, Error, Env>
} = dual(3, <Output, Input, Error, Env, State>(
self: Schedule<Output, Input, Error, Env>,
initial: State,
combine: (state: State, output: Output) => State
): Schedule<State, Input, Error, Env> =>
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: {
<State, Output, Error2, Env2>(
initial: State,
combine: (state: State, output: Output) => Effect<State, Error2, Env2>
): <Input, Error, Env>(
self: Schedule<Output, Input, Error, Env>
) => Schedule<State, Input, Error | Error2, Env | Env2>
<Output, Input, Error, Env, State, Error2, Env2>(
self: Schedule<Output, Input, Error, Env>,
initial: State,
combine: (state: State, output: Output) => Effect<State, Error2, Env2>
): Schedule<State, Input, Error | Error2, Env | Env2>
} = dual(3, <Output, Input, Error, Env, State, Error2, Env2>(
self: Schedule<Output, Input, Error, Env>,
initial: State,
combine: (state: State, output: Output) => Effect<State, Error2, Env2>
): Schedule<State, Input, Error | Error2, Env | Env2> =>
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.
+77 -3
View File
@@ -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<Duration.Duration> = []
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 = <Output, Input, Error, Env>(
schedule: Schedule.Schedule<Output, Input, Error, Env>,
input: Iterable<Input>
) => runCollect(Schedule.delays(schedule), input)
const runLast = <Output, Input, Error, Env>(
schedule: Schedule.Schedule<Output, Input, Error, Env>,
input: Iterable<Input>
) =>
runCollect(schedule, input).pipe(
Effect.map((outputs) => outputs[outputs.length - 1])
)