Skip to content

Commit d582041

Browse files
committed
Fix code for proper results before/after enumerating singleton
1 parent 5f8d8a2 commit d582041

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

src/FSharp.Control.TaskSeq/TaskSeqInternal.fs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ module ExtraTaskSeqOperators =
1111
/// A TaskSeq workflow for IAsyncEnumerable<'T> types.
1212
let taskSeq = TaskSeqBuilder()
1313

14+
[<Struct>]
15+
type AsyncEnumStatus =
16+
| BeforeAll
17+
| WithCurrent
18+
| AfterAll
19+
1420
[<Struct>]
1521
type Action<'T, 'U, 'TaskU when 'TaskU :> Task<'U>> =
1622
| CountableAction of countable_action: (int -> 'T -> 'U)
@@ -64,15 +70,24 @@ module internal TaskSeqInternal =
6470
let singleton (source: 'T) =
6571
{ new IAsyncEnumerable<'T> with
6672
member _.GetAsyncEnumerator(_) =
67-
let mutable ended = false
73+
let mutable status = BeforeAll
6874

6975
{ new IAsyncEnumerator<'T> with
7076
member _.MoveNextAsync() =
71-
let vt = ValueTask.FromResult(not ended)
72-
ended <- true
73-
vt
77+
match status with
78+
| BeforeAll ->
79+
status <- WithCurrent
80+
ValueTask.True
81+
| WithCurrent ->
82+
status <- AfterAll
83+
ValueTask.False
84+
| AfterAll -> ValueTask.False
85+
86+
member _.Current: 'T =
87+
match status with
88+
| WithCurrent -> source
89+
| _ -> Unchecked.defaultof<'T>
7490

75-
member _.Current: 'T = if ended then Unchecked.defaultof<'T> else source
7691
member _.DisposeAsync() = ValueTask.CompletedTask
7792
}
7893
}

0 commit comments

Comments
 (0)