mirror of
https://github.com/ProxymanApp/atlantis.git
synced 2026-05-20 20:20:35 +00:00
34 lines
1.0 KiB
Swift
34 lines
1.0 KiB
Swift
//
|
|
// DispatchQueue+Once.swift
|
|
// atlantis
|
|
//
|
|
// Created by Nghia Tran on 10/22/20.
|
|
// Copyright © 2020 Proxyman. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
extension DispatchQueue {
|
|
private static var _onceTracker = [String]()
|
|
|
|
class func once(file: String = #file, function: String = #function, line: Int = #line, block: ()->Void) {
|
|
let token = file + ":" + function + ":" + String(line)
|
|
once(token: token, block: block)
|
|
}
|
|
/**
|
|
Executes a block of code, associated with a unique token, only once. The code is thread safe and will
|
|
only execute the code once even in the presence of multithreaded calls.
|
|
- parameter token: A unique reverse DNS style name such as com.vectorform.<name> or a GUID
|
|
- parameter block: Block to execute once
|
|
*/
|
|
class func once(token: String, block: () -> Void) {
|
|
objc_sync_enter(self)
|
|
defer { objc_sync_exit(self) }
|
|
if _onceTracker.contains(token) {
|
|
return
|
|
}
|
|
_onceTracker.append(token)
|
|
block()
|
|
}
|
|
}
|