Skip to content

Commit

Permalink
Fix Cancelling to use Canceling (#856)
Browse files Browse the repository at this point in the history
* Fix Canceling to use Cancelling

Cancelation => Cancellation
CancelableCommand  => CancellableCommand

* Assert American spellings except CancellationToken
  • Loading branch information
ChrisPulman authored Oct 6, 2024
1 parent a5bef0d commit 6eb9596
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions reactiveui/docs/handbook/commands/canceling.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
NoTitle: true
---
If your command's execution logic can take a long time to complete, it can be useful to allow the execution to be canceled. This cancellation support can be used internally by your view models, or exposed so that users have a say in the matter.
If your command's execution logic can take a long time to complete, it can be useful to allow the execution to be canceled. This cancelation support can be used internally by your view models, or exposed so that users have a say in the matter.

## Basic Cancelation

Expand Down Expand Up @@ -36,7 +36,7 @@ command.Execute().Subscribe();
cancel.OnNext(Unit.Default);
```

Of course, you wouldn't normally create a subject specifically for cancellation. Normally you already have some other observable that you want to use as a cancellation signal. An obvious example is having one command cancel another:
Of course, you wouldn't normally create a subject specifically for cancelation. Normally you already have some other observable that you want to use as a cancelation signal. An obvious example is having one command cancel another:

```cs
public class SomeViewModel : ReactiveObject
Expand All @@ -51,7 +51,7 @@ public class SomeViewModel : ReactiveObject
.TakeUntil(this.CancelCommand));
this.CancelCommand = ReactiveCommand.Create(
() => { },
this.CancellableCommand.IsExecuting);
this.CancelableCommand.IsExecuting);
}

public ReactiveCommand<Unit, Unit> CancelableCommand { get; }
Expand All @@ -64,9 +64,9 @@ Here we have a view model with a command, `CancelableCommand`, that can be cance

> **Note** At first glance there may appear to be an irresolvable circular dependency between `CancelableCommand` and `CancelCommand`. However, note that `CancelableCommand` does not need to resolve its execution pipeline until it is executed. So as long as `CancelCommand` exists before `CancelableCommand` is executed, the circular dependency is resolved.
## Cancellation with the Task Parallel Library
## Cancelation with the Task Parallel Library

Cancellation in the TPL is handled with `CancellationToken` and `CancellationTokenSource`. Rx operators that provide TPL integration will normally have overloads that will pass you a `CancellationToken` with which to create your `Task`. The idea of these overloads is that the `CancellationToken` you receive will be canceled if the subscription is disposed. So you should pass the token through to all relevant asynchronous operations. `ReactiveCommand` provides similar overloads for `CreateFromTask`.
Cancelation in the TPL is handled with `CancellationToken` and `CancellationTokenSource`. Rx operators that provide TPL integration will normally have overloads that will pass you a `CancellationToken` with which to create your `Task`. The idea of these overloads is that the `CancellationToken` you receive will be canceled if the subscription is disposed. So you should pass the token through to all relevant asynchronous operations. `ReactiveCommand` provides similar overloads for `CreateFromTask`.

Consider the following example:

Expand Down Expand Up @@ -111,7 +111,7 @@ But what if we want to cancel the execution based on an external factor, just as

Besides forgoing TPL completely \(which is recommended if possible, but not always practical\), there are actually quite a few ways to achieve this. Perhaps the easiest is to use `CreateFromObservable` instead:

Ideally avoid using `Observable.FromAsync` with a cancellation token as Exceptions do not bubble as expected, Replace any calls that use this with a ReactiveCommand and initialise it with the `ReactiveCommand.CreateFromTask(async (ct) =>{});` method.
Ideally avoid using `Observable.FromAsync` with a cancelation token as Exceptions do not bubble as expected, Replace any calls that use this with a ReactiveCommand and initialise it with the `ReactiveCommand.CreateFromTask(async (ct) =>{});` method.

```cs
public class SomeViewModel : ReactiveObject
Expand All @@ -129,7 +129,7 @@ public class SomeViewModel : ReactiveObject
// Create a command for bindings to execute the asynchronous operation
// This can be skipped if you don't need to bind to the command and just want to execute it
// i.e. var disposable = DoSomethingCommand.Execute().TakeUntil(this.CancelCommand).Subscribe();
// This will execute the command and cancel it when the `CancelCommand` is executed but can also be cancelled by disposing the disposable
// This will execute the command and cancel it when the `CancelCommand` is executed but can also be canceled by disposing the disposable
this.CancelableCommand = ReactiveCommand
.CreateFromObservable(
() => DoSomethingCommand.Execute()
Expand All @@ -138,7 +138,7 @@ public class SomeViewModel : ReactiveObject
// Create a command to cancel the asynchronous operation
this.CancelCommand = ReactiveCommand
.Create(() => { },
this.CancellableCommand.IsExecuting);
this.CancelableCommand.IsExecuting);

// Handle exceptions
CancelableCommand.ThrownExceptions
Expand Down

0 comments on commit 6eb9596

Please sign in to comment.