Skip to content

Instantly share code, notes, and snippets.

@laevandus
Last active October 2, 2022 00:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save laevandus/f39c4d42eb52a88ac0bc90ee14f53db9 to your computer and use it in GitHub Desktop.
Save laevandus/f39c4d42eb52a88ac0bc90ee14f53db9 to your computer and use it in GitHub Desktop.
@Published var state1 = "0"
@Published var state2 = "a"
func mergeExample() {
$state1.merge(with: $state2)
.sink { value in
print("sink", value)
}
.store(in: &cancellables)
print("will change state1 to 1")
state1 = "1"
print("will change state1 to 2")
state1 = "2"
print("will change state2 to b")
state2 = "b"
print("will change state1 to 3")
state1 = "3"
print("will change state2 to c")
state2 = "c"
}
/* output:
sink: 0
sink: a
will change state1 to 1
sink: 1
will change state1 to
sink: 2
will change state2 to b
sink: b
will change state1 to 3
sink: 3
will change state2 to c
sink: c
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment