@@ -33,6 +33,7 @@ public protocol ThreadEventMethodContainer {
3333 */
3434open class EventThread : EventReceiver , EventThreadable {
3535 public typealias EventMethodTypedEventCallback < TOwner: EventThread , TEvent: Any > = ( _ sender: TOwner , _ event: TEvent , _ priority: EventPriority ) -> ( )
36+
3637 /**
3738 Property Wrapper to simplify the registration of Event Callbacks in `EventThread`-inheriting types.
3839 - Author: Simon J. Stuart
@@ -71,6 +72,34 @@ open class EventThread: EventReceiver, EventThreadable {
7172 }
7273 }
7374
75+ open class EventCallbackHandler {
76+ /**
77+ `weak` reference to the `EventThread` against which this Callback is registered.
78+ - Author: Simon J. Stuart
79+ - Version: 4.1.0
80+ */
81+ private weak var eventThread : EventThread ?
82+
83+ /**
84+ This is the Token Key assoicated with your Callback
85+ - Author: Simon J. Stuart
86+ - Version: 4.1.0
87+ */
88+ private var token : UUID
89+
90+ public func remove( ) {
91+ eventThread? . removeEventCallback ( token: token)
92+ }
93+
94+ public init ( eventThreadable: EventThread , token: UUID ) {
95+ self . eventThread = eventThreadable
96+ self . token = token
97+ }
98+ }
99+
100+ /**
101+ If this `EventThread` was spawned by an `EventPool`, this is a `weak` reference to that `EventPool`.
102+ */
74103 weak var eventPool : EventPooling ?
75104
76105 /**
@@ -121,10 +150,10 @@ open class EventThread: EventReceiver, EventThreadable {
121150 - Parameters:
122151 - callback: The code to invoke for the given `Eventable` Type
123152 - forEventType: The `Eventable` Type for which to Register the Callback
124- - Returns: A `UUID` representing a unique `token` for this Event Callback. This can be used with `removeEventCallback`
153+ - Returns: An `EventCallbackHandler` so that you can safely remove the Callback whenever you require.
125154 - Note: Version 4.1.0 adds support for multiple Callbacks per Event Type
126155 */
127- @discardableResult open func addEventCallback< TEvent: Eventable > ( _ callback: @escaping TypedEventCallback < TEvent > , forEventType: Eventable . Type ) -> UUID {
156+ @discardableResult open func addEventCallback< TEvent: Eventable > ( _ callback: @escaping TypedEventCallback < TEvent > , forEventType: Eventable . Type ) -> EventCallbackHandler {
128157 let eventTypeName = String ( reflecting: forEventType)
129158 var callbackContainer : EventCallbackContainer ? = nil
130159
@@ -150,7 +179,8 @@ open class EventThread: EventReceiver, EventThreadable {
150179 _tokens. withLock { tokens in
151180 tokens. append ( callbackContainer!. token)
152181 }
153- return callbackContainer!. token
182+
183+ return EventCallbackHandler ( eventThreadable: self , token: callbackContainer!. token)
154184 }
155185
156186 /**
0 commit comments