The simplest way would be to use the handleEvents
methods to manage any event through the pipeline. Although not the only one and more cool.
You could use two subscriptions to separate your logic. Besides for the first case where you don't want to have a delay you could take only the first value with first()
or prefix(1)
.
I share an example with you:
import Foundation
import Combine
let subject = PassthroughSubject<Void, Never>()
// The shared one is optional, depending on the case.
For heavy publishers which for example do network requests is a good practice.
let sharedSubject = subject.share()
let normalCancellable = sharedSubject.first()
.sink { print("Normal") }
let debouncedCancellable = sharedSubject
.debounce(for: .seconds(0.3), scheduler: RunLoop.main)
.sink { print("Debounced") }
subject.send()
subject.send()
subject.send()
Output
receive subscription: (PassthroughSubject)
request unlimited
receive value: (())
Normal
receive value: (())
receive value: (())
Debounced