Created
December 12, 2015 10:37
-
-
Save Tom-Alexander/0dc871ec9d175ea8bf64 to your computer and use it in GitHub Desktop.
swift pub-sub
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; | |
struct Consumer { | |
var name: String | |
var handler: (Any) -> Void | |
} | |
class EventEmitter { | |
static var consumers: [Consumer] = []; | |
static func publish(name: String, args: Any) { | |
for consumer in self.consumers { | |
if(consumer.name == name) { | |
consumer.handler(args); | |
} | |
} | |
} | |
static func subscribe(name: String, handler: (Any) -> Void) { | |
self.consumers.append( | |
Consumer(name: name, handler: handler) | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment