Skip to content

Instantly share code, notes, and snippets.

@dankochetov
Created November 17, 2025 17:43
Show Gist options
  • Select an option

  • Save dankochetov/7cf1f797b00446f3b4d7a48bba433823 to your computer and use it in GitHub Desktop.

Select an option

Save dankochetov/7cf1f797b00446f3b4d7a48bba433823 to your computer and use it in GitHub Desktop.
// Scoped effect containing a stream
const effect = Effect.gen(function* () {
const stream = Stream.make(1, 2, 3, 4, 5);
yield* Effect.addFinalizer(() => Console.log("stream closed"));
return stream;
});
// Ideally, this would produce `Atom<Result<number>>`, but it produces `Atom<Result<Stream>>` instead
const atom1 = Atom.make(effect);
// This produces `Atom<Stream<number>>` instead of `Atom<Result<number>>`, because of the `Scope` requirement on the unwrapped stream
const atom2 = Atom.make(effect.pipe(Stream.unwrap));
// Current workaround
const atom3 = Atom.make(
Effect.fn(function* (get) {
const stream = yield* effect;
yield* Stream.runForEach(stream, (v) => Effect.sync(() => get.setSelf(Result.success(v)))).pipe(Effect.forkScoped);
return yield* stream.pipe(Stream.take(1), Stream.runHead, Effect.flatten);
}),
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment