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
- Implemented a decorator method called `callTypedEventCallback` to type-test and cast an `Eventable` to an explicit Event Type
- Updated README.MD to reflect the above change.
Before we dig into the implementation of `processTemperatureEvent`, which can basically do whatever we would want to do with the data provided in the `TemperatureEvent`, let's take a moment to understand what is happening in the above code.
207
+
Before we dig into the implementation of `onTemperatureEvent`, which can basically do whatever we would want to do with the data provided in the `TemperatureEvent`, let's take a moment to understand what is happening in the above code.
214
208
215
209
Firstly, `TemperatureProcessor` inherits from `EventReceiver`, which is where all of the magic happens to receive *Events* and register our *Listeners* (or *Callbacks* or *Handlers*).
216
210
217
211
The function `registerEventListeners` will be called automatically when an instance of `TemperatureProcessor` is created. Within this method, we cal `addEventCallback` to register `onTemperatureEvent` so that it will be invoked every time an *Event* of type `TemperatureEvent` is *Dispatched*.
218
212
219
-
Our *Callback* (or *Handler* or *Listener Event*) is called `onTemperatureEvent`, and all this does is first check that the `event` received is of the type `TemperatureEvent`, and - if it is - passes the now-typed `TemperatureEvent` along to `processTemperatureEvent`, which is where we will implement whatever *Operation* is to be performed against a `TemperatureEvent`.
213
+
Notice that we use a *Closure* which invokes `self.callTypedEventCallback`. This is to address a fundamental limitation of Generics in the Swift language, and acts as a decorator to perform the Type Checking and Casting of the received `event` to the explicit *Event* type we expect. In this case, that is `TemperatureEvent`
214
+
215
+
Our *Callback* (or *Handler* or *Listener Event*) is called `onTemperatureEvent`, which is where we will implement whatever *Operation* is to be performed against a `TemperatureEvent`.
220
216
221
217
**Note**: The need to provide type checking and casting (in `onTemperatureEvent`) is intended to be a temporary requirement. We are looking at ways to decorate this internally within the library, so that we can reduce the amount of boilerplate code you have to produce in your implementations.
222
218
For the moment, this solution works well, and enables you to begin using `EventDrivenSwift` in your applications immediately.
223
219
224
-
Now, let's actually do something with our `TemperatureEvent` in the `processTemperatureEvent` method.
220
+
Now, let's actually do something with our `TemperatureEvent` in the `onTemperatureEvent` method.
225
221
```swift
226
222
/// An Enum to map a Temperature value onto a Rating
227
223
enumTemperatureRating: String{
@@ -253,13 +249,13 @@ Now, let's actually do something with our `TemperatureEvent` in the `processTemp
The above code is intended to be illustrative, rather than *useful*. Our `processTemperatureEvent` passes *Event*'s encapsulated `temperatureInCelsius` to a public variable (which could then be read by other code as necessary) as part of our `EventReceiver`, and also pre-calculates a `TemperatureRating` based on the Temperature value received in the *Event*.
258
+
The above code is intended to be illustrative, rather than *useful*. Our `onTemperatureEvent` passes *Event*'s encapsulated `temperatureInCelsius` to a public variable (which could then be read by other code as necessary) as part of our `EventReceiver`, and also pre-calculates a `TemperatureRating` based on the Temperature value received in the *Event*.
263
259
264
260
Ultimately, your code can do whatever you wish with the *Event*'s *Payload* data!
Map of `Eventable` qualified Type Names against `EventCallback` methods.
30
+
- Author: Simon J. Stuart
31
+
- Version: 1.0.0
32
+
- Note: We use the Qualified Type Name as the Key because Types are not Hashable in Swift
33
+
*/
16
34
@ThreadSafeSemaphoreprivatevareventCallbacks=[String:EventCallback]() //TODO: Make this a Revolving Door collection!
17
35
36
+
// @ThreadSafeSemaphore private var typedEventCallbacks = [String:Any]() //TODO: Find an implementation that works for strong-typed Event Callbacks (P.S. limitations of Swift Generics are very annoying!)
37
+
18
38
/**
19
39
Invoke the appropriate Callback for the given Event
20
40
- Author: Simon J. Stuart
@@ -23,16 +43,24 @@ public class EventReceiver: EventHandler, EventReceivable {
0 commit comments