Skip to content

Instantly share code, notes, and snippets.

@nalexn
Last active October 21, 2019 15:16
Show Gist options
  • Save nalexn/a2251c37430c7b6e85d44c753d254d16 to your computer and use it in GitHub Desktop.
Save nalexn/a2251c37430c7b6e85d44c753d254d16 to your computer and use it in GitHub Desktop.
class ValueConsumer {
private var subscriptions = Set<AnyCancellable>()
private let mySubject = CurrentValueSubject<Int, Never>(0)
private var myVar: Int = 0
func consumeValues(publisher1, ...) {
publisher1
.sink { print("New value: \($0)") }
.store(in: &subscriptions)
subscription2 = mySubject
.subscribe(publisher2)
.store(in: &subscriptions)
subscription3 = publisher3
.assign(to: \.myVar, on: self)
.store(in: &subscriptions)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment