Created
July 12, 2019 14:17
-
-
Save kumamotone/39cef281899a85a655a2d1547bf6a4ca to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import Foundation | |
import Combine | |
struct MyPublisher: Publisher { | |
typealias Output = Int | |
typealias Failure = Error | |
func receive<S>(subscriber: S) where S : Subscriber, | |
Failure == S.Failure, | |
Output == S.Input { | |
subscriber.receive(1) | |
print("called 1") | |
subscriber.receive(2) | |
print("called 2") | |
subscriber.receive(completion: .finished) | |
print("called finish") | |
} | |
} | |
MyPublisher() | |
.receive(on: RunLoop.main) | |
// .throttle(for: .milliseconds(1000), scheduler: RunLoop.main, latest: false) | |
// .debounce(for: .milliseconds(1000), scheduler: RunLoop.main) | |
// .delay(for: .milliseconds(1000), scheduler: DispatchQueue.main) | |
.print() | |
.sink(receiveCompletion: { completion in | |
switch completion { | |
case .finished: | |
print("finished") | |
case .failure(let error): | |
print("error:\(error)") | |
} | |
}, receiveValue: { num in | |
print("\(num)") | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment