Skip to content

Commit 8f0ef74

Browse files
committed
Add comments for short-circuiting
1 parent dfd338e commit 8f0ef74

File tree

1 file changed

+49
-45
lines changed

1 file changed

+49
-45
lines changed

src/FsToolkit.ErrorHandling.TaskResult/TaskResultCE.fs

Lines changed: 49 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,9 @@ module TaskResultBuilderBase =
250250
if condition () then
251251
if body.Invoke(&sm) then
252252
if sm.Data.IsResultError then
253+
// Set the result now to allow short-circuiting of the rest of the CE.
254+
// Run/RunDynamic will skip setting the result if it's already been set.
255+
// Combine/CombineDynamic will not continue if the result has been set.
253256
sm.Data.MethodBuilder.SetResult sm.Data.Result
254257
true
255258
else
@@ -273,6 +276,9 @@ module TaskResultBuilderBase =
273276
) : bool =
274277
if rf.Invoke(&sm) then
275278
if sm.Data.IsResultError then
279+
// Set the result now to allow short-circuiting of the rest of the CE.
280+
// Run/RunDynamic will skip setting the result if it's already been set.
281+
// Combine/CombineDynamic will not continue if the result has been set.
276282
sm.Data.MethodBuilder.SetResult sm.Data.Result
277283
true
278284
else
@@ -298,7 +304,7 @@ type TaskResultBuilderBase() =
298304
member inline _.Return(value: 'T) : TaskResultCode<'T, 'Error, 'T> =
299305
TaskResultCode<'T, 'Error, _>
300306
(fun sm ->
301-
printfn "Return Called --> "
307+
// printfn "Return Called --> "
302308

303309
match sm.Data.Result with
304310
| Ok _ -> sm.Data.Result <- Ok value
@@ -312,36 +318,30 @@ type TaskResultBuilderBase() =
312318
task1: TaskResultCode<'TOverall, 'Error, unit>,
313319
task2: TaskResultCode<'TOverall, 'Error, 'T>
314320
) : bool =
321+
let shouldContinue = task1.Invoke(&sm)
322+
315323
if sm.Data.IsTaskCompleted then
316324
true
325+
elif shouldContinue then
326+
task2.Invoke(&sm)
317327
else
318-
let shouldContinue = task1.Invoke(&sm)
319-
320-
if sm.Data.IsTaskCompleted then
321-
true
322-
elif shouldContinue then
323-
task2.Invoke(&sm)
324-
else
325-
let rec resume (mf: TaskResultResumptionFunc<_, _>) =
326-
TaskResultResumptionFunc<_, _>
327-
(fun sm ->
328-
if sm.Data.IsTaskCompleted then
329-
true
330-
else
331-
let shouldContinue = mf.Invoke(&sm)
328+
let rec resume (mf: TaskResultResumptionFunc<_, _>) =
329+
TaskResultResumptionFunc<_, _>
330+
(fun sm ->
331+
let shouldContinue = mf.Invoke(&sm)
332332

333-
if sm.Data.IsTaskCompleted then
334-
true
335-
elif shouldContinue then
336-
task2.Invoke(&sm)
337-
else
338-
sm.ResumptionDynamicInfo.ResumptionFunc <-
339-
(resume (sm.ResumptionDynamicInfo.ResumptionFunc))
333+
if sm.Data.IsTaskCompleted then
334+
true
335+
elif shouldContinue then
336+
task2.Invoke(&sm)
337+
else
338+
sm.ResumptionDynamicInfo.ResumptionFunc <-
339+
(resume (sm.ResumptionDynamicInfo.ResumptionFunc))
340340

341-
false)
341+
false)
342342

343-
sm.ResumptionDynamicInfo.ResumptionFunc <- (resume (sm.ResumptionDynamicInfo.ResumptionFunc))
344-
false
343+
sm.ResumptionDynamicInfo.ResumptionFunc <- (resume (sm.ResumptionDynamicInfo.ResumptionFunc))
344+
false
345345

346346
/// Chains together a step with its following step.
347347
/// Note that this requires that the first step has no result.
@@ -358,9 +358,9 @@ type TaskResultBuilderBase() =
358358
//-- RESUMABLE CODE START
359359
// NOTE: The code for code1 may contain await points! Resuming may branch directly
360360
// into this code!
361-
printfn "Combine Called Before Invoke --> "
361+
// printfn "Combine Called Before Invoke --> "
362362
let __stack_fin = task1.Invoke(&sm)
363-
printfn "Combine Called After Invoke --> %A " sm.Data.MethodBuilder.Task.Status
363+
// printfn "Combine Called After Invoke --> %A " sm.Data.MethodBuilder.Task.Status
364364

365365
if sm.Data.IsTaskCompleted then true
366366
elif __stack_fin then task2.Invoke(&sm)
@@ -387,13 +387,16 @@ type TaskResultBuilderBase() =
387387
// NOTE: The body of the state machine code for 'while' may contain await points, so resuming
388388
// the code will branch directly into the expanded 'body', branching directly into the while loop
389389
let __stack_body_fin = body.Invoke(&sm)
390-
printfn "While After Invoke --> %A" sm.Data.Result
390+
// printfn "While After Invoke --> %A" sm.Data.Result
391391
// If the body completed, we go back around the loop (__stack_go = true)
392392
// If the body yielded, we yield (__stack_go = false)
393393
__stack_go <- __stack_body_fin
394394
errored <- sm.Data.IsResultError
395395

396396
if errored then
397+
// Set the result now to allow short-circuiting of the rest of the CE.
398+
// Run/RunDynamic will skip setting the result if it's already been set.
399+
// Combine/CombineDynamic will not continue if the result has been set.
397400
sm.Data.MethodBuilder.SetResult sm.Data.Result
398401

399402
__stack_go
@@ -409,7 +412,7 @@ type TaskResultBuilderBase() =
409412
catch: exn -> TaskResultCode<'TOverall, 'Error, 'T>
410413
) : TaskResultCode<'TOverall, 'Error, 'T> =
411414

