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
Copy file name to clipboardExpand all lines: README.md
+31
Original file line number
Diff line number
Diff line change
@@ -477,6 +477,37 @@ In the above code example, `maximumAge` is a value defined in *nanoseconds*. Wit
477
477
478
478
This functionality is very useful when the context of an *Event*'s usage would have a known, fixed expiry.
479
479
480
+
## `EventListener` with *Custom Event Filtering* Interest
481
+
Version 5.2.0 of this library introduces the concept of *Custom Event Filtering* for *Listeners*.
482
+
483
+
Now, when registering a *Listener* for an `Eventable` type, you can specify a `customFilter`*Callback* which, ultimately, returns a `Bool` where `true` means that the *Listener* is interested in the *Event*, and `false` means that the *Listener* is **not** interested in the *Event*.
484
+
485
+
We have made it simple for you to configure a *Custom Filter* for your *Listener*. Taking the previous code example, we can simply modify it as follows:
The above code will ensure that the `onTemperatureRatingEvent` method is only invoked for a `TemperatureRatingEvent` where its `temperatureInCelsius` is less than or equal to 50 Degrees Celsius. Any `TemperatureRatingEvent` with a `temperatureInCelsius` greater than 50 will simply be ignored by this *Listener*.
510
+
480
511
## `EventPool`
481
512
Version 4.0.0 introduces the extremely powerful `EventPool` solution, making it possible to create managed groups of `EventThread`s, where inbound *Events* will be directed to the best `EventThread` in the `EventPool` at any given moment.
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-2
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