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

Adds an overload to RegisterBeforeDispose() #16322

Merged
merged 6 commits into from
Jun 17, 2024
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
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 ??= [];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better than a separate collection like I suggested.

Why the new local list though? and not just use _beforeDispose directly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just personal habit...


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);
Piedone marked this conversation as resolved.
Show resolved Hide resolved

/// <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);
gvkries marked this conversation as resolved.
Show resolved Hide resolved

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