Skip to content

Commit 84039f5

Browse files
committed
README updated and EventReceiving supports .latestOnly
- Updated README.md to reflect `.latestOnly` support in Listeners - Updated `EventReceiving` and all implementors to support `.latestOnly` as well
1 parent 8767aaf commit 84039f5

File tree

6 files changed

+36
-2
lines changed

6 files changed

+36
-2
lines changed

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,30 @@ This will remove your *Listener Callback*, meaning it will no longer be invoked
427427

428428
`EventListener`s are an extremely versatile and very powerful addition to `EventDrivenSwift`.
429429

430+
## `EventListener` with *Latest-Only* Interest
431+
Version 4.3.0 of this library introduces the concept of *Latest-Only Listeners*. A *Latest-Only Listener* is a *Listener* that will only be invoked for the very latest *Event* of its requested *Event Type*. If there are a number of older *Events* of this type pending in a Queue/Stack, they will simply be skipped over... and only the very *Latest* will invoke your *Listener*.
432+
433+
We have made it incredibly simple for you to configure your *Listener* to be a *Latest-Only Listener*. Taking the previous code example, we can simply modify it as follows:
434+
```swift
435+
class TemperatureRatingViewModel: ObservableObject {
436+
@Published var temperatureInCelsius: Float
437+
@Published var temperatureRating: TemperatureRating
438+
439+
var listenerHandle: EventListenerHandling?
440+
441+
internal func onTemperatureRatingEvent(_ event: TemperatureRatingEvent, _ priority: EventPriority) {
442+
temperatureInCelsius = event.temperatureInCelsius
443+
temperatureRating = event.temperatureRating
444+
}
445+
446+
init() {
447+
// Let's register our Event Listener Callback!
448+
listenerHandle = TemperatureRatingEvent.addListener(self, onTemperatureRatingEvent, interestedIn: .latestOnly)
449+
}
450+
}
451+
```
452+
By including the `interestedIn` optional parameter when invoking `addListener` against any `Eventable` type, and passing for this parameter a value of `.latestOnly`, we define that this *Listener* is only interested in the *Latest* `TemperatureRatingEvent` to be *Dispatched*. Should a number of `TemperatureRatingEvent`s build up in the Queue/Stack, the above-defined *Listener* will simply discard any older Events, and only invoke for the newest.
453+
430454
## `EventPool`
431455
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.
432456

Sources/EventDrivenSwift/EventDispatcher/EventDispatcher.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ open class EventDispatcher: EventHandler, EventDispatching {
102102
if receiver.receiver == nil { /// If the Recevier is `nil`...
103103
continue
104104
}
105+
if receiver.receiver!.interestedIn == .latestOnly && event.dispatchTime < latestEventDispatchTime[event.event.getEventTypeName()]! { continue } // If this Receiver is only interested in the Latest Event dispatched for this Event Type, and this Event is NOT the Latest... skip it!
105106

106107
// so, we have a receiver... let's deal with it!
107108
switch dispatchMethod {

Sources/EventDrivenSwift/EventListener/EventListener.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import Observable
1717
- Note: Inherit from this to implement a discrete unit of code designed specifically to operate upon specific `Eventable` types containing information useful to its operation(s)
1818
*/
1919
open class EventListener: EventHandler, EventListenable {
20+
public var interestedIn: EventListenerInterest = .all
21+
2022
/**
2123
Container for Event Listeners
2224
- Author: Simon J. Stuart

Sources/EventDrivenSwift/EventPool/EventPool.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import ThreadSafeSwift
1818
- Note: Event Pools own and manage all instances of the given `TEventThread` type
1919
*/
2020
open class EventPool<TEventThread: EventThreadable>: EventHandler, EventPooling {
21+
public var interestedIn: EventListenerInterest = .all
22+
2123
@ThreadSafeSemaphore public var balancer: EventPoolBalancing
2224
@ThreadSafeSemaphore public var scaler: EventPoolScaling
2325
@ThreadSafeSemaphore public var capacity: UInt8

Sources/EventDrivenSwift/EventReceiver/EventReceiver.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@ import Observable
2020
- Note: `EventThread` inherits from this
2121
*/
2222
open class EventReceiver: EventHandler, EventReceiving {
23-
23+
public var interestedIn: EventListenerInterest = .all
2424
}

Sources/EventDrivenSwift/EventReceiver/EventReceiving.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,10 @@ import Foundation
1414
- Version: 1.0.0
1515
*/
1616
public protocol EventReceiving: AnyObject, EventHandling {
17-
17+
/**
18+
Declares whether this Receiver is interested in `.all` Events, or `.latestOnly`
19+
- Author: Simon J. Stuart
20+
- Version: 4.3.0
21+
*/
22+
var interestedIn: EventListenerInterest { get set }
1823
}

0 commit comments

Comments
 (0)