Created
November 17, 2025 17:43
-
-
Save dankochetov/7cf1f797b00446f3b4d7a48bba433823 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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