@@ -33,6 +33,7 @@ public protocol ThreadEventMethodContainer {
33
33
*/
34
34
open class EventThread : EventReceiver , EventThreadable {
35
35
public typealias EventMethodTypedEventCallback < TOwner: EventThread , TEvent: Any > = ( _ sender: TOwner , _ event: TEvent , _ priority: EventPriority ) -> ( )
36
+
36
37
/**
37
38
Property Wrapper to simplify the registration of Event Callbacks in `EventThread`-inheriting types.
38
39
- Author: Simon J. Stuart
@@ -71,6 +72,34 @@ open class EventThread: EventReceiver, EventThreadable {
71
72
}
72
73
}
73
74
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
+ */
74
103
weak var eventPool : EventPooling ?
75
104
76
105
/**
@@ -121,10 +150,10 @@ open class EventThread: EventReceiver, EventThreadable {
121
150
- Parameters:
122
151
- callback: The code to invoke for the given `Eventable` Type
123
152
- 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.
125
154
- Note: Version 4.1.0 adds support for multiple Callbacks per Event Type
126
155
*/
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 {
128
157
let eventTypeName = String ( reflecting: forEventType)
129
158
var callbackContainer : EventCallbackContainer ? = nil
130
159
@@ -150,7 +179,8 @@ open class EventThread: EventReceiver, EventThreadable {
150
179
_tokens. withLock { tokens in
151
180
tokens. append ( callbackContainer!. token)
152
181
}
153
- return callbackContainer!. token
182
+
183
+ return EventCallbackHandler ( eventThreadable: self , token: callbackContainer!. token)
154
184
}
155
185
156
186
/**
0 commit comments