Repeating an Effect with a Schedule and Predicate

What's the best way to repeat an effect in a spaced interval until the success value matches some predicate? I looked at Effect.repeatUntil, but can't figure out how to make the repetition follow a Schedule. I also looked at Effect.scheduleFrom, but there I can't figure out how to retain / filter the success channel value from my original effect as it seems like the success value ends up being dictated by the Schedule

import { Console, Effect, pipe, Random } from 'effect'

const start = Date.now()
console.log('Starting at', start)

type Pending = { _tag: 'Pending' }
type Completed = { _tag: 'Completed' }

const program: Effect.Effect<never, never, Completed> = pipe(
  Effect.gen(function* (_) {
    const now = Date.now()
    yield* _(Console.log('Running at ', now))
    return yield* _(
      Random.nextIntBetween(0, 100),
      Effect.map((num) =>
        num < 10
          ? ({ _tag: 'Completed' } as Completed)
          : ({ _tag: 'Pending' } as Pending),
      ),
    )
  }),
  // How do I repeat the effect at a spaced interval until 
  // first failure or first success which matches my predicate
  // while narrowing the success channel to type `Completed`?
)

Effect.runPromise(program)
Was this page helpful?