You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+97Lines changed: 97 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,6 +8,103 @@ This library contains additional [computation expressions](https://docs.microsof
8
8
9
9
-`CancellableTask<'T>` - Alias for `CancellationToken -> Task<'T>`. Allows for lazy evaluation (also known as Cold) of the tasks, similar to [F#'s Async being cold](https://docs.microsoft.com/en-us/dotnet/fsharp/tutorials/async#core-concepts-of-async). Additionally, allows for flowing a [CancellationToken](https://docs.microsoft.com/en-us/dotnet/api/system.threading.cancellationtoken?view=net-6.0) through the computation, similar to [F#'s Async cancellation support](http://tomasp.net/blog/async-csharp-differences.aspx/#:~:text=In%20F%23%20asynchronous%20workflows%2C%20the,and%20everything%20will%20work%20automatically).
10
10
11
+
-`ParallelAsync<'T>`. Utilizes the [applicative syntax](https://docs.microsoft.com/en-us/dotnet/fsharp/whats-new/fsharp-50#applicative-computation-expressions) to allow parallel execution of [Async<'T> expressions](https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/async-expressions).
12
+
13
+
## How
14
+
15
+
### ColdTask
16
+
17
+
Short example:
18
+
19
+
```fsharp
20
+
open IcedTasks
21
+
22
+
let coldTask_dont_start_immediately = task {
23
+
let mutable someValue = null
24
+
let fooColdTask = coldTask { someValue <- 42 }
25
+
do! Async.Sleep(100)
26
+
// ColdTasks will not execute until they are called, similar to how Async works
27
+
Expect.equal someValue null ""
28
+
// Calling fooColdTask will start to execute it
29
+
do! fooColdTask ()
30
+
Expect.equal someValue 42 ""
31
+
}
32
+
33
+
```
34
+
35
+
### CancellableTask
36
+
37
+
Accessing the context's CancellationToken:
38
+
39
+
1. Binding against `CancellationToken -> Task<_>`
40
+
41
+
```fsharp
42
+
let writeJunkToFile =
43
+
let path = Path.GetTempFileName()
44
+
45
+
cancellableTask {
46
+
let junk = Array.zeroCreate bufferSize
47
+
use file = File.Create(path)
48
+
49
+
for i = 1 to manyIterations do
50
+
// You can do! directly against a function with the signature of `CancellationToken -> Task<_>` to access the context's `CancellationToken`. This is slightly more performant.
51
+
do! fun ct -> file.WriteAsync(junk, 0, junk.Length, ct)
52
+
}
53
+
```
54
+
55
+
2. Binding against `CancellableTask.getCancellationToken`
56
+
57
+
```fsharp
58
+
let writeJunkToFile =
59
+
let path = Path.GetTempFileName()
60
+
61
+
cancellableTask {
62
+
let junk = Array.zeroCreate bufferSize
63
+
use file = File.Create(path)
64
+
// You can bind against `CancellableTask.getCancellationToken` to get the current context's `CancellationToken`.
65
+
let! ct = CancellableTask.getCancellationToken
66
+
for i = 1 to manyIterations do
67
+
do! file.WriteAsync(junk, 0, junk.Length, ct)
68
+
}
69
+
```
70
+
71
+
Short example:
72
+
73
+
```fsharp
74
+
let executeWriting = task {
75
+
// CancellableTask is an alias for `CancellationToken -> Task<_>` so we'll need to pass in a `CancellationToken`.
76
+
// For this example we'll use a `CancellationTokenSource` but if you were using something like ASP.NET, passing in `httpContext.RequestAborted` would be appropriate.
0 commit comments