Created
June 14, 2022 00:17
-
-
Save skywalkerlw/19d7f250659f14ce61ac195b610a39eb 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 Counter { | |
let publisher = PassthroughSubject<Int, Never>() | |
private(set) var value = 0 { | |
didSet { | |
publisher.send(value) | |
} | |
} | |
func increment() { | |
value += 1 | |
} | |
} | |
let counter = Counter() | |
let cancellable = counter.publisher | |
.filter { $0 > 2} | |
.sink { value in | |
print(value) | |
} | |
counter.increment() | |
counter.increment() | |
counter.increment() | |
counter.increment() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment