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