Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added default CancellationTokens to common methods (fixes #663) #674

Merged
merged 1 commit into from
Mar 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Caliburn.Micro.Core/Conductor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Caliburn.Micro
public partial class Conductor<T> : ConductorBaseWithActiveItem<T> where T : class
{
/// <inheritdoc />
public override async Task ActivateItemAsync(T item, CancellationToken cancellationToken)
public override async Task ActivateItemAsync(T item, CancellationToken cancellationToken = default)
{
if (item != null && item.Equals(ActiveItem))
{
Expand Down Expand Up @@ -41,7 +41,7 @@ public override async Task ActivateItemAsync(T item, CancellationToken cancellat
/// <param name="close">Indicates whether or not to close the item after deactivating it.</param>
/// <param name="cancellationToken">The cancellation token to cancel operation.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
public override async Task DeactivateItemAsync(T item, bool close, CancellationToken cancellationToken)
public override async Task DeactivateItemAsync(T item, bool close, CancellationToken cancellationToken = default)
{
if (item == null || !item.Equals(ActiveItem))
{
Expand All @@ -61,7 +61,7 @@ public override async Task DeactivateItemAsync(T item, bool close, CancellationT
/// </summary>
/// <param name="cancellationToken">The cancellation token to cancel operation.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
public override async Task<bool> CanCloseAsync(CancellationToken cancellationToken)
public override async Task<bool> CanCloseAsync(CancellationToken cancellationToken = default )
{
var closeResult = await CloseStrategy.ExecuteAsync(new[] { ActiveItem }, cancellationToken);

Expand Down
4 changes: 2 additions & 2 deletions src/Caliburn.Micro.Core/ConductorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Task IConductor.ActivateItemAsync(object item, CancellationToken cancellationTok
/// <param name="item">The item to activate.</param>
/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
public abstract Task ActivateItemAsync(T item, CancellationToken cancellationToken);
public abstract Task ActivateItemAsync(T item, CancellationToken cancellationToken = default);

/// <summary>
/// Deactivates the specified item.
Expand All @@ -65,7 +65,7 @@ Task IConductor.ActivateItemAsync(object item, CancellationToken cancellationTok
/// <param name="close">Indicates whether or not to close the item after deactivating it.</param>
/// <param name="cancellationToken">The cancellation token to cancel operation.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
public abstract Task DeactivateItemAsync(T item, bool close, CancellationToken cancellationToken);
public abstract Task DeactivateItemAsync(T item, bool close, CancellationToken cancellationToken = default);

/// <summary>
/// Called by a subclass when an activation needs processing.
Expand Down
8 changes: 4 additions & 4 deletions src/Caliburn.Micro.Core/ConductorWithCollectionAllActive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ protected override async Task OnDeactivateAsync(bool close, CancellationToken ca
/// </summary>
/// <param name="cancellationToken">The cancellation token to cancel operation.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
public override async Task<bool> CanCloseAsync(CancellationToken cancellationToken)
public override async Task<bool> CanCloseAsync(CancellationToken cancellationToken = default)
{
var closeResult = await CloseStrategy.ExecuteAsync(_items.ToList(), cancellationToken);

Expand Down Expand Up @@ -130,7 +130,7 @@ await Task.WhenAll(GetType().GetRuntimeProperties()
/// <param name="item">The item to activate.</param>
/// <param name="cancellationToken">The cancellation token to cancel operation.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
public override async Task ActivateItemAsync(T item, CancellationToken cancellationToken)
public override async Task ActivateItemAsync(T item, CancellationToken cancellationToken = default)
{
if (item == null)
return;
Expand All @@ -150,7 +150,7 @@ public override async Task ActivateItemAsync(T item, CancellationToken cancellat
/// <param name="close">Indicates whether or not to close the item after deactivating it.</param>
/// <param name="cancellationToken">The cancellation token to cancel operation.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
public override async Task DeactivateItemAsync(T item, bool close, CancellationToken cancellationToken)
public override async Task DeactivateItemAsync(T item, bool close, CancellationToken cancellationToken = default)
{
if (item == null)
return;
Expand All @@ -175,7 +175,7 @@ public override IEnumerable<T> GetChildren()
return _items;
}

private async Task CloseItemCoreAsync(T item, CancellationToken cancellationToken)
private async Task CloseItemCoreAsync(T item, CancellationToken cancellationToken = default)
{
await ScreenExtensions.TryDeactivateAsync(item, true, cancellationToken);
_items.Remove(item);
Expand Down
8 changes: 4 additions & 4 deletions src/Caliburn.Micro.Core/ConductorWithCollectionOneActive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public OneActive()
/// <param name="item">The item to activate.</param>
/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
public override async Task ActivateItemAsync(T item, CancellationToken cancellationToken)
public override async Task ActivateItemAsync(T item, CancellationToken cancellationToken = default)
{
if (item != null && item.Equals(ActiveItem))
{
Expand All @@ -86,7 +86,7 @@ public override async Task ActivateItemAsync(T item, CancellationToken cancellat
/// <param name="close">Indicates whether or not to close the item after deactivating it.</param>
/// <param name="cancellationToken">The cancellation token to cancel operation.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
public override async Task DeactivateItemAsync(T item, bool close, CancellationToken cancellationToken)
public override async Task DeactivateItemAsync(T item, bool close, CancellationToken cancellationToken = default)
{
if (item == null)
return;
Expand All @@ -102,7 +102,7 @@ public override async Task DeactivateItemAsync(T item, bool close, CancellationT
}
}

private async Task CloseItemCoreAsync(T item, CancellationToken cancellationToken)
private async Task CloseItemCoreAsync(T item, CancellationToken cancellationToken = default)
{
if (item.Equals(ActiveItem))
{
Expand Down Expand Up @@ -144,7 +144,7 @@ protected virtual T DetermineNextItemToActivate(IList<T> list, int lastIndex)
/// </summary>
/// <param name="cancellationToken">The cancellation token to cancel operation.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
public override async Task<bool> CanCloseAsync(CancellationToken cancellationToken)
public override async Task<bool> CanCloseAsync(CancellationToken cancellationToken = default)
{
var closeResult = await CloseStrategy.ExecuteAsync(_items.ToList(), cancellationToken);

Expand Down
2 changes: 1 addition & 1 deletion src/Caliburn.Micro.Core/DefaultCloseStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public DefaultCloseStrategy(bool closeConductedItemsWhenConductorCannotClose = f
}

/// <inheritdoc />
public async Task<ICloseResult<T>> ExecuteAsync(IEnumerable<T> toClose, CancellationToken cancellationToken)
public async Task<ICloseResult<T>> ExecuteAsync(IEnumerable<T> toClose, CancellationToken cancellationToken = default)
{
var closeable = new List<T>();
var closeCanOccur = true;
Expand Down
2 changes: 1 addition & 1 deletion src/Caliburn.Micro.Core/EventAggregator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public virtual void Unsubscribe(object subscriber)
}

/// <inheritdoc />
public virtual Task PublishAsync(object message, Func<Func<Task>, Task> marshal, CancellationToken cancellationToken)
public virtual Task PublishAsync(object message, Func<Func<Task>, Task> marshal, CancellationToken cancellationToken = default)
{
if (message == null)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Caliburn.Micro.Core/IActivate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public interface IActivate
/// </summary>
/// <param name="cancellationToken">The cancellation token to cancel operation.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
Task ActivateAsync(CancellationToken cancellationToken);
Task ActivateAsync(CancellationToken cancellationToken = default);

/// <summary>
/// Raised after activation occurs.
Expand Down
2 changes: 1 addition & 1 deletion src/Caliburn.Micro.Core/ICloseStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ public interface ICloseStrategy<T>
/// <param name="toClose">Items that are requesting close.</param>
/// <param name="cancellationToken">The cancellation token to cancel operation.</param>
/// <returns>A task that represents the asynchronous operation and contains the result of the strategy.</returns>
Task<ICloseResult<T>> ExecuteAsync(IEnumerable<T> toClose, CancellationToken cancellationToken);
Task<ICloseResult<T>> ExecuteAsync(IEnumerable<T> toClose, CancellationToken cancellationToken = default);
}
}
4 changes: 2 additions & 2 deletions src/Caliburn.Micro.Core/IConductor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public interface IConductor : IParent, INotifyPropertyChangedEx
/// </summary>
/// <param name="item">The item to activate.</param>
/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
Task ActivateItemAsync(object item, CancellationToken cancellationToken);
Task ActivateItemAsync(object item, CancellationToken cancellationToken = default);

/// <summary>
/// Deactivates the specified item.
Expand All @@ -24,7 +24,7 @@ public interface IConductor : IParent, INotifyPropertyChangedEx
/// <param name="close">Indicates whether or not to close the item after deactivating it.</param>
/// <param name="cancellationToken">The cancellation token to cancel operation.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
Task DeactivateItemAsync(object item, bool close, CancellationToken cancellationToken);
Task DeactivateItemAsync(object item, bool close, CancellationToken cancellationToken = default);

/// <summary>
/// Occurs when an activation request is processed.
Expand Down
2 changes: 1 addition & 1 deletion src/Caliburn.Micro.Core/IDeactivate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public interface IDeactivate
/// <param name="close">Indicates whether or not this instance is being closed.</param>
/// <param name="cancellationToken">The cancellation token to cancel operation.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
Task DeactivateAsync(bool close, CancellationToken cancellationToken);
Task DeactivateAsync(bool close, CancellationToken cancellationToken = default);

/// <summary>
/// Raised after deactivation.
Expand Down
2 changes: 1 addition & 1 deletion src/Caliburn.Micro.Core/IEventAggregator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ public interface IEventAggregator
/// <param name = "marshal">Allows the publisher to provide a custom thread marshaller for the message publication.</param>
/// <param name="cancellationToken">The cancellation token to cancel operation.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
Task PublishAsync(object message, Func<Func<Task>, Task> marshal, CancellationToken cancellationToken);
Task PublishAsync(object message, Func<Func<Task>, Task> marshal, CancellationToken cancellationToken = default);
}
}
2 changes: 1 addition & 1 deletion src/Caliburn.Micro.Core/IGuardClose.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ public interface IGuardClose : IClose
/// </summary>
/// <param name="cancellationToken">The cancellation token to cancel operation.</param>
/// <returns>A task that represents the asynchronous operation and contains the result of the close.</returns>
Task<bool> CanCloseAsync(CancellationToken cancellationToken);
Task<bool> CanCloseAsync(CancellationToken cancellationToken = default);
}
}
2 changes: 1 addition & 1 deletion src/Caliburn.Micro.Core/Screen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ async Task IDeactivate.DeactivateAsync(bool close, CancellationToken cancellatio
/// </summary>
/// <param name="cancellationToken">The cancellation token to cancel operation.</param>
/// <returns>A task that represents the asynchronous operation and holds the value of the close check..</returns>
public virtual Task<bool> CanCloseAsync(CancellationToken cancellationToken)
public virtual Task<bool> CanCloseAsync(CancellationToken cancellationToken = default)
{
return Task.FromResult(true);
}
Expand Down
33 changes: 33 additions & 0 deletions src/Caliburn.Micro.Core/ScreenExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ public static Task TryActivateAsync(object potentialActivatable, CancellationTok
return potentialActivatable is IActivate activator ? activator.ActivateAsync(cancellationToken) : Task.FromResult(true);
}

/// <summary>
/// Deactivates the item if it implements <see cref="IDeactivate"/>, otherwise does nothing.
/// </summary>
/// <param name="potentialDeactivatable">The potential deactivatable.</param>
/// <param name="close">Indicates whether or not to close the item after deactivating it.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
public static Task TryDeactivateAsync(object potentialDeactivatable, bool close)
{
return potentialDeactivatable is IDeactivate deactivator ? deactivator.DeactivateAsync(close, CancellationToken.None) : Task.FromResult(true);
}

/// <summary>
/// Deactivates the item if it implements <see cref="IDeactivate"/>, otherwise does nothing.
/// </summary>
Expand All @@ -41,6 +52,17 @@ public static Task TryDeactivateAsync(object potentialDeactivatable, bool close,
return potentialDeactivatable is IDeactivate deactivator ? deactivator.DeactivateAsync(close, cancellationToken): Task.FromResult(true);
}

/// <summary>
/// Closes the specified item.
/// </summary>
/// <param name="conductor">The conductor.</param>
/// <param name="item">The item to close.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
public static Task CloseItemAsync(this IConductor conductor, object item)
{
return conductor.DeactivateItemAsync(item, true, CancellationToken.None);
}

/// <summary>
/// Closes the specified item.
/// </summary>
Expand All @@ -53,6 +75,17 @@ public static Task CloseItemAsync(this IConductor conductor, object item, Cancel
return conductor.DeactivateItemAsync(item, true, cancellationToken);
}

/// <summary>
/// Closes the specified item.
/// </summary>
/// <param name="conductor">The conductor.</param>
/// <param name="item">The item to close.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
public static Task CloseItemAsync<T>(this ConductorBase<T> conductor, T item) where T : class
{
return conductor.DeactivateItemAsync(item, true, CancellationToken.None);
}

/// <summary>
/// Closes the specified item.
/// </summary>
Expand Down