Last active
August 29, 2015 14:03
-
-
Save gscalzo/28213d731981fea958ba to your computer and use it in GitHub Desktop.
Need for Mixins: what I have
This file contains hidden or 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
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