-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathFeatureFlagNotifier.swift
More file actions
55 lines (47 loc) · 1.98 KB
/
FeatureFlagNotifier.swift
File metadata and controls
55 lines (47 loc) · 1.98 KB
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
import Combine
import SwiftUI
public extension Notification.Name {
static let gitHubCopilotFeatureFlagsDidChange = Notification
.Name("com.github.CopilotForXcode.CopilotFeatureFlagsDidChange")
}
public enum ExperimentValue: Hashable, Codable {
case string(String)
case number(Double)
case boolean(Bool)
case stringArray([String])
}
public typealias ActiveExperimentForFeatureFlags = [String: ExperimentValue]
public struct FeatureFlags: Hashable, Codable {
public var rt: Bool
public var sn: Bool
public var chat: Bool
public var ic: Bool
public var pc: Bool
public var xc: Bool?
public var ae: ActiveExperimentForFeatureFlags
public var agent_mode: Bool?
}
public protocol FeatureFlagNotifier {
var featureFlags: FeatureFlags { get }
var featureFlagsDidChange: PassthroughSubject<FeatureFlags, Never> { get }
func handleFeatureFlagNotification(_ featureFlags: FeatureFlags)
}
public class FeatureFlagNotifierImpl: FeatureFlagNotifier {
public var featureFlags: FeatureFlags
public static let shared = FeatureFlagNotifierImpl()
public var featureFlagsDidChange: PassthroughSubject<FeatureFlags, Never>
init(featureFlags: FeatureFlags = FeatureFlags(rt: false, sn: false, chat: true, ic: true, pc: true, ae: [:]),
featureFlagsDidChange: PassthroughSubject<FeatureFlags, Never> = PassthroughSubject<FeatureFlags, Never>()) {
self.featureFlags = featureFlags
self.featureFlagsDidChange = featureFlagsDidChange
}
public func handleFeatureFlagNotification(_ featureFlags: FeatureFlags) {
self.featureFlags = featureFlags
self.featureFlags.chat = featureFlags.chat == true && featureFlags.xc == true
DispatchQueue.main.async { [weak self] in
guard let self else { return }
self.featureFlagsDidChange.send(self.featureFlags)
DistributedNotificationCenter.default().post(name: .gitHubCopilotFeatureFlagsDidChange, object: nil)
}
}
}