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
+26
Original file line number
Diff line number
Diff line change
@@ -451,6 +451,32 @@ class TemperatureRatingViewModel: ObservableObject {
451
451
```
452
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
453
454
+
## `EventListener` with *Maximum Age* Interest
455
+
Version 5.1.0 of this library introduces the concent of *Maximum Age Listeners*. A *Maximum Age Listener* is a *Listener* that will only be invoked for *Events* of its registered *Event Type* that are younger than a defined *Maximum Age*. Any *Event* older than the defined *Maximum Age* will be skipped over, while any *Event* younger will invoke your *Listener*.
456
+
457
+
We have made it simple for you to configure your *Listener* to define a *Maximum Age* interest. Taking the previous code example, we can simply modify it as follows:
In the above code example, `maximumAge` is a value defined in *nanoseconds*. With that in mind, `1 * 1_000_000_000` would be 1 second. This means that, any `TemperatureRatingEvent` older than 1 second would be ignored by the *Listener*, while any `TemperatureRatingEvent`*younger* than 1 second would invoke the `onTemperatureRatingEvent` method.
477
+
478
+
This functionality is very useful when the context of an *Event*'s usage would have a known, fixed expiry.
479
+
454
480
## `EventPool`
455
481
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.
Copy file name to clipboardExpand all lines: Sources/EventDrivenSwift/EventDispatcher/EventDispatcher.swift
+3
Original file line number
Diff line number
Diff line change
@@ -102,8 +102,11 @@ open class EventDispatcher: EventHandler, EventDispatching {
102
102
if receiver.receiver ==nil{ /// If the Recevier is `nil`...
103
103
continue
104
104
}
105
+
105
106
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!
106
107
108
+
if receiver.receiver!.interestedIn ==.youngerThan && receiver.receiver!.maximumEventAge !=0 && (DispatchTime.now().uptimeNanoseconds - event.dispatchTime.uptimeNanoseconds)> receiver.receiver!.maximumEventAge {continue} // If this Receiver has a maximum age of interest, and this Event is older than that... skip it!
Copy file name to clipboardExpand all lines: Sources/EventDrivenSwift/EventListener/EventListenable.swift
+4-2
Original file line number
Diff line number
Diff line change
@@ -53,15 +53,17 @@ public protocol EventListenable: AnyObject, EventReceiving {
53
53
/**
54
54
Registers an Event Callback for the given `Eventable` Type
55
55
- Author: Simon J. Stuart
56
-
- Version: 3.0.0
56
+
- Version: 5.1.0
57
57
- Parameters:
58
58
- requester: The Object owning the Callback Method
59
59
- callback: The code to invoke for the given `Eventable` Type
60
60
- forEventType: The `Eventable` Type for which to Register the Callback
61
61
- executeOn: Tells the `EventListenable` whether to execute the Callback on the `requester`'s Thread, or the Listener's.
62
+
- interestedIn: Defines the conditions under which the Listener is interested in an Event (anything outside of the given condition will be ignored by this Listener)
63
+
- 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
62
64
- Returns: A `UUID` value representing the `token` associated with this Event Callback
Copy file name to clipboardExpand all lines: Sources/EventDrivenSwift/EventListener/EventListener.swift
+7-2
Original file line number
Diff line number
Diff line change
@@ -17,6 +17,8 @@ import Observable
17
17
- 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)
@@ -31,6 +33,7 @@ open class EventListener: EventHandler, EventListenable {
31
33
vardispatchQueue:DispatchQueue?
32
34
varexecuteOn:ExecuteEventOn=.requesterThread
33
35
varinterestedIn:EventListenerInterest=.all
36
+
varmaximumEventAge:UInt64=0
34
37
}
35
38
36
39
/**
@@ -68,6 +71,8 @@ open class EventListener: EventHandler, EventListenable {
68
71
69
72
if listener.interestedIn ==.latestOnly && event.dispatchTime <latestEventDispatchTime[event.event.getEventTypeName()]! {continue} // If this Listener is only interested in the Latest Event dispatched for this Event Type, and this Event is NOT the Latest... skip it!
70
73
74
+
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
+
71
76
switch listener.executeOn {
72
77
case.requesterThread:
73
78
Task{ // We raise a Task because we don't want the entire Listener blocked in the event the dispatchQueue is busy or blocked!
@@ -86,12 +91,12 @@ open class EventListener: EventHandler, EventListenable {
0 commit comments