Skip to content

Instantly share code, notes, and snippets.

@gscalzo
Created July 8, 2014 15:58
Show Gist options
  • Save gscalzo/fe395e0600ea6347bc54 to your computer and use it in GitHub Desktop.
Save gscalzo/fe395e0600ea6347bc54 to your computer and use it in GitHub Desktop.
Need For Mixins: what I want
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 {
@delegate let dispatcher = ObservableDispatcher()
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