-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from Flowduino/better-event-callbacks
Better event callbacks
- Loading branch information
Showing
15 changed files
with
459 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// | ||
// EventMethod.swift | ||
// Copyright (c) 2022, Flowduino | ||
// Authored by Simon J. Stuart on 21st August 2022 | ||
// | ||
// Subject to terms, restrictions, and liability waiver of the MIT License | ||
// | ||
|
||
import Foundation | ||
|
||
public typealias EventMethodTypedEventCallback<TOwner: AnyObject, TEvent: Any> = (_ sender: TOwner, _ event: TEvent, _ priority: EventPriority) -> () | ||
|
||
/** | ||
Any Property wrapped with `EventMethod` will automatically conform to `EventMethodContainer` | ||
- Author: Simon J. Stuart | ||
- Version: 4.1.0 | ||
- Note: This is used to conformity-test decorated `var`s to automatically register Event Listeners | ||
*/ | ||
public protocol EventMethodContainer { | ||
associatedtype TEventType: Eventable | ||
associatedtype TOwner: AnyObject | ||
|
||
var wrappedValue: EventMethodTypedEventCallback<TOwner, TEventType>? { get set } | ||
|
||
mutating func prepare(owner: AnyObject) | ||
} | ||
|
||
/** | ||
Decorate Typed Event Callback Closures as `var` with `@EventMethod<TEventType>` to automatically register them. | ||
- Author: Simon J. Stuart | ||
- Version: 4.1.0 | ||
*/ | ||
@propertyWrapper | ||
public struct EventMethod<TOwner: AnyObject, TEventType: Eventable>: EventMethodContainer { | ||
public var wrappedValue: EventMethodTypedEventCallback<TOwner, TEventType>? | ||
public var executeOn: ExecuteEventOn | ||
|
||
private weak var owner: AnyObject? = nil | ||
|
||
private func callback(event: TEventType, priority: EventPriority) { | ||
if let typedOwner = owner as? TOwner { | ||
wrappedValue?(typedOwner, event, priority) | ||
} | ||
} | ||
|
||
public init(wrappedValue: EventMethodTypedEventCallback<TOwner, TEventType>?, executeOn: ExecuteEventOn = .requesterThread) { | ||
self.wrappedValue = wrappedValue | ||
self.executeOn = executeOn | ||
} | ||
|
||
mutating public func prepare(owner: AnyObject) { | ||
if let typedOwner = owner as? TOwner { | ||
self.owner = owner | ||
TEventType.addListener( | ||
typedOwner, | ||
callback, | ||
executeOn: executeOn) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
Sources/EventDrivenSwift/EventListener/EventListening.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// | ||
// EventListening.swift | ||
// Copyright (c) 2022, Flowduino | ||
// Authored by Simon J. Stuart on 4th August 2022 | ||
// | ||
// Subject to terms, restrictions, and liability waiver of the MIT License | ||
// | ||
|
||
import Foundation | ||
|
||
/** | ||
Apply `EventListening` to any `Class` intent on listening for `Eventable`s to register `@EventMethod`-decorated (immutable) Listeners via Reflection. | ||
- Author: Simon J. Stuart | ||
- Version: 4.1.0 | ||
*/ | ||
public protocol EventListening: AnyObject { | ||
/** | ||
Invoke this method to automatically register any Event Listener callback bearing the `@EventMethod` wrapper. | ||
- Author: Simon J. Stuart | ||
- Version: 4.1.0 | ||
- Note: Any Event Callback implemented this way will be automatically registered for you. | ||
```` | ||
@EventMethod<MyEventThreadType, MyEventType> | ||
private var onMyEvent = { | ||
(self, event: MyEventType, priority: EventPriority) in | ||
/// Do something with `MyEventType` via its `event` reference here | ||
} | ||
```` | ||
*/ | ||
func registerListeners() | ||
} | ||
|
||
/** | ||
Universal implementations to automatically Register and Unregister `@EventMethod`-decorated Event Listener Callbacks using Reflection | ||
- Author: Simon J. Stuart | ||
- Version: 4.1.0 | ||
*/ | ||
public extension EventListening { | ||
func registerListeners() { | ||
let mirror = Mirror(reflecting: self) | ||
for child in mirror.children { | ||
if var child = child.value as? (any EventMethodContainer) { | ||
child.prepare(owner: self) | ||
} | ||
} | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
Sources/EventDrivenSwift/EventListener/Handler/EventListenerHandler.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// | ||
// EventListenerHandler.swift | ||
// Copyright (c) 2022, Flowduino | ||
// Authored by Simon J. Stuart on 28th August 2022 | ||
// | ||
// Subject to terms, restrictions, and liability waiver of the MIT License | ||
// | ||
|
||
import Foundation | ||
|
||
/** | ||
A Handler for an `EventListener` your code has registered. You can use this to revoke your Event Listeners at any time. | ||
- Author: Simon J. Stuart | ||
- Version: 4.1.0 | ||
*/ | ||
public class EventListenerHandler: EventListenerHandling { | ||
/** | ||
`weak` reference to the `EventListenable` against which this Listener is registered. | ||
- Author: Simon J. Stuart | ||
- Version: 4.1.0 | ||
*/ | ||
private weak var eventListenable: EventListenable? | ||
|
||
/** | ||
This is the Token Key assoicated with your Listener | ||
- Author: Simon J. Stuart | ||
- Version: 4.1.0 | ||
*/ | ||
private var token: UUID | ||
|
||
public func remove() { | ||
eventListenable?.removeListener(token) | ||
} | ||
|
||
public init(eventListenable: EventListenable, token: UUID) { | ||
self.eventListenable = eventListenable | ||
self.token = token | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
Sources/EventDrivenSwift/EventListener/Handler/EventListenerHandling.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// | ||
// EventListenerHandling.swift | ||
// Copyright (c) 2022, Flowduino | ||
// Authored by Simon J. Stuart on 28th August 2022 | ||
// | ||
// Subject to terms, restrictions, and liability waiver of the MIT License | ||
// | ||
|
||
import Foundation | ||
|
||
/** | ||
A Handler for an `EventListener` your code has registered. You can use this to revoke your Event Listeners at any time. | ||
- Author: Simon J. Stuart | ||
- Version: 4.1.0 | ||
*/ | ||
public protocol EventListenerHandling: AnyObject { | ||
/** | ||
Removes your Event Listener | ||
*/ | ||
func remove() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.