|
| 1 | +// |
| 2 | +// EventThread.swift |
| 3 | +// Copyright (c) 2022, Flowduino |
| 4 | +// Authored by Simon J. Stuart on 4th August 2022 |
| 5 | +// |
| 6 | +// Subject to terms, restrictions, and liability waiver of the MIT License |
| 7 | +// |
| 8 | + |
| 9 | +import Foundation |
| 10 | +import ThreadSafeSwift |
| 11 | +import Observable |
| 12 | + |
| 13 | +/** |
| 14 | + Abstract Base Type for all `EventThread` Thread Types. |
| 15 | + - Author: Simon J. Stuart |
| 16 | + - Version: 1.0.0 |
| 17 | + - Note: Inherit from this to implement a discrete unit of code designed specifically to operate upon specific `Eventable` types containing information useful to its operation(s) |
| 18 | + */ |
| 19 | +open class EventThread: EventHandler, EventThreadable { |
| 20 | + /** |
| 21 | + Map of `Eventable` qualified Type Names against `EventCallback` methods. |
| 22 | + - Author: Simon J. Stuart |
| 23 | + - Version: 1.0.0 |
| 24 | + - Note: We use the Qualified Type Name as the Key because Types are not Hashable in Swift |
| 25 | + */ |
| 26 | + @ThreadSafeSemaphore private var eventCallbacks = [String:EventCallback]() |
| 27 | + |
| 28 | + /** |
| 29 | + Invoke the appropriate Callback for the given Event |
| 30 | + - Author: Simon J. Stuart |
| 31 | + - Version: 1.0.0 |
| 32 | + */ |
| 33 | + override internal func processEvent(_ event: any Eventable, dispatchMethod: EventDispatchMethod, priority: EventPriority) { |
| 34 | + let eventTypeName = String(reflecting: type(of: event)) |
| 35 | + var callback: EventCallback? = nil |
| 36 | + |
| 37 | + _eventCallbacks.withLock { eventCallbacks in |
| 38 | + callback = eventCallbacks[eventTypeName] |
| 39 | + } |
| 40 | + |
| 41 | + if callback == nil { return } // If there is no Callback, we will just return! |
| 42 | + |
| 43 | + callback!(event, priority) |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + Registers an Event Callback for the given `Eventable` Type |
| 48 | + - Author: Simon J. Stuart |
| 49 | + - Version: 2.1.0 |
| 50 | + - Parameters: |
| 51 | + - callback: The code to invoke for the given `Eventable` Type |
| 52 | + - forEventType: The `Eventable` Type for which to Register the Callback |
| 53 | + */ |
| 54 | + internal func addEventCallback<TEvent: Eventable>(_ callback: @escaping TypedEventCallback<TEvent>, forEventType: Eventable.Type) { |
| 55 | + let eventTypeName = String(reflecting: forEventType) |
| 56 | + |
| 57 | + _eventCallbacks.withLock { eventCallbacks in |
| 58 | + eventCallbacks[eventTypeName] = { event, priority in |
| 59 | + self.callTypedEventCallback(callback, forEvent: event, priority: priority) |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + /// We automatically register the Receiver with the Central Event Dispatcher |
| 64 | + EventCentral.shared.addReceiver(self, forEventType: forEventType) |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + Performs a Transparent Type Test, Type Cast, and Method Call via the `callback` Closure. |
| 69 | + - Author: Simon J. Stuart |
| 70 | + - Version: 2.1.0 |
| 71 | + - Parameters: |
| 72 | + - callback: The code (Closure or Callback Method) to execute for the given `forEvent`, typed generically using `TEvent` |
| 73 | + - forEvent: The instance of the `Eventable` type to be processed |
| 74 | + - priority: The `EventPriority` with which the `forEvent` was dispatched |
| 75 | + */ |
| 76 | + internal func callTypedEventCallback<TEvent: Eventable>(_ callback: @escaping TypedEventCallback<TEvent>, forEvent: Eventable, priority: EventPriority) { |
| 77 | + if let typedEvent = forEvent as? TEvent { |
| 78 | + callback(typedEvent, priority) |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + Removes an Event Callback for the given `Eventable` Type |
| 84 | + - Author: Simon J. Stuart |
| 85 | + - Version: 1.0.0 |
| 86 | + - Parameters: |
| 87 | + - forEventType: The `Eventable` Type for which to Remove the Callback |
| 88 | + */ |
| 89 | + internal func removeEventCallback(forEventType: any Eventable) { |
| 90 | + let eventTypeName = String(reflecting: forEventType) |
| 91 | + |
| 92 | + _eventCallbacks.withLock { eventCallbacks in |
| 93 | + eventCallbacks.removeValue(forKey: eventTypeName) |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + Override this to register your Event Listeners/Callbacks |
| 99 | + - Author: Simon J. Stuart |
| 100 | + - Version: 1.0.0 |
| 101 | + */ |
| 102 | + internal func registerEventListeners() { |
| 103 | + // No default implementation |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + Initializes an `EventReciever` decendant and invokes `registerEventListeners()` to register your Event Listeners/Callbacks within your `EventThread` type. |
| 108 | + - Author: Simon J. Stuart |
| 109 | + - Version: 1.0.0 |
| 110 | + */ |
| 111 | + public override init() { |
| 112 | + super.init() |
| 113 | + registerEventListeners() |
| 114 | + } |
| 115 | +} |
0 commit comments