杨锴
2025-03-11 90dc3329d1973fda691e357cf4523d5c7c67fa1d
Pods/SwifterSwift/Sources/SwifterSwift/Dispatch/DispatchQueueExtensions.swift
@@ -6,10 +6,11 @@
// MARK: - Properties
public extension DispatchQueue {
    #if !os(Linux)
    /// SwifterSwift: A Boolean value indicating whether the current dispatch queue is the main queue.
    static var isMainQueue: Bool {
        enum Static {
            static var key: DispatchSpecificKey<Void> = {
            static let key: DispatchSpecificKey<Void> = {
                let key = DispatchSpecificKey<Void>()
                DispatchQueue.main.setSpecific(key: key, value: ())
                return key
@@ -17,6 +18,7 @@
        }
        return DispatchQueue.getSpecific(key: Static.key) != nil
    }
    #endif
}
// MARK: - Methods
@@ -45,16 +47,18 @@
    func asyncAfter(delay: Double,
                    qos: DispatchQoS = .unspecified,
                    flags: DispatchWorkItemFlags = [],
                    execute work: @escaping () -> Void) {
                    execute work: @Sendable @escaping () -> Void) {
        asyncAfter(deadline: .now() + delay, qos: qos, flags: flags, execute: work)
    }
    #if !os(Linux)
    @available(macOS 14.0, iOS 17.0, tvOS 17.0, watchOS 10.0, *)
    func debounce(delay: Double, action: @escaping () -> Void) -> () -> Void {
        // http://stackoverflow.com/questions/27116684/how-can-i-debounce-a-method-call
        var lastFireTime = DispatchTime.now()
        let deadline = { lastFireTime + delay }
        return {
            self.asyncAfter(deadline: deadline()) {
            self.asyncAfterUnsafe(deadline: deadline()) {
                let now = DispatchTime.now()
                if now >= deadline() {
                    lastFireTime = now
@@ -63,6 +67,7 @@
            }
        }
    }
    #endif
}
#endif