Skip to content

Commit 6f2eb86

Browse files
committed
Inline Pass-throughs for Central Event Dispatcher
I've added Inline Pass-Through methods/properties for the Central Event Dispatcher, eliminating the need to do `EventCentral.shared.<method>` you can now just do `EventCentral.<method>` instead.
1 parent 0dceb16 commit 6f2eb86

File tree

3 files changed

+106
-4
lines changed

3 files changed

+106
-4
lines changed

Sources/EventDrivenSwift/Central/EventCentral.swift

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,63 @@
99
import Foundation
1010

1111
/**
12-
Singleton for the Central Event Handler.
12+
Singleton for the Central Event Dispatcher.
1313
- Author: Simon J. Stuart
1414
- Version: 1.0.0
1515
- Note: This is a Singleton!
1616
- Note: This is used when invoking the `queue` and `stack` methods of `Eventable`.
1717
*/
18-
final public class EventCentral: EventDispatcher {
19-
private static var _shared: EventDispatcher = EventCentral()
18+
final public class EventCentral: EventDispatcher, EventCentralable {
19+
/**
20+
Singleton Instance of our Central Event Dispatcher
21+
- Author: Simon J. Stuart
22+
- Version: 1.0.0
23+
*/
24+
private static var _shared: EventDispatchable = EventCentral()
2025

26+
/**
27+
Returns the Central Event Dispatcher
28+
- Author: Simon J. Stuart
29+
- Version: 1.0.0
30+
*/
2131
public static var shared: EventDispatchable {
2232
get {
2333
return _shared
2434
}
2535
}
2636

37+
public static subscript() -> EventDispatchable {
38+
get {
39+
return _shared
40+
}
41+
}
42+
43+
/// This just makes it so that your code cannot initialise instances of `EventCentral`. It's a Singleton!
2744
override private init() {}
45+
46+
@inline(__always) public static func addListener(_ listener: EventReceivable, forEventType: Eventable.Type) {
47+
_shared.addListener(listener, forEventType: forEventType)
48+
}
49+
50+
@inline(__always) public static func removeListener(_ listener: EventReceivable, forEventType: Eventable.Type) {
51+
_shared.removeListener(listener, forEventType: forEventType)
52+
}
53+
54+
@inline(__always) public static func removeListener(_ listener: EventReceivable) {
55+
_shared.removeListener(listener)
56+
}
57+
58+
@inline(__always) public static func queueEvent(_ event: Eventable, priority: EventPriority) {
59+
_shared.queueEvent(event, priority: priority)
60+
}
61+
62+
@inline(__always) public static func stackEvent(_ event: Eventable, priority: EventPriority) {
63+
_shared.stackEvent(event, priority: priority)
64+
}
65+
66+
@inline(__always) public static var eventCount: Int {
67+
get {
68+
return _shared.eventCount
69+
}
70+
}
2871
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
//
2+
// EventCentralable.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+
public protocol EventCentralable {
12+
/**
13+
Registers the given `listener` for the given `Eventable` Type with the Central Event Dispatcher
14+
- Author: Simon J. Stuart
15+
- Version: 1.0.0
16+
*/
17+
static func addListener(_ listener: any EventReceivable, forEventType: Eventable.Type)
18+
19+
/**
20+
Unregisters the given `listener` from the given `Eventable` Type for the Central Event Dispatcher
21+
- Author: Simon J. Stuart
22+
- Version: 1.0.0
23+
*/
24+
static func removeListener(_ listener: any EventReceivable, forEventType: Eventable.Type)
25+
26+
/**
27+
Unregisters the given `listener` from all `Eventable` Types from the Central Event Dispatcher
28+
- Author: Simon J. Stuart
29+
- Version: 1.0.0
30+
*/
31+
static func removeListener(_ listener: any EventReceivable)
32+
33+
/**
34+
Adds the given `event` to the Central Event Queue with the given `priority`
35+
- Author: Simon J. Stuart
36+
- Version: 1.0.0
37+
- Parameters:
38+
- event: The Event
39+
- priority: The Priority of the Event
40+
*/
41+
static func queueEvent(_ event: any Eventable, priority: EventPriority)
42+
43+
/**
44+
Adds the given `event` to the Central Event Stack with the given `priority`
45+
- Author: Simon J. Stuart
46+
- Version: 1.0.0
47+
- Parameters:
48+
- event: The Event
49+
- priority: The Priority of the Event
50+
*/
51+
static func stackEvent(_ event: any Eventable, priority: EventPriority)
52+
53+
/**
54+
The number of Events currently pending in the Queue and Stack (combined) of the Central Event Dispatcher
55+
- Author: Simon J. Stuart
56+
- Version: 1.0.0
57+
- Returns: The number of Events currently pending in the Queue and Stack combined
58+
*/
59+
static var eventCount: Int { get }
60+
}

Tests/EventDrivenSwiftTests/BasicEventReceiverTests.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ final class BasicEventReceiverTests: XCTestCase {
5252
let eventThread = TestEventThread() // Create the Thread
5353

5454
XCTAssertEqual(eventThread.foo, 0, "Expect initial value of eventThread.foo to be 0, but it's \(eventThread.foo)")
55-
5655
EventCentral.shared.queueEvent(testOne, priority: .normal) // Now let's dispatch our Event to change this value
5756

5857
let result = eventThread.awaiter.wait(timeout: DispatchTime.now().advanced(by: DispatchTimeInterval.seconds(10)))

0 commit comments

Comments
 (0)