Skip to content

Commit

Permalink
Adds an overload to RegisterBeforeDispose() (#16322)
Browse files Browse the repository at this point in the history
Co-authored-by: Hisham Bin Ateya <hishamco_2007@yahoo.com>
  • Loading branch information
gvkries and hishamco authored Jun 17, 2024
1 parent e6d7a19 commit 5e90031
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
19 changes: 17 additions & 2 deletions src/OrchardCore/OrchardCore.Abstractions/Shell/Scope/ShellScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,22 @@ internal async Task ActivateShellInternalAsync()
/// <summary>
/// Registers a delegate to be invoked when 'BeforeDisposeAsync()' is called on this scope.
/// </summary>
internal void BeforeDispose(Func<ShellScope, Task> callback) => (_beforeDispose ??= []).Insert(0, callback);
/// <param name="callback">The delegate to be invoked before disposal. This delegate takes a <see cref="ShellScope"/> parameter and returns a <see cref="Task"/>.</param>
/// <param name="last">A boolean value indicating whether the delegate should be invoked last.
/// If true, the delegate is added to the end of the invocation list; otherwise, it is added to the beginning.</param>
internal void BeforeDispose(Func<ShellScope, Task> callback, bool last)
{
var list = _beforeDispose ??= [];

if (last)
{
list.Add(callback);
}
else
{
list.Insert(0, callback);
}
}

/// <summary>
/// Adds a Signal (if not already present) to be sent just after 'BeforeDisposeAsync()'.
Expand All @@ -396,7 +411,7 @@ internal async Task ActivateShellInternalAsync()
/// <summary>
/// Registers a delegate to be invoked before the current shell scope will be disposed.
/// </summary>
public static void RegisterBeforeDispose(Func<ShellScope, Task> callback) => Current?.BeforeDispose(callback);
public static void RegisterBeforeDispose(Func<ShellScope, Task> callback, bool last = false) => Current?.BeforeDispose(callback, last);

/// <summary>
/// Adds a Signal (if not already present) to be sent just before the current shell scope will be disposed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,33 @@ public static class ShellScopeExtensions
/// <summary>
/// Registers a delegate task to be invoked before this shell scope will be disposed.
/// </summary>
public static ShellScope RegisterBeforeDispose(this ShellScope scope, Func<ShellScope, Task> callback)
/// <param name="scope">The <see cref="ShellScope"/> instance on which to register the delegate.</param>
/// <param name="callback">The delegate task to be invoked before disposal. This delegate takes a <see cref="ShellScope"/> parameter and returns a <see cref="Task"/>.</param>
/// <param name="last">A boolean value indicating whether the delegate should be invoked last.
/// If true, the delegate is added to the end of the invocation list; otherwise, it is added to the beginning. The default value is false.</param>
/// <returns>The <see cref="ShellScope"/> instance for chaining further calls.</returns>
public static ShellScope RegisterBeforeDispose(this ShellScope scope, Func<ShellScope, Task> callback, bool last = false)
{
scope?.BeforeDispose(callback);
scope?.BeforeDispose(callback, last);

return scope;
}

/// <summary>
/// Registers a delegate action to be invoked before this shell scope will be disposed.
/// </summary>
public static ShellScope RegisterBeforeDispose(this ShellScope scope, Action<ShellScope> callback)
/// <param name="scope">The <see cref="ShellScope"/> instance on which to register the delegate.</param>
/// <param name="callback">The delegate action to be invoked before disposal. This delegate takes a <see cref="ShellScope"/> parameter.</param>
/// <param name="last">A boolean value indicating whether the delegate should be invoked last.
/// If true, the delegate is added to the end of the invocation list; otherwise, it is added to the beginning. The default value is false.</param>
/// <returns>The <see cref="ShellScope"/> instance for chaining further calls.</returns>
public static ShellScope RegisterBeforeDispose(this ShellScope scope, Action<ShellScope> callback, bool last = false)
{
scope?.BeforeDispose(scope =>
{
callback(scope);
return Task.CompletedTask;
});
}, last);

return scope;
}
Expand Down

0 comments on commit 5e90031

Please sign in to comment.