Skip to content

Commit

Permalink
🛠 Refactor: Extract and Move RNIWeakDictionary
Browse files Browse the repository at this point in the history
Related to: TODO:2023-03-04-15-39-46 - Impl: RNIModalManager

Summary: Extract `RNIWeakDictionary` from `ios/src_library/React Native/RNIWeak/RNIWeakRef.swift` to it's own file at `ios/src_library/React Native/RNIWeak/RNIWeakDictionary.swift`.
  • Loading branch information
dominicstop committed Mar 15, 2023
1 parent fd67ff9 commit 3ea1d05
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 45 deletions.
55 changes: 55 additions & 0 deletions ios/src_library/React Native/RNIWeak/RNIWeakDictionary.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//
// RNIWeakDictionary.swift
// react-native-ios-modal
//
// Created by Dominic Go on 3/15/23.
//

import Foundation


public class RNIWeakDictionary<K: Hashable, T> {

public var rawDict: [K: RNIWeakRef<T>] = [:];

public var purgedDict: [K: RNIWeakRef<T>] {
get {
self.rawDict.compactMapValues {
$0.rawRef != nil ? $0 : nil;
}
}
};

public var dict: [K: RNIWeakRef<T>] {
get {
let purgedDict = self.purgedDict;
self.rawDict = purgedDict;

return purgedDict;
}
}

public func set(for key: K, with value: T){
self.rawDict[key] = RNIWeakRef(with: value as AnyObject);
};

public func get(for key: K) -> T? {
guard let ref = self.rawDict[key]?.synthesizedRef else {
self.rawDict.removeValue(forKey: key);
return nil;
};

return ref;
};

public subscript(key: K) -> T? {
get {
self.get(for: key);
}
set {
guard let ref = newValue else { return };
self.set(for: key, with: ref);
}
}
};

45 changes: 0 additions & 45 deletions ios/src_library/React Native/RNIWeak/RNIWeakRef.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,48 +19,3 @@ public class RNIWeakRef<T> {
self.rawRef = ref;
};
};

public class RNIWeakDictionary<K: Hashable, T> {

public var rawDict: [K: RNIWeakRef<T>] = [:];

public var purgedDict: [K: RNIWeakRef<T>] {
get {
self.rawDict.compactMapValues {
$0.rawRef != nil ? $0 : nil;
}
}
};

public var dict: [K: RNIWeakRef<T>] {
get {
let purgedDict = self.purgedDict;
self.rawDict = purgedDict;

return purgedDict;
}
}

public func set(for key: K, with value: T){
self.rawDict[key] = RNIWeakRef(with: value as AnyObject);
};

public func get(for key: K) -> T? {
guard let ref = self.rawDict[key]?.synthesizedRef else {
self.rawDict.removeValue(forKey: key);
return nil;
};

return ref;
};

public subscript(key: K) -> T? {
get {
self.get(for: key);
}
set {
guard let ref = newValue else { return };
self.set(for: key, with: ref);
}
}
};

0 comments on commit 3ea1d05

Please sign in to comment.