Skip to content

Commit

Permalink
⭐️ Impl: RNIIdentifiable
Browse files Browse the repository at this point in the history
  • Loading branch information
dominicstop committed Mar 30, 2023
1 parent 2bb51d3 commit 1f6ff8a
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions ios/src_library/Helpers+Utilities/RNIIdentifiable.swift
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;
};
};

0 comments on commit 1f6ff8a

Please sign in to comment.