Skip to content

Commit 6c3995f

Browse files
committed
cleanup states
1 parent 2f937fd commit 6c3995f

1 file changed

Lines changed: 38 additions & 44 deletions

File tree

src/FSharp.Control.AsyncSeq/AsyncSeq.fs

Lines changed: 38 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ module AsyncSeq =
103103
return (if res then Some v else None) }
104104
member x.Dispose() = () } }
105105

106+
[<RequireQualifiedAccess>]
106107
type AppendState<'T> =
107108
| NotStarted1 of AsyncSeq<'T> * AsyncSeq<'T>
108109
| HaveEnumerator1 of IAsyncEnumerator<'T> * AsyncSeq<'T>
@@ -111,11 +112,9 @@ module AsyncSeq =
111112
| Finished
112113

113114
let append (inp1: AsyncSeq<'T>) (inp2: AsyncSeq<'T>) : AsyncSeq<'T> =
114-
// Note: this is put outside the object deliberately, so the object doesn't permanently capture inp1 and inp2
115-
let initialState = NotStarted1 (inp1, inp2)
116115
{ new IAsyncEnumerable<'T> with
117116
member x.GetEnumerator() =
118-
let state = ref initialState
117+
let state = ref (AppendState.NotStarted1 (inp1, inp2) )
119118
{ new IAsyncEnumerator<'T> with
120119
member x.MoveNext() =
121120
async { match !state with
@@ -164,17 +163,16 @@ module AsyncSeq =
164163
member x.GetEnumerator() = f().GetEnumerator() }
165164

166165

166+
[<RequireQualifiedAccess>]
167167
type BindState<'T,'U> =
168168
| NotStarted of Async<'T>
169169
| HaveEnumerator of IAsyncEnumerator<'U>
170170
| Finished
171171

172172
let bindAsync (f: 'T -> AsyncSeq<'U>) (inp : Async<'T>) : AsyncSeq<'U> =
173-
// Note: this is put outside the object deliberately, so the object doesn't permanently capture inp1 and inp2
174-
let initialState = NotStarted inp
175173
{ new IAsyncEnumerable<'U> with
176174
member x.GetEnumerator() =
177-
let state = ref initialState
175+
let state = ref (BindState.NotStarted inp)
178176
{ new IAsyncEnumerator<'U> with
179177
member x.MoveNext() =
180178
async { match !state with
@@ -234,6 +232,7 @@ module AsyncSeq =
234232
let! moven = ie.MoveNext()
235233
b := moven }
236234

235+
[<RequireQualifiedAccess>]
237236
type TryWithState<'T> =
238237
| NotStarted of AsyncSeq<'T>
239238
| HaveBodyEnumerator of IAsyncEnumerator<'T>
@@ -243,14 +242,13 @@ module AsyncSeq =
243242
/// Implements the 'TryWith' functionality for computation builder
244243
let tryWith (inp: AsyncSeq<'T>) (handler : exn -> AsyncSeq<'T>) : AsyncSeq<'T> =
245244
// Note: this is put outside the object deliberately, so the object doesn't permanently capture inp1 and inp2
246-
let initialState = NotStarted inp
247245
{ new IAsyncEnumerable<'T> with
248246
member x.GetEnumerator() =
249-
let state = ref initialState
247+
let state = ref (TryWithState.NotStarted inp)
250248
{ new IAsyncEnumerator<'T> with
251249
member x.MoveNext() =
252250
async { match !state with
253-
| NotStarted inp ->
251+
| TryWithState.NotStarted inp ->
254252
let res = ref Unchecked.defaultof<_>
255253
try
256254
res := Choice1Of2 (inp.GetEnumerator())
@@ -302,6 +300,7 @@ module AsyncSeq =
302300
| _ -> () } }
303301

304302

303+
[<RequireQualifiedAccess>]
305304
type TryFinallyState<'T> =
306305
| NotStarted of AsyncSeq<'T>
307306
| HaveBodyEnumerator of IAsyncEnumerator<'T>
@@ -310,11 +309,9 @@ module AsyncSeq =
310309
// This pushes the handler through all the async computations
311310
// The (synchronous) compensation is run when the Dispose() is called
312311
let tryFinally (inp: AsyncSeq<'T>) (compensation : unit -> unit) : AsyncSeq<'T> =
313-
// Note: this is put outside the object deliberately, so the object doesn't permanently capture inp1 and inp2
314-
let initialState = NotStarted inp
315312
{ new IAsyncEnumerable<'T> with
316313
member x.GetEnumerator() =
317-
let state = ref initialState
314+
let state = ref (TryFinallyState.NotStarted inp)
318315
{ new IAsyncEnumerator<'T> with
319316
member x.MoveNext() =
320317
async { match !state with
@@ -341,41 +338,40 @@ module AsyncSeq =
341338
| _ -> () } }
342339

343340

341+
[<RequireQualifiedAccess>]
344342
type CollectState<'T,'U> =
345343
| NotStarted of AsyncSeq<'T>
346344
| HaveInputEnumerator of IAsyncEnumerator<'T>
347345
| HaveInnerEnumerator of IAsyncEnumerator<'T> * IAsyncEnumerator<'U>
348346
| Finished
349347

350348
let collect (f: 'T -> AsyncSeq<'U>) (inp: AsyncSeq<'T>) : AsyncSeq<'U> =
351-
// Note: this is put outside the object deliberately, so the object doesn't permanently capture inp1 and inp2
352-
let initialState = NotStarted inp
353349
{ new IAsyncEnumerable<'U> with
354350
member x.GetEnumerator() =
355-
let state = ref initialState
351+
let state = ref (CollectState.NotStarted inp)
356352
{ new IAsyncEnumerator<'U> with
357353
member x.MoveNext() =
358354
async { match !state with
359-
| NotStarted inp ->
355+
| CollectState.NotStarted inp ->
360356
return!
361357
(let e1 = inp.GetEnumerator()
362-
state := HaveInputEnumerator e1
358+
state := CollectState.HaveInputEnumerator e1
363359
x.MoveNext())
364-
| HaveInputEnumerator e1 ->
360+
| CollectState.HaveInputEnumerator e1 ->
365361
let! res1 = e1.MoveNext()
366362
return!
367363
(match res1 with
368364
| Some v1 ->
369365
let e2 = (f v1).GetEnumerator()
370-
state := HaveInnerEnumerator (e1, e2)
366+
state := CollectState.HaveInnerEnumerator (e1, e2)
371367
| None ->
372368
x.Dispose()
373369
x.MoveNext())
374-
| HaveInnerEnumerator (e1, e2) ->
370+
| CollectState.HaveInnerEnumerator (e1, e2) ->
375371
let! res2 = e2.MoveNext()
376372
match res2 with
377373
| None ->
378-
state := HaveInputEnumerator e1
374+
state := CollectState.HaveInputEnumerator e1
379375
dispose e2
380376
return! x.MoveNext()
381377
| Some _ ->
@@ -384,15 +380,16 @@ module AsyncSeq =
384380
return None }
385381
member x.Dispose() =
386382
match !state with
387-
| HaveInputEnumerator e1 ->
388-
state := Finished
383+
| CollectState.HaveInputEnumerator e1 ->
384+
state := CollectState.Finished
389385
dispose e1
390-
| HaveInnerEnumerator (e1, e2) ->
391-
state := Finished
386+
| CollectState.HaveInnerEnumerator (e1, e2) ->
387+
state := CollectState.Finished
392388
dispose e2
393389
dispose e1
394390
| _ -> () } }
395391

392+
[<RequireQualifiedAccess>]
396393
type CollectSeqState<'T,'U> =
397394
| NotStarted of seq<'T>
398395
| HaveInputEnumerator of IEnumerator<'T>
@@ -401,69 +398,66 @@ module AsyncSeq =
401398

402399
// Like collect, but the input is a sequence, where no bind is required on each step of the enumeration
403400
let collectSeq (f: 'T -> AsyncSeq<'U>) (inp: seq<'T>) : AsyncSeq<'U> =
404-
// Note: this is put outside the object deliberately, so the object doesn't permanently capture inp1 and inp2
405-
let initialState = NotStarted inp
406401
{ new IAsyncEnumerable<'U> with
407402
member x.GetEnumerator() =
408-
let state = ref initialState
403+
let state = ref (CollectSeqState.NotStarted inp)
409404
{ new IAsyncEnumerator<'U> with
410405
member x.MoveNext() =
411406
async { match !state with
412-
| NotStarted inp ->
407+
| CollectSeqState.NotStarted inp ->
413408
return!
414409
(let e1 = inp.GetEnumerator()
415-
state := HaveInputEnumerator e1
410+
state := CollectSeqState.HaveInputEnumerator e1
416411
x.MoveNext())
417-
| HaveInputEnumerator e1 ->
412+
| CollectSeqState.HaveInputEnumerator e1 ->
418413
return!
419414
(if e1.MoveNext() then
420415
let e2 = (f e1.Current).GetEnumerator()
421-
state := HaveInnerEnumerator (e1, e2)
416+
state := CollectSeqState.HaveInnerEnumerator (e1, e2)
422417
else
423418
x.Dispose()
424419
x.MoveNext())
425-
| HaveInnerEnumerator (e1, e2)->
420+
| CollectSeqState.HaveInnerEnumerator (e1, e2)->
426421
let! res2 = e2.MoveNext()
427422
match res2 with
428423
| None ->
429424
return!
430-
(state := HaveInputEnumerator e1
425+
(state := CollectSeqState.HaveInputEnumerator e1
431426
dispose e2
432427
x.MoveNext())
433428
| Some _ ->
434429
return res2
435430
| _ -> return None}
436431
member x.Dispose() =
437432
match !state with
438-
| HaveInputEnumerator e1 ->
439-
state := Finished
433+
| CollectSeqState.HaveInputEnumerator e1 ->
434+
state := CollectSeqState.Finished
440435
dispose e1
441-
| HaveInnerEnumerator (e1, e2) ->
442-
state := Finished
436+
| CollectSeqState.HaveInnerEnumerator (e1, e2) ->
437+
state := CollectSeqState.Finished
443438
dispose e2
444439
dispose e1
445440
x.Dispose()
446441
| _ -> () } }
447442

443+
[<RequireQualifiedAccess>]
448444
type MapState<'T> =
449445
| NotStarted of seq<'T>
450446
| HaveEnumerator of IEnumerator<'T>
451447
| Finished
452448

453449
let ofSeq (inp: seq<'T>) : AsyncSeq<'T> =
454-
// Note: this is put outside the object deliberately, so the object doesn't permanently capture inp1 and inp2
455-
let initialState = NotStarted inp
456450
{ new IAsyncEnumerable<'T> with
457451
member x.GetEnumerator() =
458-
let state = ref initialState
452+
let state = ref (MapState.NotStarted inp)
459453
{ new IAsyncEnumerator<'T> with
460454
member x.MoveNext() =
461455
async { match !state with
462-
| NotStarted inp ->
456+
| MapState.NotStarted inp ->
463457
let e = inp.GetEnumerator()
464458
state := MapState.HaveEnumerator e
465459
return! x.MoveNext()
466-
| HaveEnumerator e ->
460+
| MapState.HaveEnumerator e ->
467461
return
468462
(if e.MoveNext() then
469463
Some e.Current
@@ -473,7 +467,7 @@ module AsyncSeq =
473467
| _ -> return None }
474468
member x.Dispose() =
475469
match !state with
476-
| HaveEnumerator e ->
470+
| MapState.HaveEnumerator e ->
477471
state := MapState.Finished
478472
dispose e
479473
| _ -> () } }

0 commit comments

Comments
 (0)