Last active
May 24, 2024 06:04
-
-
Save jaclync/3b77c8d0fdfa6481721558320b72fe38 to your computer and use it in GitHub Desktop.
Test mapping to nullable or filtering + mapping
This file contains hidden or 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
import Combine | |
struct PointOfSaleEntryPoint {} | |
let isBetaFeatureEnabled = PassthroughSubject<Bool, Never>() | |
let isEligibleForPOS = PassthroughSubject<Bool, Never>() | |
let posViewModel = | |
Publishers.CombineLatest(isBetaFeatureEnabled, isEligibleForPOS) | |
// // Map to optional value | |
// // Output: | |
// // posViewModel: nil | |
// // posViewModel: Optional(__lldb_expr_38.PointOfSaleEntryPoint()) | |
// // posViewModel: nil | |
// .map { isBetaFeatureEnabled, isEligibleForPOS -> PointOfSaleEntryPoint? in | |
// if isBetaFeatureEnabled && isEligibleForPOS { | |
// return PointOfSaleEntryPoint() | |
// } else { | |
// return nil | |
// } | |
// } | |
// Filter + map | |
// Output: | |
// posViewModel: PointOfSaleEntryPoint() | |
.filter { isBetaFeatureEnabled, isEligibleForPOS in | |
isBetaFeatureEnabled && isEligibleForPOS | |
} | |
.map { _ in | |
PointOfSaleEntryPoint() | |
} | |
posViewModel | |
.sink { viewModel in | |
print("posViewModel: \(viewModel)") | |
} | |
// Expected: nil view model | |
isBetaFeatureEnabled.send(true) | |
isEligibleForPOS.send(false) | |
// Expected: non-nil view model | |
isEligibleForPOS.send(true) | |
// Expected: nil view model | |
isBetaFeatureEnabled.send(false) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment