-
Notifications
You must be signed in to change notification settings - Fork 0
/
FeatureToggleService.swift
63 lines (50 loc) · 1.7 KB
/
FeatureToggleService.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//
// FeatureToggle.swift
// FeatureToggle
//
import Foundation
public protocol FeatureToggleService {
typealias SetupResult = Swift.Result<Void, FeatureToggle.SetupError>
typealias FeatureEnabledResult = Swift.Result<Bool, FeatureToggle.FeatureError>
func setup(completion: @escaping (SetupResult) -> Void)
func isFeatureEnabled(feature: FeatureToggle.Feature, completion: @escaping (FeatureEnabledResult) -> Void)
}
public class FeatureToggle {
public enum Feature: String {
case feature1
}
public enum SetupError: Swift.Error {
case failedToFetch
}
public enum FeatureError: Swift.Error {
case featureDoesNotExist(feature: Feature)
}
private let provider: FeatureToggleProvider
private var features: [String: Bool] = [
Feature.feature1.rawValue: false
]
public init(provider: FeatureToggleProvider) {
self.provider = provider
self.provider.setup(with: features)
}
}
extension FeatureToggle: FeatureToggleService {
public func setup(completion: @escaping (SetupResult) -> Void) {
provider.fetchFeatureToggles { [weak self] result in
completion(
result
.mapError { _ in .failedToFetch }
.map { featureToggles in
self?.features = featureToggles
}
)
}
}
public func isFeatureEnabled(feature: Feature, completion: @escaping (FeatureEnabledResult) -> Void) {
if let value = features[feature.rawValue] {
completion(.success(value))
} else {
completion(.failure(.featureDoesNotExist(feature: feature)))
}
}
}