From e12bae8a62100d5b6c1e6beb45ecbd8aac3dc423 Mon Sep 17 00:00:00 2001 From: Dominic Go Date: Fri, 27 Sep 2024 13:49:43 +0800 Subject: [PATCH] =?UTF-8?q?=E2=AD=90=EF=B8=8F=20Impl:=20`ValueInjectable+H?= =?UTF-8?q?elpers`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ios/Temp/ValueInjectable+Helpers.swift | 48 ++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 ios/Temp/ValueInjectable+Helpers.swift diff --git a/ios/Temp/ValueInjectable+Helpers.swift b/ios/Temp/ValueInjectable+Helpers.swift new file mode 100644 index 00000000..e372cae6 --- /dev/null +++ b/ios/Temp/ValueInjectable+Helpers.swift @@ -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>( + 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>( + 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; + }; +};