Effect CommunityEC
Effect Community2y ago
14 replies
rover7539

Combining two queues without dropping items

Hello everyone. I'm trying to create an effect that will take from two different queues. However, the solution I came up with drops items if both queues contain items. I feel like I'm missing an easy way to combine them.
const program = Effect.gen(function* () {
  const q1 = yield* Queue.bounded<string>(100);
  const q2 = yield* Queue.bounded<string>(100);

  const combinedTake = Effect.raceAll([q1.take, q2.take]);

  yield* Effect.fork(
    Effect.gen(function* () {
      while (true) {
        console.log(yield* combinedTake);
      }
    })
  );

  yield* q1.offer("1"); // Logged
  yield* q2.offer("2"); // Not logged
  yield* q2.offer("3"); // Logged

  yield* Effect.sleep(100);
});

Effect.runPromise(program);
Was this page helpful?