412-
printfn "Combine Called --> "
415+
// printfn "TryWith Called --> "
413416
ResumableCode.TryWith(body, catch)
414417

415418
/// Wraps a step in a try/finally. This catches exceptions both in the evaluation of the function
@@ -420,7 +423,7 @@ type TaskResultBuilderBase() =
420423
[<InlineIfLambda>] compensation: unit -> unit
421424
) : TaskResultCode<'TOverall, 'Error, 'T> =
422425

423-
printfn "TryFinally Called --> "
426+
// printfn "TryFinally Called --> "
424427

425428
ResumableCode.TryFinally(
426429
body,
@@ -461,10 +464,10 @@ type TaskResultBuilderBase() =
461464

462465
if not __stack_vtask.IsCompleted then
463466
let mutable awaiter = __stack_vtask.GetAwaiter()
464-
printfn "TryFinallyAsync Before Invoke Task.Status --> %A" sm.Data.MethodBuilder.Task.Status
467+
// printfn "TryFinallyAsync Before Invoke Task.Status --> %A" sm.Data.MethodBuilder.Task.Status
465468
let __stack_yield_fin = ResumableCode.Yield().Invoke(&sm)
466469
__stack_condition_fin <- __stack_yield_fin
467-
printfn "TryFinallyAsync Task.Status --> %A" sm.Data.MethodBuilder.Task.Status
470+
// printfn "TryFinallyAsync Task.Status --> %A" sm.Data.MethodBuilder.Task.Status
468471

469472
if not __stack_condition_fin then
470473
sm.Data.MethodBuilder.AwaitUnsafeOnCompleted(&awaiter, &sm)
@@ -538,14 +541,15 @@ type TaskResultBuilder() =
538541

539542
try
540543
sm.ResumptionDynamicInfo.ResumptionData <- null
541-
printfn "RunDynamic BeforeInvoke Data --> %A" sm.Data.Result
544+
// printfn "RunDynamic BeforeInvoke Data --> %A" sm.Data.Result
542545
let step = info.ResumptionFunc.Invoke(&sm)
543-
printfn "RunDynamic AfterInvoke Data --> %A %A" sm.Data.Result sm.Data.MethodBuilder.Task.Status
546+
// printfn "RunDynamic AfterInvoke Data --> %A %A" sm.Data.Result sm.Data.MethodBuilder.Task.Status
544547

548+
// If the `sm.Data.MethodBuilder` has already been set somewhere else (like While/WhileDynamic), we shouldn't continue
545549
if sm.Data.IsTaskCompleted then
546550
()
547551
elif step then
548-
printfn "RunDynamic Data --> %A" sm.Data.Result
552+
// printfn "RunDynamic Data --> %A" sm.Data.Result
549553
sm.Data.MethodBuilder.SetResult(sm.Data.Result)
550554
else
551555
let mutable awaiter =
@@ -580,13 +584,13 @@ type TaskResultBuilder() =
580584
let mutable __stack_exn: Exception = null
581585

582586
try
583-
printfn "Run BeforeInvoke Task.Status --> %A" sm.Data.MethodBuilder.Task.Status
587+
// printfn "Run BeforeInvoke Task.Status --> %A" sm.Data.MethodBuilder.Task.Status
584588
let __stack_code_fin = code.Invoke(&sm)
585-
printfn "Run Task.Status --> %A" sm.Data.MethodBuilder.Task.Status
586-
589+
// printfn "Run Task.Status --> %A" sm.Data.MethodBuilder.Task.Status
590+
// If the `sm.Data.MethodBuilder` has already been set somewhere else (like While/WhileDynamic), we shouldn't continue
587591
if __stack_code_fin && not sm.Data.IsTaskCompleted then
588592

589-
printfn "Run __stack_code_fin Data --> %A" sm.Data.Result
593+
// printfn "Run __stack_code_fin Data --> %A" sm.Data.Result
590594
sm.Data.MethodBuilder.SetResult(sm.Data.Result)
591595
with
592596
| exn -> __stack_exn <- exn
@@ -633,7 +637,7 @@ type BackgroundTaskResultBuilder() =
633637
try
634638
let __stack_code_fin = code.Invoke(&sm)
635639

636-
if __stack_code_fin then
640+
if __stack_code_fin && not sm.Data.IsTaskCompleted then
637641
sm.Data.MethodBuilder.SetResult(sm.Data.Result)
638642
with
639643
| exn -> sm.Data.MethodBuilder.SetException exn
@@ -701,7 +705,7 @@ module TaskResultCEExtensionsLowPriority =
701705
let cont =
702706
(TaskResultResumptionFunc<'TOverall, 'Error>
703707
(fun sm ->
704-
printfn "ByndDynamic --> %A" sm.Data.Result
708+
// printfn "ByndDynamic --> %A" sm.Data.Result
705709

706710
let result =
707711
(^Awaiter: (member GetResult : unit -> Result<'TResult1, 'Error>) (awaiter))
@@ -736,7 +740,7 @@ module TaskResultCEExtensionsLowPriority =
736740
if __useResumableCode then
737741
//-- RESUMABLE CODE START
738742
// Get an awaiter from the awaitable
739-
printfn "Bynd --> %A" sm.Data.Result
743+
// printfn "Bynd --> %A" sm.Data.Result
740744

741745
let mutable awaiter =
742746
(^TaskLike: (member GetAwaiter : unit -> ^Awaiter) (task))
@@ -759,7 +763,7 @@ module TaskResultCEExtensionsLowPriority =
759763
sm.Data.Result <- Error e
760764
true
761765
else
762-
printfn "Bind tasklike --> %A" sm.Data.MethodBuilder.Task.Status
766+
// printfn "Bind tasklike --> %A" sm.Data.MethodBuilder.Task.Status
763767
sm.Data.MethodBuilder.AwaitUnsafeOnCompleted(&awaiter, &sm)
764768
false
765769
else
@@ -813,7 +817,7 @@ module TaskResultCEExtensionsHighPriority =
813817
let cont =
814818
(TaskResultResumptionFunc<'TOverall, 'Error>
815819
(fun sm ->
816-
printfn "ByndDynamic --> %A" sm.Data.Result
820+
// printfn "ByndDynamic --> %A" sm.Data.Result
817821
let result = awaiter.GetResult()
818822

819823
match result with
@@ -841,7 +845,7 @@ module TaskResultCEExtensionsHighPriority =
841845
if __useResumableCode then
842846
//-- RESUMABLE CODE START
843847
// Get an awaiter from the task
844-
printfn "Bynd--> %A" sm.Data.Result
848+
// printfn "Bynd--> %A" sm.Data.Result
845849
let mutable awaiter = task.GetAwaiter()
846850

847851
let mutable __stack_fin = true

0 commit comments

Comments
 (0)