|
| 1 | +// RUN: %empty-directory(%t) |
| 2 | +// RUN: split-file %s %t |
| 3 | + |
| 4 | +// RUN: %target-build-swift %t/Lib.swift \ |
| 5 | +// RUN: -module-name=Lib -package-name Pkg \ |
| 6 | +// RUN: -parse-as-library -emit-module -emit-module-path %t/Lib.swiftmodule -I%t \ |
| 7 | +// RUN: -enable-default-cmo \ |
| 8 | +// RUN: -O -wmo |
| 9 | + |
| 10 | +// RUN: %target-build-swift %t/Utils.swift \ |
| 11 | +// RUN: -module-name=Utils -package-name Pkg \ |
| 12 | +// RUN: -parse-as-library -emit-module -emit-module-path %t/Utils.swiftmodule -I%t \ |
| 13 | +// RUN: -enable-default-cmo \ |
| 14 | +// RUN: -O -wmo |
| 15 | + |
| 16 | +// RUN: %target-build-swift %t/Client.swift \ |
| 17 | +// RUN: -module-name=Client -package-name Pkg \ |
| 18 | +// RUN: -parse-as-library -emit-module -emit-module-path %t/Client.swiftmodule -I%t \ |
| 19 | +// RUN: -enable-default-cmo \ |
| 20 | +// RUN: -O -wmo |
| 21 | + |
| 22 | + |
| 23 | +// REQUIRES: swift_in_compiler |
| 24 | + |
| 25 | + |
| 26 | +//--- Lib.swift |
| 27 | + |
| 28 | +public protocol ProgressAnimationProtocol { |
| 29 | + func update(step: Int, total: Int, text: String) |
| 30 | + func complete(success: Bool) |
| 31 | +} |
| 32 | + |
| 33 | +//--- Utils.swift |
| 34 | +import Lib |
| 35 | + |
| 36 | +//@_spi(SwiftPMInternal) |
| 37 | +extension ProgressAnimationProtocol { |
| 38 | +// @_spi(SwiftPMInternal) |
| 39 | + public func throttled<C: Clock>( |
| 40 | + now: @escaping () -> C.Instant, |
| 41 | + interval: C.Duration, |
| 42 | + clock: C.Type = C.self |
| 43 | + ) -> some ProgressAnimationProtocol { |
| 44 | + ThrottledProgressAnimation(self, now: now, interval: interval, clock: clock) |
| 45 | + } |
| 46 | + |
| 47 | +// @_spi(SwiftPMInternal) |
| 48 | + public func throttled( |
| 49 | + interval: ContinuousClock.Duration |
| 50 | + ) -> some ProgressAnimationProtocol { |
| 51 | + self.throttled(now: { ContinuousClock().now }, interval: interval, clock: ContinuousClock.self) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +public struct ProgressAnimationArguments: ProgressAnimationProtocol { |
| 56 | + public init() { |
| 57 | + } |
| 58 | + |
| 59 | + public func update(step: Int, total: Int, text: String) { |
| 60 | + } |
| 61 | + |
| 62 | + public func complete(success: Bool) { |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +final class ThrottledProgressAnimation: ProgressAnimationProtocol { |
| 67 | + private let animation: ProgressAnimationProtocol |
| 68 | + |
| 69 | + init<C: Clock>( |
| 70 | + _ animation: ProgressAnimationProtocol, |
| 71 | + now: @escaping () -> C.Instant, interval: C.Duration, clock: C.Type = C.self |
| 72 | + ) { |
| 73 | + self.animation = animation |
| 74 | + } |
| 75 | + |
| 76 | + func update(step: Int, total: Int, text: String) { |
| 77 | + } |
| 78 | + |
| 79 | + func complete(success: Bool) { |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | + |
| 84 | +//--- Client.swift |
| 85 | +import Lib |
| 86 | +import Utils |
| 87 | + |
| 88 | +public func foo() { |
| 89 | + let arg = ProgressAnimationArguments() |
| 90 | + let x = arg.throttled(interval: .nanoseconds(1)) |
| 91 | + x.complete(success: true) |
| 92 | +} |
0 commit comments