Skip to content

Commit

Permalink
Merge pull request #76 from growthbook/feature/subscription
Browse files Browse the repository at this point in the history
added logic for subscription
  • Loading branch information
vazarkevych authored Sep 14, 2024
2 parents 860bc89 + 0dc1fa3 commit 98a28c5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
17 changes: 16 additions & 1 deletion Sources/CommonMain/GrowthBookSDK.swift
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ public struct GrowthBookModel {
/// It exposes two main methods: feature and run.
@objc public class GrowthBookSDK: NSObject, FeaturesFlowDelegate {
private var refreshHandler: CacheRefreshHandler?
private var subscriptions: [ExperimentRunCallback] = []
private var networkDispatcher: NetworkProtocol
public var gbContext: Context
private var featureVM: FeaturesViewModel!
Expand Down Expand Up @@ -189,6 +190,14 @@ public struct GrowthBookModel {
@objc public func getFeatures() -> [String: Feature] {
return gbContext.features
}

public func subscribe(_ result: @escaping ExperimentRunCallback) {
self.subscriptions.append(result)
}

public func clearSubscriptions() {
self.subscriptions.removeAll()
}

/// Get the value of the feature with a fallback
public func getFeatureValue(feature id: String, default defaultValue: JSON) -> JSON {
Expand Down Expand Up @@ -244,7 +253,13 @@ public struct GrowthBookModel {

/// The run method takes an Experiment object and returns an experiment result
@objc public func run(experiment: Experiment) -> ExperimentResult {
return ExperimentEvaluator(attributeOverrides: attributeOverrides).evaluateExperiment(context: gbContext, experiment: experiment)
let result = ExperimentEvaluator(attributeOverrides: attributeOverrides).evaluateExperiment(context: gbContext, experiment: experiment)

self.subscriptions.forEach { subscription in
subscription(experiment, result)
}

return result
}

/// The setForcedFeatures method updates forced features
Expand Down
3 changes: 3 additions & 0 deletions Sources/CommonMain/Utils/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ public typealias CacheRefreshHandler = (Bool) -> Void
/// Handler for experiment result
public typealias TrackingCallback = (Experiment, ExperimentResult) -> Void

/// Handler for subscribed experiment result
public typealias ExperimentRunCallback = (Experiment, ExperimentResult) -> Void

/// Triple Tuple for GrowthBook Namespaces
///
/// It has ID, StartRange & EndRange
Expand Down

0 comments on commit 98a28c5

Please sign in to comment.