Skip to content

Instantly share code, notes, and snippets.

@Tom-Alexander
Created December 12, 2015 10:37
Show Gist options
  • Save Tom-Alexander/0dc871ec9d175ea8bf64 to your computer and use it in GitHub Desktop.
Save Tom-Alexander/0dc871ec9d175ea8bf64 to your computer and use it in GitHub Desktop.
swift pub-sub
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