11namespace FsToolkit.ErrorHandling
22
3- /// Contains methods to build CancellableTasks using the F# computation expression syntax
43/// Contains methods to build CancellableTasks using the F# computation expression syntax
54[<AutoOpen>]
65module CancellableTaskResultCE =
@@ -17,8 +16,6 @@ module CancellableTaskResultCE =
1716 open IcedTasks
1817
1918
20- /// Contains methods to build CancellableTasks using the F# computation expression syntax
21-
2219 /// Contains methods to build CancellableTasks using the F# computation expression syntax
2320 type CancellableTaskResultBuilder () =
2421
@@ -33,27 +30,18 @@ module CancellableTaskResultCE =
3330 /// <summary>
3431 /// The entry point for the dynamic implementation of the corresponding operation. Do not use directly, only used when executing quotations that involve tasks or other reflective execution of F# code.
3532 /// </summary>
36- /// <summary>
37- /// The entry point for the dynamic implementation of the corresponding operation. Do not use directly, only used when executing quotations that involve tasks or other reflective execution of F# code.
38- /// </summary>
3933 static member inline RunDynamic
40- ( code : CancellableTaskResultBuilderBaseCode < 'T , 'T , 'Error , _ >)
4134 ( code : CancellableTaskResultBuilderBaseCode < 'T , 'T , 'Error , _ >)
4235 : CancellableTaskResult < 'T , 'Error > =
4336
44- let mutable sm = CancellableTaskResultBuilderBaseStateMachine< 'T, 'Error, _>()
4537 let mutable sm = CancellableTaskResultBuilderBaseStateMachine< 'T, 'Error, _>()
4638
4739 let initialResumptionFunc =
4840 CancellableTaskResultBuilderBaseResumptionFunc< 'T, 'Error, _>( fun sm ->
4941 code.Invoke(& sm)
5042 )
51- CancellableTaskResultBuilderBaseResumptionFunc< 'T, 'Error, _>( fun sm ->
52- code.Invoke(& sm)
53- )
5443
5544 let resumptionInfo =
56- { new CancellableTaskResultBuilderBaseResumptionDynamicInfo< 'T, 'Error, _>( initialResumptionFunc) with
5745 { new CancellableTaskResultBuilderBaseResumptionDynamicInfo< 'T, 'Error, _>( initialResumptionFunc) with
5846 member info.MoveNext ( sm ) =
5947 let mutable savedExn = null
@@ -62,8 +50,6 @@ module CancellableTaskResultCE =
6250 sm.ResumptionDynamicInfo.ResumptionData <- null
6351 let step = info.ResumptionFunc.Invoke(& sm)
6452
65- if step then
66- MethodBuilder.SetResult(& sm.Data.MethodBuilder, sm.Data.Result)
6753 if step then
6854 MethodBuilder.SetResult(& sm.Data.MethodBuilder, sm.Data.Result)
6955 else
@@ -79,23 +65,15 @@ module CancellableTaskResultCE =
7965 & sm
8066 )
8167
82- MethodBuilder.AwaitUnsafeOnCompleted(
83- & sm.Data.MethodBuilder,
84- & awaiter,
85- & sm
86- )
87-
8868 with exn ->
8969 savedExn <- exn
9070 // Run SetException outside the stack unwind, see https://github.com/dotnet/roslyn/issues/26567
9171 match savedExn with
9272 | null -> ()
9373 | exn -> MethodBuilder.SetException(& sm.Data.MethodBuilder, exn)
94- | exn -> MethodBuilder.SetException(& sm.Data.MethodBuilder, exn)
9574
9675 member _.SetStateMachine ( sm , state ) =
9776 MethodBuilder.SetStateMachine(& sm.Data.MethodBuilder, state)
98- MethodBuilder.SetStateMachine(& sm.Data.MethodBuilder, state)
9977 }
10078
10179 fun ( ct ) ->
@@ -105,20 +83,15 @@ module CancellableTaskResultCE =
10583 sm.Data.CancellationToken <- ct
10684 sm.ResumptionDynamicInfo <- resumptionInfo
10785 sm.Data.MethodBuilder <- AsyncTaskMethodBuilder< Result< 'T, 'Error>>. Create()
108- sm.Data.MethodBuilder <- AsyncTaskMethodBuilder< Result< 'T, 'Error>>. Create()
10986 sm.Data.MethodBuilder.Start(& sm)
11087 sm.Data.MethodBuilder.Task
11188
11289
113- /// Hosts the task code in a state machine and starts the task.
114-
11590 /// Hosts the task code in a state machine and starts the task.
11691 member inline _.Run
117- ( code : CancellableTaskResultBuilderBaseCode < 'T , 'T , 'Error , _ >)
11892 ( code : CancellableTaskResultBuilderBaseCode < 'T , 'T , 'Error , _ >)
11993 : CancellableTaskResult < 'T , 'Error > =
12094 if __ useResumableCode then
121- __ stateMachine< CancellableTaskResultBuilderBaseStateMachineData< 'T, 'Error, _>, CancellableTaskResult< 'T, 'Error>>
12295 __ stateMachine< CancellableTaskResultBuilderBaseStateMachineData< 'T, 'Error, _>, CancellableTaskResult< 'T, 'Error>>
12396 ( MoveNextMethodImpl<_>( fun sm ->
12497 //-- RESUMABLE CODE START
@@ -130,20 +103,16 @@ module CancellableTaskResultCE =
130103
131104 if __ stack_ code_ fin then
132105 MethodBuilder.SetResult(& sm.Data.MethodBuilder, sm.Data.Result)
133- if __ stack_ code_ fin then
134- MethodBuilder.SetResult(& sm.Data.MethodBuilder, sm.Data.Result)
135106 with exn ->
136107 __ stack_ exn <- exn
137108 // Run SetException outside the stack unwind, see https://github.com/dotnet/roslyn/issues/26567
138109 match __ stack_ exn with
139110 | null -> ()
140111 | exn -> MethodBuilder.SetException(& sm.Data.MethodBuilder, exn)
141- | exn -> MethodBuilder.SetException(& sm.Data.MethodBuilder, exn)
142112 //-- RESUMABLE CODE END
143113 ))
144114 ( SetStateMachineMethodImpl<_>( fun sm state ->
145115 MethodBuilder.SetStateMachine(& sm.Data.MethodBuilder, state)
146- MethodBuilder.SetStateMachine(& sm.Data.MethodBuilder, state)
147116 ))
148117 ( AfterCode<_, _>( fun sm ->
149118 let sm = sm
@@ -157,7 +126,6 @@ module CancellableTaskResultCE =
157126
158127 sm.Data.MethodBuilder <-
159128 AsyncTaskMethodBuilder< Result< 'T, 'Error>>. Create()
160- AsyncTaskMethodBuilder< Result< 'T, 'Error>>. Create()
161129
162130 sm.Data.MethodBuilder.Start(& sm)
163131 sm.Data.MethodBuilder.Task
@@ -176,14 +144,10 @@ module CancellableTaskResultCE =
176144
177145 inherit CancellableTaskResultBuilderBase()
178146
179- /// <summary>
180- /// The entry point for the dynamic implementation of the corresponding operation. Do not use directly, only used when executing quotations that involve tasks or other reflective execution of F# code.
181- /// </summary>
182147 /// <summary>
183148 /// The entry point for the dynamic implementation of the corresponding operation. Do not use directly, only used when executing quotations that involve tasks or other reflective execution of F# code.
184149 /// </summary>
185150 static member inline RunDynamic
186- ( code : CancellableTaskResultBuilderBaseCode < 'T , 'T , 'Error , _ >)
187151 ( code : CancellableTaskResultBuilderBaseCode < 'T , 'T , 'Error , _ >)
188152 : CancellableTaskResult < 'T , 'Error > =
189153 // backgroundTask { .. } escapes to a background thread where necessary
@@ -200,48 +164,34 @@ module CancellableTaskResultCE =
200164 ct
201165 )
202166
203- /// <summary>
204- /// Hosts the task code in a state machine and starts the task, executing in the ThreadPool using Task.Run
205- /// </summary>
206167 /// <summary>
207168 /// Hosts the task code in a state machine and starts the task, executing in the ThreadPool using Task.Run
208169 /// </summary>
209170 member inline _.Run
210- ( code : CancellableTaskResultBuilderBaseCode < 'T , 'T , 'Error , _ >)
211171 ( code : CancellableTaskResultBuilderBaseCode < 'T , 'T , 'Error , _ >)
212172 : CancellableTaskResult < 'T , 'Error > =
213173 if __ useResumableCode then
214- __ stateMachine< CancellableTaskResultBuilderBaseStateMachineData< 'T, 'Error, _>, CancellableTaskResult< 'T, 'Error>>
215174 __ stateMachine< CancellableTaskResultBuilderBaseStateMachineData< 'T, 'Error, _>, CancellableTaskResult< 'T, 'Error>>
216175 ( MoveNextMethodImpl<_>( fun sm ->
217176 //-- RESUMABLE CODE START
218177 __ resumeAt sm.ResumptionPoint
219178 let mutable __stack_exn : Exception = null
220- let mutable __stack_exn : Exception = null
221179
222180 try
223181 let __stack_code_fin = code.Invoke(& sm)
224182
225183 if __ stack_ code_ fin then
226184 MethodBuilder.SetResult(& sm.Data.MethodBuilder, sm.Data.Result)
227- if __ stack_ code_ fin then
228- MethodBuilder.SetResult(& sm.Data.MethodBuilder, sm.Data.Result)
229185 with exn ->
230186 __ stack_ exn <- exn
231187 // Run SetException outside the stack unwind, see https://github.com/dotnet/roslyn/issues/26567
232188 match __ stack_ exn with
233189 | null -> ()
234- | exn -> MethodBuilder.SetException(& sm.Data.MethodBuilder, exn)
235- __ stack_ exn <- exn
236- // Run SetException outside the stack unwind, see https://github.com/dotnet/roslyn/issues/26567
237- match __ stack_ exn with
238- | null -> ()
239190 | exn -> MethodBuilder.SetException(& sm.Data.MethodBuilder, exn)
240191 //-- RESUMABLE CODE END
241192 ))
242193 ( SetStateMachineMethodImpl<_>( fun sm state ->
243194 MethodBuilder.SetStateMachine(& sm.Data.MethodBuilder, state)
244- MethodBuilder.SetStateMachine(& sm.Data.MethodBuilder, state)
245195 ))
246196 ( AfterCode<_, CancellableTaskResult< 'T, 'Error>>( fun sm ->
247197 // backgroundTask { .. } escapes to a background thread where necessary
@@ -260,7 +210,6 @@ module CancellableTaskResultCE =
260210
261211 sm.Data.MethodBuilder <-
262212 AsyncTaskMethodBuilder< Result< 'T, 'Error>>. Create()
263- AsyncTaskMethodBuilder< Result< 'T, 'Error>>. Create()
264213
265214 sm.Data.MethodBuilder.Start(& sm)
266215 sm.Data.MethodBuilder.Task
@@ -277,7 +226,6 @@ module CancellableTaskResultCE =
277226 sm.Data.CancellationToken <- ct
278227
279228 sm.Data.MethodBuilder <-
280- AsyncTaskMethodBuilder< Result< 'T, 'Error>>
281229 AsyncTaskMethodBuilder< Result< 'T, 'Error>>
282230 .Create()
283231
@@ -288,79 +236,24 @@ module CancellableTaskResultCE =
288236 )
289237 ))
290238
291-
292239 else
293240 BackgroundCancellableTaskResultBuilder.RunDynamic( code)
294241
295- /// Contains the cancellableTask computation expression builder.
296242 /// Contains the cancellableTask computation expression builder.
297243 [<AutoOpen>]
298244 module CancellableTaskResultBuilder =
299245
300- /// <summary>
301- /// Builds a cancellableTask using computation expression syntax.
302- /// </summary>
303246 /// <summary>
304247 /// Builds a cancellableTask using computation expression syntax.
305248 /// </summary>
306249 let cancellableTaskResult = CancellableTaskResultBuilder()
307250
308- /// <summary>
309- /// Builds a cancellableTask using computation expression syntax which switches to execute on a background thread if not already doing so.
310- /// </summary>
311-
312251 /// <summary>
313252 /// Builds a cancellableTask using computation expression syntax which switches to execute on a background thread if not already doing so.
314253 /// </summary>
315254 let backgroundCancellableTaskResult = BackgroundCancellableTaskResultBuilder()
316255
317256
318- // There is explicitly no Binds for `CancellableTasks` in `Microsoft.FSharp.Control.TaskBuilderBase`.
319- // You need to explicitly pass in a `CancellationToken`to start it, you can use `CancellationToken.None`.
320- // Reason is I don't want people to assume cancellation is happening without the caller being explicit about where the CancellationToken came from.
321- // Similar reasoning for `IcedTasks.ColdTasks.ColdTaskBuilderBase`.
322-
323- // Contains a set of standard functional helper function
324-
325- [<RequireQualifiedAccess>]
326- module CancellableTaskResult =
327- open System.Threading .Tasks
328- open System.Threading
329- open IcedTasks
330-
331- /// <summary>Gets the default cancellation token for executing computations.</summary>
332- ///
333- /// <returns>The default CancellationToken.</returns>
334- ///
335- /// <category index="3">Cancellation and Exceptions</category>
336- ///
337- /// <example id="default-cancellation-token-1">
338- /// <code lang="F#">
339- /// use tokenSource = new CancellationTokenSource()
340- /// let primes = [ 2; 3; 5; 7; 11 ]
341- /// for i in primes do
342- /// let computation =
343- /// cancellableTask {
344- /// let! cancellationToken = CancellableTask.getCancellationToken()
345- /// do! Task.Delay(i * 1000, cancellationToken)
346- /// printfn $"{i}"
347- /// }
348- /// computation tokenSource.Token |> ignore
349- /// Thread.Sleep(6000)
350- /// tokenSource.Cancel()
351- /// printfn "Tasks Finished"
352- /// </code>
353- /// This will print "2" 2 seconds from start, "3" 3 seconds from start, "5" 5 seconds from start, cease computation and then
354- /// followed by "Tasks Finished".
355- /// </example>
356- let inline getCancellationToken () =
357- fun ( ct : CancellationToken ) -> ValueTask< CancellationToken> ct
358-
359- /// <summary>Lifts an item to a CancellableTask.</summary>
360- /// <param name="item">The item to be the result of the CancellableTask.</param>
361- /// <returns>A CancellableTask with the item as the result.</returns>
362- let inline singleton ( item : 'item ) : CancellableTaskResult < 'item , 'Error > =
363- fun _ -> Task.FromResult( Ok item)
364257[<RequireQualifiedAccess>]
365258module CancellableTaskResult =
366259 open System.Threading .Tasks
@@ -402,18 +295,6 @@ module CancellableTaskResult =
402295 fun _ -> Task.FromResult( Ok item)
403296
404297
405- /// <summary>Allows chaining of CancellableTasks.</summary>
406- /// <param name="binder">The continuation.</param>
407- /// <param name="cTask">The value.</param>
408- /// <returns>The result of the binder.</returns>
409- let inline bind
410- ( [<InlineIfLambda>] binder : 'input -> CancellableTaskResult < 'output , 'Error >)
411- ( [<InlineIfLambda>] cTask : CancellableTaskResult < 'input , 'Error >)
412- =
413- cancellableTaskResult {
414- let! cResult = cTask
415- return ! binder cResult
416- }
417298 /// <summary>Allows chaining of CancellableTasks.</summary>
418299 /// <param name="binder">The continuation.</param>
419300 /// <param name="cTask">The value.</param>
@@ -431,18 +312,6 @@ module CancellableTaskResult =
431312 /// <param name="mapper">The continuation.</param>
432313 /// <param name="cTask">The value.</param>
433314 /// <returns>The result of the mapper wrapped in a CancellableTasks.</returns>
434- let inline map
435- ( [<InlineIfLambda>] mapper : 'input -> 'output )
436- ( [<InlineIfLambda>] cTask : CancellableTaskResult < 'input , 'Error >)
437- =
438- cancellableTaskResult {
439- let! cResult = cTask
440- return mapper cResult
441- }
442- /// <summary>Allows chaining of CancellableTasks.</summary>
443- /// <param name="mapper">The continuation.</param>
444- /// <param name="cTask">The value.</param>
445- /// <returns>The result of the mapper wrapped in a CancellableTasks.</returns>
446315 let inline map
447316 ( [<InlineIfLambda>] mapper : 'input -> 'output )
448317 ( [<InlineIfLambda>] cTask : CancellableTaskResult < 'input , 'Error >)
@@ -456,19 +325,6 @@ module CancellableTaskResult =
456325 /// <param name="applicable">A function wrapped in a CancellableTasks</param>
457326 /// <param name="cTask">The value.</param>
458327 /// <returns>The result of the applicable.</returns>
459- let inline apply
460- ( [<InlineIfLambda>] applicable : CancellableTaskResult < 'input -> 'output , 'Error >)
461- ( [<InlineIfLambda>] cTask : CancellableTaskResult < 'input , 'Error >)
462- =
463- cancellableTaskResult {
464- let! ( applier : 'input -> 'output ) = applicable
465- let! ( cResult : 'input ) = cTask
466- return applier cResult
467- }
468- /// <summary>Allows chaining of CancellableTasks.</summary>
469- /// <param name="applicable">A function wrapped in a CancellableTasks</param>
470- /// <param name="cTask">The value.</param>
471- /// <returns>The result of the applicable.</returns>
472328 let inline apply
473329 ( [<InlineIfLambda>] applicable : CancellableTaskResult < 'input -> 'output , 'Error >)
474330 ( [<InlineIfLambda>] cTask : CancellableTaskResult < 'input , 'Error >)
@@ -483,19 +339,6 @@ module CancellableTaskResult =
483339 /// <param name="left">The left value.</param>
484340 /// <param name="right">The right value.</param>
485341 /// <returns>A tuple of the parameters passed in</returns>
486- let inline zip
487- ( [<InlineIfLambda>] left : CancellableTaskResult < 'left , 'Error >)
488- ( [<InlineIfLambda>] right : CancellableTaskResult < 'right , 'Error >)
489- =
490- cancellableTaskResult {
491- let! r1 = left
492- let! r2 = right
493- return r1, r2
494- }
495- /// <summary>Takes two CancellableTasks, starts them serially in order of left to right, and returns a tuple of the pair.</summary>
496- /// <param name="left">The left value.</param>
497- /// <param name="right">The right value.</param>
498- /// <returns>A tuple of the parameters passed in</returns>
499342 let inline zip
500343 ( [<InlineIfLambda>] left : CancellableTaskResult < 'left , 'Error >)
501344 ( [<InlineIfLambda>] right : CancellableTaskResult < 'right , 'Error >)
@@ -522,19 +365,3 @@ module CancellableTaskResult =
522365 let! r2 = r2
523366 return Result.zip r1 r2
524367 }
525- /// <summary>Takes two CancellableTask, starts them concurrently, and returns a tuple of the pair.</summary>
526- /// <param name="left">The left value.</param>
527- /// <param name="right">The right value.</param>
528- /// <returns>A tuple of the parameters passed in.</returns>
529- let inline parallelZip
530- ( [<InlineIfLambda>] left : CancellableTaskResult < 'left , 'Error >)
531- ( [<InlineIfLambda>] right : CancellableTaskResult < 'right , 'Error >)
532- =
533- cancellableTask {
534- let! ct = getCancellationToken ()
535- let r1 = left ct
536- let r2 = right ct
537- let! r1 = r1
538- let! r2 = r2
539- return Result.zip r1 r2
540- }
0 commit comments