Skip to content

Instantly share code, notes, and snippets.

@gscalzo
Last active August 29, 2015 14:03
Show Gist options
  • Save gscalzo/28213d731981fea958ba to your computer and use it in GitHub Desktop.
Save gscalzo/28213d731981fea958ba to your computer and use it in GitHub Desktop.
Need for Mixins: what I have
protocol Observable {
func observe(onUpdate: (Void) -> Void) -> String
func unobserve(key: String)
}
class ObservableDispatcher {
var observerClosures: Dictionary<String, (Void) -> Void> = Dictionary<String, (Void) -> Void>()
func observe(onUpdate: (Void) -> Void) -> String {
let key = "key_\(observerClosures.count+1)_key"
observerClosures[key] = onUpdate
return key
}
func unobserve(key: String) {
observerClosures.removeValueForKey(key)
}
func update() {
for closure in self.observerClosures.values {
closure()
}
}
}
class HDSViewModel: Observable {
var observerClosures: Dictionary<String, (Void) -> Void> = Dictionary<String, (Void) -> Void>()
let dispatcher = ObservableDispatcher()
func observe(onUpdate: (Void) -> Void) -> String {
return dispatcher.observe(onUpdate)
}
func unobserve(key: String) {
dispatcher.unobserve(key)
}
func doSometing() {
// Do Something
self.dispatcher.update()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment