Created
October 24, 2018 09:24
-
-
Save ha1f/a84e981b23002fb09860d5f2936317e7 to your computer and use it in GitHub Desktop.
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
class OperationGroup<T1, T2> { | |
private var item1: T1? | |
private var item2: T2? | |
private var observers: [(T1, T2) -> Void] = [] | |
func notify1(_ item: T1) { | |
item1 = item | |
_notifyIfNeeded() | |
} | |
func notify2(_ item: T2) { | |
item2 = item | |
_notifyIfNeeded() | |
} | |
func subscribe(_ observer: @escaping (T1, T2) -> Void) { | |
observers.append(observer) | |
_notifyIfNeeded() | |
} | |
private func _notifyIfNeeded() { | |
guard let item1 = item1, let item2 = item2 else { | |
return | |
} | |
while let observer = observers.popLast() { | |
observer(item1, item2) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment