Created
August 5, 2017 22:53
-
-
Save nesteruk/37f2a8de16a40378f1d2123866635edb to your computer and use it in GitHub Desktop.
Swift mediator exercise
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
import Foundation | |
import XCTest | |
public protocol Disposable | |
{ | |
func dispose() | |
} | |
protocol Invocable : class | |
{ | |
func invoke(_ data: Any) | |
} | |
public class Event<T> | |
{ | |
public typealias EventHandler = (T) -> () | |
var eventHandlers = [Invocable]() | |
public func raise(_ data: T) | |
{ | |
for handler in self.eventHandlers | |
{ | |
handler.invoke(data) | |
} | |
} | |
public func addHandler<U: AnyObject> | |
(target: U, handler: @escaping (U) -> EventHandler) -> Disposable | |
{ | |
let subscription = Subscription(target: target, handler: handler, event: self) | |
eventHandlers.append(subscription) | |
return subscription | |
} | |
} | |
class Subscription<T: AnyObject, U> : Invocable, Disposable | |
{ | |
weak var target: T? | |
let handler: (T) -> (U) -> () | |
let event: Event<U> | |
init(target: T?, handler: @escaping (T) -> (U) -> (), event: Event<U>) | |
{ | |
self.target = target | |
self.handler = handler | |
self.event = event | |
} | |
func invoke(_ data: Any) { | |
if let t = target { | |
handler(t)(data as! U) | |
} | |
} | |
func dispose() | |
{ | |
event.eventHandlers = event.eventHandlers.filter { $0 as AnyObject? !== self } | |
} | |
} | |
class Participant | |
{ | |
private let mediator: Mediator | |
var value = 0 | |
init(mediator: Mediator) | |
{ | |
self.mediator = mediator | |
mediator.alert.addHandler(target: self, | |
handler: self.alert) // NOT WORKING!!! | |
} | |
func alert(_ sender: AnyObject, _ n: Int) | |
{ | |
if (sender !== self) | |
{ | |
value += n | |
} | |
} | |
func say(_ n: Int) | |
{ | |
mediator.broadcast(self, n) | |
} | |
} | |
class Mediator | |
{ | |
let alert = Event<(AnyObject, Int)>() | |
func broadcast(_ sender: AnyObject, _ n: Int) | |
{ | |
alert.raise(sender, n) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment