-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
154 additions
and
326 deletions.
There are no files selected for viewing
This file contains 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
This file was deleted.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file was deleted.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,42 @@ | ||
import Foundation | ||
|
||
public struct BulkBufferTimer { | ||
public final class BulkBufferTimer { | ||
|
||
private let interval: DispatchTimeInterval | ||
private var interval: Duration | ||
|
||
private let onTimeout: @Sendable () async -> Void | ||
private var onTimeout: (isolated (any Actor)?) async -> Void | ||
private var item: Task<(), Never>? | ||
|
||
public init( | ||
interval: DispatchTimeInterval, | ||
@_inheritActorContext onTimeout: @escaping @Sendable () async -> Void | ||
interval: Duration, | ||
onTimeout: sending @escaping () async -> Void | ||
) { | ||
|
||
self.interval = interval | ||
self.onTimeout = onTimeout | ||
|
||
refresh() | ||
self.onTimeout = { a in | ||
await onTimeout() | ||
} | ||
|
||
} | ||
|
||
public mutating func tap() { | ||
refresh() | ||
public func tap(isolation: isolated (any Actor)? = #isolation) { | ||
refresh(isolation: isolation) | ||
} | ||
|
||
private mutating func refresh() { | ||
private func refresh(isolation: isolated (any Actor)? = #isolation) { | ||
|
||
self.item?.cancel() | ||
|
||
let task = Task { [onTimeout, interval] in | ||
|
||
try? await Task.sleep(nanoseconds: interval.makeNanoseconds()) | ||
try? await Task.sleep(for: interval) | ||
|
||
guard Task.isCancelled == false else { return } | ||
|
||
await onTimeout() | ||
await onTimeout(isolation) | ||
} | ||
|
||
self.item = task | ||
} | ||
|
||
} | ||
|
||
extension DispatchTimeInterval { | ||
|
||
fileprivate func makeNanoseconds() -> UInt64 { | ||
|
||
switch self { | ||
case .nanoseconds(let v): return UInt64(v) | ||
case .microseconds(let v): return UInt64(v) * 1_000 | ||
case .milliseconds(let v): return UInt64(v) * 1_000_000 | ||
case .seconds(let v): return UInt64(v) * 1_000_000_000 | ||
case .never: return UInt64.max | ||
@unknown default: | ||
assertionFailure() | ||
return 0 | ||
} | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.