-
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.
🛠 Refactor: Extract and Move
RNIWeakDictionary
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
1 parent
fd67ff9
commit 3ea1d05
Showing
2 changed files
with
55 additions
and
45 deletions.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
ios/src_library/React Native/RNIWeak/RNIWeakDictionary.swift
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,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); | ||
} | ||
} | ||
}; | ||
|
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