Created
December 17, 2015 16:35
-
-
Save paulofaria/68ffe9d5cd8d883fe68e to your computer and use it in GitHub Desktop.
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
public class EventListener<T> { | |
public typealias Listener = T -> () | |
private let listener: Listener | |
private var calls: Int | |
private var active = true | |
internal init(calls: Int, listener: Listener) { | |
self.calls = calls | |
self.listener = listener | |
} | |
public func call(event: T) -> Bool { | |
calls -= 1 | |
if calls == 0 { active = false } | |
listener(event) | |
return active | |
} | |
public func stop() { | |
active = false | |
} | |
} | |
public class EventEmitter<T> { | |
private var listeners: [EventListener<T>] = [] | |
public func listen(times times: Int = -1, listener: EventListener<T>.Listener) -> EventListener<T> { | |
let listener = EventListener<T>(calls: times, listener: listener) | |
listeners.append(listener) | |
return listener | |
} | |
public func send(event: T) { | |
listeners = listeners.filter({ $0.active }) | |
listeners.forEach({ $0.call(event) }) | |
} | |
} | |
// ------------------------------ | |
public protocol BinaryEventEmitter { | |
var binaryEmitter = EventEmitter<[Int8]>() { get } | |
func onBinary(listener: [Int8] -> Void) -> EventListener<[Int8]> | |
func sendBinary(binary: [Int8]) | |
} | |
extension BinaryEventEmitter { | |
func onBinary(listener: [Int8] -> Void) -> EventListener<[Int8]> { | |
return binaryEmitter.listen(listener: listener) | |
} | |
func sendBinary(binary: [Int8]) { | |
binaryEmitter.send(binary) | |
} | |
} | |
public protocol TextEventEmitter { | |
private let textEmitter = EventEmitter<String>() | |
func onText(listener: String -> Void) -> EventListener<String> | |
func sendText(text: String) | |
} | |
extension TextEventEmitter { | |
func onText(listener: String -> Void) -> EventListener<String> { | |
return textEmitter.listen(listener: listener) | |
} | |
func sendText(text: String) { | |
textEmitter.send(text) | |
} | |
} | |
public protocol PingEventEmitter { ... } | |
public protocol PongEventEmitter { ... } | |
public protocol CloseEventEmitter { ... } | |
.... | |
// ---------------- | |
public class WebSocket: BinaryEventEmitter, TextEventEmitter, PingEventEmitter, PongEventEmitter, CloseEventEmitter { | |
// MARK: - EventEmitter | |
private let binaryEmitter = EventEmitter<[Int8]>() | |
private let textEmitter = EventEmitter<String>() | |
private let pingEmitter = EventEmitter<[Int8]>() | |
private let pongEmitter = EventEmitter<[Int8]>() | |
private let closeEmitter = EventEmitter<(Int, String?)>() | |
} | |
// ------------------------- | |
let ws = WebSocket() | |
let listener = ws.onText { text in | |
print("\(text)") | |
} | |
ws.onClose { code, reason in | |
print("closed with code \(code)") | |
} | |
ws.sendText("string") | |
ws.sendText("string") | |
listener.stop() | |
ws.sendText("string") | |
ws.sendClose((1000, nil)) | |
ws.sendClose((1000, nil)) | |
func doSomethingWithBinaryEventEmitter(binaryEventEmitter: BinaryEventEmitter) { | |
binaryEventEmitter.onBinary { binary in | |
} | |
binaryEventEmitter.sendBinary([0, 1, 2]) | |
} | |
doSomethingWithBinaryEventEmitter(ws) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment