diff --git a/src/FSharp.Control.TaskSeq.Test/TaskSeq.Do.Tests.fs b/src/FSharp.Control.TaskSeq.Test/TaskSeq.Do.Tests.fs index c914a0e..3957a19 100644 --- a/src/FSharp.Control.TaskSeq.Test/TaskSeq.Do.Tests.fs +++ b/src/FSharp.Control.TaskSeq.Test/TaskSeq.Do.Tests.fs @@ -6,6 +6,7 @@ open FsUnit open Xunit open FSharp.Control +open System.Threading [] let ``CE taskSeq: use 'do'`` () = @@ -57,6 +58,56 @@ let ``CE taskSeq: use 'do!' with a task-delay`` () = |> verifyEmpty |> Task.map (fun _ -> value |> should equal 2) +//module CancellationToken = +// [] +// let ``CE taskSeq: use 'do!' with a default cancellation-token`` () = +// let mutable value = 0 + +// taskSeq { +// do value <- value + 1 +// do! CancellationToken() +// do value <- value + 1 +// } +// |> verifyEmpty +// |> Task.map (fun _ -> value |> should equal 2) + +// [] +// let ``CE taskSeq: use 'do!' with a timer cancellation-token - explicit`` () = task { +// let mutable value = 0 +// use tokenSource = new CancellationTokenSource(500) + +// return! +// taskSeq { +// do! tokenSource.Token // this sets the token for this taskSeq +// do value <- value + 1 +// do! Task.Delay(300, tokenSource.Token) +// do! Task.Delay(300, tokenSource.Token) +// do! Task.Delay(300, tokenSource.Token) +// do value <- value + 1 +// } +// |> verifyEmpty +// |> Task.map (fun _ -> value |> should equal 2) +// } + + +// [] +// let ``CE taskSeq: use 'do!' with a timer cancellation-token - implicit`` () = task { +// let mutable value = 0 +// use tokenSource = new CancellationTokenSource(500) + +// return! +// taskSeq { +// do! tokenSource.Token // this sets the token for this taskSeq +// do value <- value + 1 +// do! Task.Delay(300) +// do! Task.Delay(300) +// do! Task.Delay(300) +// do value <- value + 1 +// } +// |> verifyEmpty +// |> Task.map (fun _ -> value |> should equal 2) +// } + [] let ``CE taskSeq: use 'do!' with Async`` () = let mutable value = 0 diff --git a/src/FSharp.Control.TaskSeq/TaskSeqBuilder.fs b/src/FSharp.Control.TaskSeq/TaskSeqBuilder.fs index 40888af..aa984d9 100644 --- a/src/FSharp.Control.TaskSeq/TaskSeqBuilder.fs +++ b/src/FSharp.Control.TaskSeq/TaskSeqBuilder.fs @@ -565,7 +565,7 @@ module LowPriority = else Debug.logInfo "at TaskLike bind: await further" - + sm.Data.cancellationToken.ThrowIfCancellationRequested() sm.Data.awaiter <- awaiter sm.Data.current <- ValueNone false) @@ -623,6 +623,10 @@ module HighPriority = // member inline _.Bind(task: Task<'T>, continuation: ('T -> ResumableTSC<'U>)) = ResumableTSC<'U>(fun sm -> + // WTF??? + //let x = Func>(fun _ -> task) + //Task<'TResult>.Run(x, sm.Data.cancellationToken) + let mutable awaiter = task.GetAwaiter() let mutable __stack_fin = true @@ -644,15 +648,21 @@ module HighPriority = else Debug.logInfo "at Bind: await further" - + sm.Data.cancellationToken.ThrowIfCancellationRequested() sm.Data.awaiter <- awaiter sm.Data.current <- ValueNone false) - // Binding to a cancellation token. This allows `do! someCancellationToken` - member inline _.Bind(myToken: CancellationToken, continuation: (unit -> ResumableTSC<'T>)) : ResumableTSC<'T> = + //// Binding to a cancellation token. This allows `do! someCancellationToken` + //member inline _.Bind(cancellationToken, continuation: (unit -> ResumableTSC<'T>)) : ResumableTSC<'T> = + // ResumableTSC<'T>(fun sm -> + // sm.Data.cancellationToken <- cancellationToken + // (continuation ()).Invoke(&sm)) + + [] + member inline _.SetCancellationToken(cancellationToken, continuation: (unit -> ResumableTSC<'T>)) : ResumableTSC<'T> = ResumableTSC<'T>(fun sm -> - sm.Data.cancellationToken <- myToken + sm.Data.cancellationToken <- cancellationToken (continuation ()).Invoke(&sm)) member inline _.Bind(computation: Async<'T>, continuation: ('T -> ResumableTSC<'U>)) = diff --git a/src/FSharp.Control.TaskSeq/TaskSeqBuilder.fsi b/src/FSharp.Control.TaskSeq/TaskSeqBuilder.fsi index 4c185be..e393702 100644 --- a/src/FSharp.Control.TaskSeq/TaskSeqBuilder.fsi +++ b/src/FSharp.Control.TaskSeq/TaskSeqBuilder.fsi @@ -209,3 +209,8 @@ module HighPriority = member inline Bind: task: Task<'T> * continuation: ('T -> ResumableTSC<'U>) -> ResumableTSC<'U> member inline Bind: computation: Async<'T> * continuation: ('T -> ResumableTSC<'U>) -> ResumableTSC<'U> + //member inline Bind: + // cancellationToken: CancellationToken * continuation: (unit -> ResumableTSC<'T>) -> ResumableTSC<'T> + [] + member inline SetCancellationToken: + cancellationToken: CancellationToken * continuation: (unit -> ResumableTSC<'T>) -> ResumableTSC<'T> diff --git a/src/FSharp.Control.TaskSeq/TaskSeqInternal.fs b/src/FSharp.Control.TaskSeq/TaskSeqInternal.fs index 6db570b..457db94 100644 --- a/src/FSharp.Control.TaskSeq/TaskSeqInternal.fs +++ b/src/FSharp.Control.TaskSeq/TaskSeqInternal.fs @@ -76,10 +76,11 @@ module internal TaskSeqInternal = KeyNotFoundException("The predicate function or index did not satisfy any item in the task sequence.") |> raise - let inline withCancellationToken (cancellationToken: CancellationToken) (source: taskSeq<'T>) = taskSeq { - do! cancellationToken - yield! source - } + //let inline withCancellationToken (cancellationToken2: CancellationToken) (source: taskSeq<'T>) = taskSeq { + // // COMPILE ERROR HERE + // cancellationToken cancellationToken2 + // yield! source + //} let isEmpty (source: TaskSeq<_>) = checkNonNull (nameof source) source