You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- Added support to `EventListener` to take a custom Filter Callback (returning a `Bool` to determine whether or not the `Listener` is interested in the `Eventable`
Convienience `typealias` used for Typed Event Callbacks
@@ -63,7 +64,7 @@ public protocol EventListenable: AnyObject, EventReceiving {
63
64
- maximumAge: If `interestedIn` == `.youngerThan`, this is the number of nanoseconds between the time of dispatch and the moment of processing where the Listener will be interested in the Event. Any Event older will be ignored
64
65
- Returns: A `UUID` value representing the `token` associated with this Event Callback
Copy file name to clipboardExpand all lines: Sources/EventDrivenSwift/EventListener/EventListener.swift
+29-2Lines changed: 29 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -34,6 +34,7 @@ open class EventListener: EventHandler, EventListenable {
34
34
varexecuteOn:ExecuteEventOn=.requesterThread
35
35
varinterestedIn:EventListenerInterest=.all
36
36
varmaximumEventAge:UInt64=0
37
+
varcustomFilter:EventFilterCallback?
37
38
}
38
39
39
40
/**
@@ -73,6 +74,8 @@ open class EventListener: EventHandler, EventListenable {
73
74
74
75
if listener.interestedIn ==.youngerThan && listener.maximumEventAge !=0 && (DispatchTime.now().uptimeNanoseconds - event.dispatchTime.uptimeNanoseconds)> listener.maximumEventAge {continue} // If this Receiver has a maximum age of interest, and this Event is older than that... skip it!
75
76
77
+
if listener.interestedIn ==.custom && (listener.customFilter ==nil || !listener.customFilter!(event.event, priority, event.dispatchTime)){continue}
78
+
76
79
switch listener.executeOn {
77
80
case.requesterThread:
78
81
Task{ // We raise a Task because we don't want the entire Listener blocked in the event the dispatchQueue is busy or blocked!
@@ -91,12 +94,18 @@ open class EventListener: EventHandler, EventListenable {
Performs a Transparent Type Test, Type Cast, and Method Call to the Custom Filter via a `callback` Closure.
183
+
- Author: Simon J. Stuart
184
+
- Version: 5.2.0
185
+
- Parameters:
186
+
- callback: The code (Closure or Callback Method) to execute for the given `forEvent`, typed generically using `TEvent`... returns `true` if the Listener is interested in `forEvent`, `false` if the Listener wants to ignore it
187
+
- forEvent: The instance of the `Eventable` type to be processed
188
+
- priority: The `EventPriority` with which the `forEvent` was dispatched
189
+
- dispatchTime: The `DispatchTime` at which `forEvent` was Dispatched
0 commit comments