Skip to content

Instantly share code, notes, and snippets.

@tilltue
Created December 5, 2021 16:58
Show Gist options
  • Save tilltue/1cef4e78ad7f64c8d970693dde5a3172 to your computer and use it in GitHub Desktop.
Save tilltue/1cef4e78ad7f64c8d970693dde5a3172 to your computer and use it in GitHub Desktop.
class Timer: DataListner {
let timer: DispatchSourceTimer
init(closure: @escaping (UploadStatus<Int>) -> Void) {
self.timer = DispatchSource.makeTimerSource(flags: [], queue: DispatchQueue.main)
timer.schedule(deadline: .now(), repeating: 1)
let progress = Progress()
timer.setEventHandler(handler: {
progress.completedUnitCount += 1
progress.totalUnitCount = 10
if progress.completedUnitCount == progress.totalUnitCount {
closure(.completion(1))
} else {
closure(.progress(progress))
}
})
timer.resume()
}
func remove() {
print("timer remove")
timer.cancel()
}
}
@tilltue
Copy link
Author

tilltue commented Dec 5, 2021

        let publisher = UploadDataStreamPublisher<Int>(handler: {
            return Timer(closure: $0)
        })
        publisher
            .sink {
                print($0)
            }.store(in: &cancellable)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment