| | | 1 | | //======================================================================= |
| | | 2 | | // LifecycleHookHandler.cs |
| | | 3 | | //======================================================================= |
| | | 4 | | // MIT License, Copyright (c) 2026–present David Oravsky (mrdav30) |
| | | 5 | | // See LICENSE file in the project root for full license information. |
| | | 6 | | //======================================================================= |
| | | 7 | | |
| | | 8 | | using SwiftCollections; |
| | | 9 | | using System; |
| | | 10 | | using System.Collections.Generic; |
| | | 11 | | |
| | | 12 | | namespace Gravitas.Support; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Provides functionality to register, unregister, and invoke lifecycle hooks in a thread-safe manner. |
| | | 16 | | /// </summary> |
| | | 17 | | public class LifecycleHookHandler |
| | | 18 | | { |
| | 5083 | 19 | | private readonly object _lifecycleHookLock = new(); |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// Registers a lifecycle hook with the specified owner, order, and callback. |
| | | 23 | | /// Hooks are executed in order of their specified order, and if orders are equal, they are sorted by owner name. |
| | | 24 | | /// The returned IDisposable can be used to unregister the hook when it is no longer needed. |
| | | 25 | | /// </summary> |
| | | 26 | | /// <param name="hooks">The list of hooks to register the new hook with.</param> |
| | | 27 | | /// <param name="owner">The owner of the hook, used to identify and manage the hook.</param> |
| | | 28 | | /// <param name="order">The order in which the hook should be executed relative to other hooks.</param> |
| | | 29 | | /// <param name="callback">The callback to invoke when the hook is executed.</param> |
| | | 30 | | /// <returns>An IDisposable that can be used to unregister the hook.</returns> |
| | | 31 | | /// <exception cref="ArgumentException">Thrown if the owner is null or whitespace.</exception> |
| | | 32 | | /// <exception cref="InvalidOperationException">Thrown if a hook with the same owner is already registered.</excepti |
| | | 33 | | public IDisposable RegisterHook( |
| | | 34 | | SwiftList<OrderedLifecycleHook> hooks, |
| | | 35 | | string owner, |
| | | 36 | | int order, |
| | | 37 | | Action callback) |
| | | 38 | | { |
| | 18 | 39 | | if (string.IsNullOrWhiteSpace(owner)) |
| | 1 | 40 | | throw new ArgumentException("Lifecycle hook owner cannot be null or whitespace.", nameof(owner)); |
| | | 41 | | |
| | 17 | 42 | | SwiftThrowHelper.ThrowIfNull(callback, nameof(callback)); |
| | | 43 | | |
| | 16 | 44 | | lock (_lifecycleHookLock) |
| | | 45 | | { |
| | 38 | 46 | | for (int i = 0; i < hooks.Count; i++) |
| | | 47 | | { |
| | 4 | 48 | | if (hooks[i].Owner == owner) |
| | 1 | 49 | | throw new InvalidOperationException($"Lifecycle hook '{owner}' is already registered."); |
| | | 50 | | } |
| | | 51 | | |
| | 15 | 52 | | hooks.Add(new OrderedLifecycleHook(owner, order, callback)); |
| | 15 | 53 | | hooks.SortInPlace(CompareHook()); |
| | 15 | 54 | | } |
| | | 55 | | |
| | 15 | 56 | | return new LifecycleHookRegistration(() => UnregisterHook(hooks, owner)); |
| | | 57 | | } |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Unregisters a lifecycle hook based on the specified owner. |
| | | 61 | | /// </summary> |
| | | 62 | | /// <param name="hooks">The list of hooks to unregister the hook from.</param> |
| | | 63 | | /// <param name="owner">The owner of the hook to unregister.</param> |
| | | 64 | | public void UnregisterHook(SwiftList<OrderedLifecycleHook> hooks, string owner) |
| | | 65 | | { |
| | 15 | 66 | | lock (_lifecycleHookLock) |
| | | 67 | | { |
| | 15 | 68 | | hooks.RemoveAll(hook => hook.Owner == owner); |
| | 15 | 69 | | } |
| | 15 | 70 | | } |
| | | 71 | | |
| | | 72 | | /// <summary> |
| | | 73 | | /// Invokes all registered lifecycle hooks in the order they were registered. |
| | | 74 | | /// </summary> |
| | | 75 | | /// <param name="hooks">The list of hooks to invoke.</param> |
| | | 76 | | public void InvokeHooks(SwiftList<OrderedLifecycleHook> hooks) |
| | | 77 | | { |
| | | 78 | | OrderedLifecycleHook[] snapshot; |
| | 12 | 79 | | lock (_lifecycleHookLock) |
| | | 80 | | { |
| | 12 | 81 | | snapshot = hooks.ToArray(); |
| | 12 | 82 | | } |
| | | 83 | | |
| | 50 | 84 | | for (int i = 0; i < snapshot.Length; i++) |
| | 16 | 85 | | snapshot[i].Callback(); |
| | 9 | 86 | | } |
| | | 87 | | |
| | | 88 | | private static Comparer<OrderedLifecycleHook> CompareHook() |
| | | 89 | | { |
| | 15 | 90 | | return Comparer<OrderedLifecycleHook>.Create((left, right) => |
| | 15 | 91 | | { |
| | 15 | 92 | | int orderCompare = left.Order.CompareTo(right.Order); |
| | 15 | 93 | | if (orderCompare != 0) |
| | 15 | 94 | | return orderCompare; |
| | 15 | 95 | | |
| | 15 | 96 | | return StringComparer.Ordinal.Compare(left.Owner, right.Owner); |
| | 15 | 97 | | }); |
| | | 98 | | } |
| | | 99 | | } |