|
| 1 | +// |
| 2 | +// AsyncSequences+Timer.swift |
| 3 | +// |
| 4 | +// |
| 5 | +// Created by Thibault Wittemberg on 04/03/2022. |
| 6 | +// |
| 7 | + |
| 8 | +import Foundation |
| 9 | + |
| 10 | +public extension AsyncSequences { |
| 11 | + typealias Timer = AsyncTimerSequence |
| 12 | +} |
| 13 | + |
| 14 | +/// `AsyncTimerSequence`is an async sequence that repeatedly emits the current date on the given interval, with the given priority. |
| 15 | +public class AsyncTimerSequence: AsyncSequence { |
| 16 | + public typealias Element = Date |
| 17 | + public typealias AsyncIterator = AsyncThrowingStream<Date, Error>.AsyncIterator |
| 18 | + |
| 19 | + let priority: TaskPriority? |
| 20 | + let interval: AsyncSequences.Interval |
| 21 | + var task: Task<Void, Error>? |
| 22 | + |
| 23 | + /// Created an async sequence that repeatedly emits the current date on the given interval, with the given priority. |
| 24 | + /// |
| 25 | + /// ``` |
| 26 | + /// let timer = AsyncSequences.Timer(priority: .high, every: .seconds(1)) |
| 27 | + /// |
| 28 | + /// Task { |
| 29 | + /// for try await element in timer { |
| 30 | + /// print(element) |
| 31 | + /// } |
| 32 | + /// } |
| 33 | + /// |
| 34 | + /// // will print: |
| 35 | + /// // 2022-03-06 19:31:22 +0000 |
| 36 | + /// // 2022-03-06 19:31:23 +0000 |
| 37 | + /// // 2022-03-06 19:31:24 +0000 |
| 38 | + /// // 2022-03-06 19:31:25 +0000 |
| 39 | + /// // 2022-03-06 19:31:26 +0000 |
| 40 | + /// // and will stop once timer.cancel() is called or the parent task is cancelled. |
| 41 | + /// ``` |
| 42 | + /// |
| 43 | + /// - Parameters: |
| 44 | + /// - priority: The priority of the inderlying Task. Nil by default. |
| 45 | + /// - interval: The time interval on which to publish events. For example, a value of `0.5` publishes an event approximately every half-second. |
| 46 | + /// - Returns: An async sequence that repeatedly emits the current date on the given interval, with the given priority. |
| 47 | + public init(priority: TaskPriority? = nil, every interval: AsyncSequences.Interval) { |
| 48 | + self.priority = priority |
| 49 | + self.interval = interval |
| 50 | + } |
| 51 | + |
| 52 | + func makeStream() -> AsyncThrowingStream<Date, Error> { |
| 53 | + AsyncThrowingStream<Date, Error>(Date.self, bufferingPolicy: .unbounded) { [weak self] continuation in |
| 54 | + let interval = self?.interval ?? .immediate |
| 55 | + self?.task = Task(priority: self?.priority) { |
| 56 | + do { |
| 57 | + while !Task.isCancelled { |
| 58 | + try await Task.sleep(nanoseconds: interval.value) |
| 59 | + continuation.yield(Date()) |
| 60 | + } |
| 61 | + } catch is CancellationError { |
| 62 | + continuation.finish() |
| 63 | + } catch { |
| 64 | + throw error |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + /// Cancels the timer. |
| 71 | + public func cancel() { |
| 72 | + self.task?.cancel() |
| 73 | + } |
| 74 | + |
| 75 | + public func makeAsyncIterator() -> AsyncIterator { |
| 76 | + self.makeStream().makeAsyncIterator() |
| 77 | + } |
| 78 | +} |
0 commit comments