-
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
0cb73f7
commit e12bae8
Showing
1 changed file
with
48 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,48 @@ | ||
// | ||
// ValueInjectable+Helpers.swift | ||
// react-native-ios-modal | ||
// | ||
// Created by Dominic Go on 9/27/24. | ||
// | ||
|
||
import Foundation | ||
import DGSwiftUtilities | ||
|
||
|
||
public extension ValueInjectable { | ||
|
||
/// Lazily provides a optional fallback value, and set if needed | ||
func getInjectedValue<T, U: RawRepresentable<String>>( | ||
forKey key: U, | ||
fallbackValueProvider: (() -> T?)? = nil | ||
) -> T? { | ||
|
||
if let value = self.injectedValues[key.rawValue] as? T { | ||
return value; | ||
}; | ||
|
||
let fallbackValue = fallbackValueProvider?() | ||
guard let fallbackValue = fallbackValue else { | ||
return nil; | ||
}; | ||
|
||
self.injectedValues[key.rawValue] = fallbackValue; | ||
return fallbackValue; | ||
}; | ||
|
||
/// Lazily provides a fallback value, and set if needed | ||
func getInjectedValue<T, U: RawRepresentable<String>>( | ||
forKey key: U, | ||
fallbackValueProvider: () -> T | ||
) -> T { | ||
|
||
if let value = self.injectedValues[key.rawValue] as? T { | ||
return value; | ||
}; | ||
|
||
let fallbackValue = fallbackValueProvider() | ||
self.injectedValues[key.rawValue] = fallbackValue; | ||
|
||
return fallbackValue; | ||
}; | ||
}; |