-
Notifications
You must be signed in to change notification settings - Fork 0
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
6 changed files
with
80 additions
and
11 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// | ||
// DispatchQueue.swift | ||
// Song Rating | ||
// | ||
// Created by Cirno MainasuK on 2019-10-23. | ||
// Copyright © 2019 Cirno MainasuK. All rights reserved. | ||
// | ||
|
||
import Cocoa | ||
|
||
public extension DispatchQueue { | ||
|
||
private static var _onceTracker = [String]() | ||
|
||
/** | ||
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() | ||
} | ||
|
||
} |
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