Skip to content

Commit 1784643

Browse files
committed
5.2.0 - README.md Updated
- Added `EventListener`'s new *Custom Event Filter* documentation to README.md with a full code sample.
1 parent d01387a commit 1784643

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,37 @@ In the above code example, `maximumAge` is a value defined in *nanoseconds*. Wit
477477

478478
This functionality is very useful when the context of an *Event*'s usage would have a known, fixed expiry.
479479

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:
486+
```swift
487+
class TemperatureRatingViewModel: ObservableObject {
488+
@Published var temperatureInCelsius: Float
489+
@Published var temperatureRating: TemperatureRating
490+
491+
var listenerHandle: EventListenerHandling?
492+
493+
internal func onTemperatureRatingEvent(_ event: TemperatureRatingEvent, _ priority: EventPriority, _ dispatchTime: DispatchTime) {
494+
temperatureInCelsius = event.temperatureInCelsius
495+
temperatureRating = event.temperatureRating
496+
}
497+
498+
internal func onTemperatureRatingEventFilter(_ event: TemperatureRatingEvent, _ priority: EventPriority, _ dispatchTime: DispatchTime) -> Bool {
499+
if event.temperatureInCelsius > 50 { return false } // If the Temperature is above 50 Degrees, this Listener is not interested in it!
500+
return true // If the Temperature is NOT above 50 Degrees, the Listener IS interested in it!
501+
}
502+
503+
init() {
504+
// Let's register our Event Listener Callback!
505+
listenerHandle = TemperatureRatingEvent.addListener(self, onTemperatureRatingEvent, interestedIn: .custom, customFilter: onTemperatureRatingEventFilter)
506+
}
507+
}
508+
```
509+
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+
480511
## `EventPool`
481512
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.
482513

0 commit comments

Comments
 (0)