-
Notifications
You must be signed in to change notification settings - Fork 3
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
1 parent
2bb51d3
commit 1f6ff8a
Showing
1 changed file
with
64 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// | ||
// RNIIdentifiable.swift | ||
// react-native-ios-modal | ||
// | ||
// Created by Dominic Go on 3/31/23. | ||
// | ||
|
||
import Foundation | ||
|
||
fileprivate final class RNIObjectIdentifier { | ||
static var counterID = -1; | ||
|
||
let id: Int = { | ||
RNIObjectIdentifier.counterID += 1; | ||
return RNIObjectIdentifier.counterID; | ||
}(); | ||
|
||
let uuid = UUID(); | ||
}; | ||
|
||
fileprivate final class RNIObjectIdentifierMap { | ||
static let map = NSMapTable<AnyObject, RNIObjectIdentifier>( | ||
keyOptions: .weakMemory, | ||
valueOptions: .strongMemory | ||
); | ||
}; | ||
|
||
|
||
public protocol RNIIdentifiable: AnyObject { | ||
|
||
static var synthesizedIdPrefix: String { set get }; | ||
|
||
var synthesizedID: Int { get }; | ||
|
||
var synthesizedUUID: UUID { get }; | ||
|
||
}; | ||
|
||
extension RNIIdentifiable { | ||
fileprivate var identifier: RNIObjectIdentifier { | ||
if let identifier = RNIObjectIdentifierMap.map.object(forKey: self) { | ||
return identifier; | ||
}; | ||
|
||
let identifier = RNIObjectIdentifier(); | ||
RNIObjectIdentifierMap.map.setObject(identifier, forKey: self); | ||
|
||
return identifier; | ||
}; | ||
|
||
static var synthesizedIdPrefix: String { "" }; | ||
|
||
public var synthesizedID: Int { | ||
self.identifier.id; | ||
}; | ||
|
||
public var synthesizedStringID: String { | ||
Self.synthesizedIdPrefix + "\(self.synthesizedID)"; | ||
}; | ||
|
||
public var synthesizedUUID: UUID { | ||
self.identifier.uuid; | ||
}; | ||
}; |