Fixing the Consumer in a Producer/Consumer Effect Pattern

Hello everyone, I'm playing around with Effect a bit and I'm trying to get a basic producer/consumer example working, but I'm not entirely sure how to best do it. So far, I have the following code:

import { Effect, Queue, Fiber, Console, Schedule } from "effect";

const program = Effect.gen(function* ($) {
    const queue = yield* Queue.unbounded<number>();

    let i = 0;

    const producerGen = Effect.repeat(
        Effect.gen(function* () {
            yield* Console.log(`producing value ${i}`)
            yield* Queue.offer(queue, i)
            i += 1;
        }),
        Schedule.fixed("1 second")
    )

    const consumerGen = Effect.repeat(
        Effect.gen(function* () {
            const value = yield* Queue.take(queue);
            yield* Console.log(`got ${value}`)
        }),
        Schedule.fixed("1 second")
    );

    const consumer = yield* Effect.fork(consumerGen)
    const producer = yield* Effect.fork(producerGen)

    yield* Fiber.join(consumer)
    yield* Fiber.join(producer)
});

Effect.runFork(program)


Essentially, I want to produce an item on a fixed time basis and consume as items are produced. Right now, I run the producer fiber on a 1-second basis, but if I try to just fork the consumer (without Effect.repeat) the program immediately terminates after 1 run (which makes sense, since I join at the end). How would it be possible to not have the repeat for the consumer?
Was this page helpful?