Skip to content

Commit

Permalink
⭐️ Impl: ValueInjectable+Helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
dominicstop committed Sep 27, 2024
1 parent 0cb73f7 commit e12bae8
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions ios/Temp/ValueInjectable+Helpers.swift
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;
};
};

0 comments on commit e12bae8

Please sign in to comment.