1 // 2 // Dispatch.swift 3 // BareExpo 4 // 5 // Created by the Expo team on 5/27/20. 6 // Copyright © 2020 Expo. All rights reserved. 7 // 8 9 import Foundation 10 11 public class Dispatch 12 { 13 private static var _onceTracker = [String]() 14 15 public class func once(file: String = #file, 16 function: String = #function, 17 line: Int = #line, 18 block: () -> Void) { 19 let token = "\(file):\(function):\(line)" 20 once(token: token, block: block) 21 } 22 23 /** 24 Executes a block of code, associated with a unique token, only once. The code is thread safe and will 25 only execute the code once even in the presence of multithreaded calls. 26 27 - parameter token: A unique reverse DNS style name such as com.vectorform.<name> or a GUID 28 - parameter block: Block to execute once 29 */ 30 public class func once(token: String, 31 block: () -> Void) { 32 objc_sync_enter(self) 33 defer { objc_sync_exit(self) } 34 35 guard !_onceTracker.contains(token) else { return } 36 37 _onceTracker.append(token) 38 block() 39 } 40 } 41