Skip to content

Commit d1ea187

Browse files
committed
Serious refactoring...
- Stubbed `EventPooling` protocol - Stubbed `EventPool` class - Copied implementation of current `EventReciever` class to new `EventThread` class, which inherits from `EventThreadable` protocol About to make `EventReceiver` into a common Base Class, because it will be used by both `EventThread` and `EventPool` alike.
1 parent 426bc9b commit d1ea187

File tree

4 files changed

+169
-0
lines changed

4 files changed

+169
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//
2+
// EventPool.swift
3+
// Copyright (c) 2022, Flowduino
4+
// Authored by Simon J. Stuart on 13th August 2022
5+
//
6+
// Subject to terms, restrictions, and liability waiver of the MIT License
7+
//
8+
9+
import Foundation
10+
11+
/**
12+
Concrete Implementation for an `EventPool`.
13+
- Author: Simon J. Stuart
14+
- Version: 3.1.0
15+
*/
16+
open class EventPool<TEventReceiver: EventReceivable>: EventHandler, EventPooling {
17+
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//
2+
// EventPooling.swift
3+
// Copyright (c) 2022, Flowduino
4+
// Authored by Simon J. Stuart on 13th August 2022
5+
//
6+
// Subject to terms, restrictions, and liability waiver of the MIT License
7+
//
8+
9+
import Foundation
10+
11+
/**
12+
Protocol describing anything that Pools `EventReceivable`s
13+
- Author: Simon J. Stuart
14+
- Version: 3.1.0
15+
*/
16+
public protocol EventPooling: AnyObject, EventReceivable {
17+
18+
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
//
2+
// EventThread.swift
3+
// Copyright (c) 2022, Flowduino
4+
// Authored by Simon J. Stuart on 4th August 2022
5+
//
6+
// Subject to terms, restrictions, and liability waiver of the MIT License
7+
//
8+
9+
import Foundation
10+
import ThreadSafeSwift
11+
import Observable
12+
13+
/**
14+
Abstract Base Type for all `EventThread` Thread Types.
15+
- Author: Simon J. Stuart
16+
- Version: 1.0.0
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)
18+
*/
19+
open class EventThread: EventHandler, EventThreadable {
20+
/**
21+
Map of `Eventable` qualified Type Names against `EventCallback` methods.
22+
- Author: Simon J. Stuart
23+
- Version: 1.0.0
24+
- Note: We use the Qualified Type Name as the Key because Types are not Hashable in Swift
25+
*/
26+
@ThreadSafeSemaphore private var eventCallbacks = [String:EventCallback]()
27+
28+
/**
29+
Invoke the appropriate Callback for the given Event
30+
- Author: Simon J. Stuart
31+
- Version: 1.0.0
32+
*/
33+
override internal func processEvent(_ event: any Eventable, dispatchMethod: EventDispatchMethod, priority: EventPriority) {
34+
let eventTypeName = String(reflecting: type(of: event))
35+
var callback: EventCallback? = nil
36+
37+
_eventCallbacks.withLock { eventCallbacks in
38+
callback = eventCallbacks[eventTypeName]
39+
}
40+
41+
if callback == nil { return } // If there is no Callback, we will just return!
42+
43+
callback!(event, priority)
44+
}
45+
46+
/**
47+
Registers an Event Callback for the given `Eventable` Type
48+
- Author: Simon J. Stuart
49+
- Version: 2.1.0
50+
- Parameters:
51+
- callback: The code to invoke for the given `Eventable` Type
52+
- forEventType: The `Eventable` Type for which to Register the Callback
53+
*/
54+
internal func addEventCallback<TEvent: Eventable>(_ callback: @escaping TypedEventCallback<TEvent>, forEventType: Eventable.Type) {
55+
let eventTypeName = String(reflecting: forEventType)
56+
57+
_eventCallbacks.withLock { eventCallbacks in
58+
eventCallbacks[eventTypeName] = { event, priority in
59+
self.callTypedEventCallback(callback, forEvent: event, priority: priority)
60+
}
61+
}
62+
63+
/// We automatically register the Receiver with the Central Event Dispatcher
64+
EventCentral.shared.addReceiver(self, forEventType: forEventType)
65+
}
66+
67+
/**
68+
Performs a Transparent Type Test, Type Cast, and Method Call via the `callback` Closure.
69+
- Author: Simon J. Stuart
70+
- Version: 2.1.0
71+
- Parameters:
72+
- callback: The code (Closure or Callback Method) to execute for the given `forEvent`, typed generically using `TEvent`
73+
- forEvent: The instance of the `Eventable` type to be processed
74+
- priority: The `EventPriority` with which the `forEvent` was dispatched
75+
*/
76+
internal func callTypedEventCallback<TEvent: Eventable>(_ callback: @escaping TypedEventCallback<TEvent>, forEvent: Eventable, priority: EventPriority) {
77+
if let typedEvent = forEvent as? TEvent {
78+
callback(typedEvent, priority)
79+
}
80+
}
81+
82+
/**
83+
Removes an Event Callback for the given `Eventable` Type
84+
- Author: Simon J. Stuart
85+
- Version: 1.0.0
86+
- Parameters:
87+
- forEventType: The `Eventable` Type for which to Remove the Callback
88+
*/
89+
internal func removeEventCallback(forEventType: any Eventable) {
90+
let eventTypeName = String(reflecting: forEventType)
91+
92+
_eventCallbacks.withLock { eventCallbacks in
93+
eventCallbacks.removeValue(forKey: eventTypeName)
94+
}
95+
}
96+
97+
/**
98+
Override this to register your Event Listeners/Callbacks
99+
- Author: Simon J. Stuart
100+
- Version: 1.0.0
101+
*/
102+
internal func registerEventListeners() {
103+
// No default implementation
104+
}
105+
106+
/**
107+
Initializes an `EventReciever` decendant and invokes `registerEventListeners()` to register your Event Listeners/Callbacks within your `EventThread` type.
108+
- Author: Simon J. Stuart
109+
- Version: 1.0.0
110+
*/
111+
public override init() {
112+
super.init()
113+
registerEventListeners()
114+
}
115+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//
2+
// EventThreadable.swift
3+
// Copyright (c) 2022, Flowduino
4+
// Authored by Simon J. Stuart on 4th August 2022
5+
//
6+
// Subject to terms, restrictions, and liability waiver of the MIT License
7+
//
8+
9+
import Foundation
10+
11+
/**
12+
Protocol describing Threads that are Event-Driven
13+
- Author: Simon J. Stuart
14+
- Version: 3.1.0
15+
*/
16+
public protocol EventThreadable: AnyObject, EventReceivable {
17+
18+
}

0 commit comments

Comments
 (0)