Skip to content

Commit 5cf2646

Browse files
committed
updated async sample
1 parent a0dc1b0 commit 5cf2646

1 file changed

Lines changed: 16 additions & 22 deletions

File tree

src/TestConsole/ProgramTask.fs

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,33 @@
11
module ProgramTask
22

33
open FSharp.SystemCommandLine
4-
open System.CommandLine.Builder
4+
open System.Threading
55
open System.Threading.Tasks
66

7-
type WordService() =
8-
member _.Join(separator: string, words: string list) =
9-
task {
10-
do! Task.Delay(1000)
11-
return System.String.Join(separator, words)
12-
}
13-
14-
let app (svc: WordService) (words: string list, separator: string) =
7+
let app (cancel: CancellationToken, words: string array, separator: string) =
158
task {
16-
let! result = svc.Join(separator, words)
17-
result |> printfn "Result: %s"
9+
for i in [1..20] do
10+
if cancel.IsCancellationRequested then
11+
printfn "Cancellation Requested!"
12+
raise (new System.OperationCanceledException())
13+
else
14+
printfn $"{i}"
15+
do! Task.Delay(1000)
16+
17+
System.String.Join(separator, words)
18+
|> printfn "Result: %s"
1819
}
1920

2021
//[<EntryPoint>]
2122
let main argv =
22-
let words = Input.Option(["--word"; "-w"], [], "A list of words to be appended")
23+
let cancel = Input.InjectedDependency()
24+
let words = Input.Option(["--word"; "-w"], [||], "A list of words to be appended")
2325
let separator = Input.Option(["--separator"; "-s"], ", ", "A character that will separate the joined words.")
2426

25-
// Initialize app dependencies
26-
let svc = WordService()
27-
2827
rootCommand argv {
2928
description "Appends words together"
30-
inputs (words, separator)
31-
usePipeline (fun builder ->
32-
CommandLineBuilder() // Override pipeline
33-
.UseSuggestDirective()
34-
.UseParseDirective()
35-
)
36-
setHandler (app svc) // Partially apply app dependencies
29+
inputs (cancel, words, separator)
30+
setHandler app
3731
}
3832
|> Async.AwaitTask
3933
|> Async.RunSynchronously

0 commit comments

Comments
 (0)