Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add synchronization around the client's user object #66

Merged
merged 5 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion AmplitudeExperiment.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Pod::Spec.new do |spec|
spec.osx.source_files = 'sources/Experiment/**/*.{h,swift}'
spec.osx.resource_bundle = { 'AmplitudeExperiment': ['Sources/Experiment/PrivacyInfo.xcprivacy'] }

spec.tvos.deployment_target = '10.0'
spec.tvos.deployment_target = '11.0'
spec.tvos.source_files = 'sources/Experiment/**/*.{h,swift}'
spec.tvos.resource_bundle = { 'AmplitudeExperiment': ['Sources/Experiment/PrivacyInfo.xcprivacy'] }

Expand Down
12 changes: 6 additions & 6 deletions Experiment.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@
SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
TARGETED_DEVICE_FAMILY = "1,2,6";
TVOS_DEPLOYMENT_TARGET = 10.0;
TVOS_DEPLOYMENT_TARGET = 11.0;
VERSIONING_SYSTEM = "apple-generic";
VERSION_INFO_PREFIX = "";
};
Expand Down Expand Up @@ -545,7 +545,7 @@
SWIFT_COMPILATION_MODE = wholemodule;
SWIFT_OPTIMIZATION_LEVEL = "-O";
TARGETED_DEVICE_FAMILY = "1,2,6";
TVOS_DEPLOYMENT_TARGET = 10.0;
TVOS_DEPLOYMENT_TARGET = 11.0;
VALIDATE_PRODUCT = YES;
VERSIONING_SYSTEM = "apple-generic";
VERSION_INFO_PREFIX = "";
Expand Down Expand Up @@ -580,7 +580,7 @@
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2,3,4,6";
TVOS_DEPLOYMENT_TARGET = 10.0;
TVOS_DEPLOYMENT_TARGET = 11.0;
WATCHOS_DEPLOYMENT_TARGET = 3.0;
};
name = Debug;
Expand Down Expand Up @@ -612,7 +612,7 @@
SUPPORTED_PLATFORMS = "iphonesimulator iphoneos watchos watchsimulator appletvos appletvsimulator macosx";
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2,3,4,6";
TVOS_DEPLOYMENT_TARGET = 10.0;
TVOS_DEPLOYMENT_TARGET = 11.0;
WATCHOS_DEPLOYMENT_TARGET = 3.0;
};
name = Release;
Expand All @@ -639,7 +639,7 @@
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2,3,6";
TVOS_DEPLOYMENT_TARGET = 10.0;
TVOS_DEPLOYMENT_TARGET = 11.0;
};
name = Debug;
};
Expand All @@ -664,7 +664,7 @@
SWIFT_OBJC_BRIDGING_HEADER = "Tests/ExperimentTests/ExperimentTests-Bridging-Header.h";
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2,3,6";
TVOS_DEPLOYMENT_TARGET = 10.0;
TVOS_DEPLOYMENT_TARGET = 11.0;
};
name = Release;
};
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ let package = Package(
platforms: [
.iOS(.v10),
.macOS(.v10_13),
.tvOS(.v10),
.tvOS(.v11),
.watchOS(.v3)
],
products: [
Expand Down
28 changes: 14 additions & 14 deletions Sources/Experiment/ExperimentClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ internal class DefaultExperimentClient : NSObject, ExperimentClient {
private let fetchQueue = DispatchQueue(label: "com.amplitude.experiment.FetchQueue")
private let flagsQueue = DispatchQueue(label: "com.amplitude.experiment.FlagsQueue")
private let startQueue = DispatchQueue(label: "com.amplitude.experiment.StartQueue")
private let userQueue = DispatchQueue(label: "com.amplitude.experiment.UserQueue")

internal init(apiKey: String, config: ExperimentConfig, storage: Storage) {
self.apiKey = apiKey
Expand Down Expand Up @@ -156,7 +157,7 @@ internal class DefaultExperimentClient : NSObject, ExperimentClient {

public func fetch(user: ExperimentUser?, options: FetchOptions?, completion: ((ExperimentClient, Error?) -> Void)? = nil) -> Void {
if user != nil && user != ExperimentUser() {
self.user = user
self.setUser(user)
}
fetchQueue.async {
do {
Expand Down Expand Up @@ -221,7 +222,9 @@ internal class DefaultExperimentClient : NSObject, ExperimentClient {
}

public func setUser(_ user: ExperimentUser?) {
self.user = user
userQueue.sync {
self.user = user
}
}

@available(*, deprecated, message: "User ExperimentConfig.userProvider instead")
Expand Down Expand Up @@ -573,13 +576,15 @@ internal class DefaultExperimentClient : NSObject, ExperimentClient {
self.backoff = nil
}

internal func mergeUserWithProvider() -> ExperimentUser {
var libraryUser: ExperimentUser = self.user ?? ExperimentUser()
if self.user?.library == nil {
let library = "\(ExperimentConfig.Constants.Library)/\(ExperimentConfig.Constants.Version)"
libraryUser = libraryUser.copyToBuilder().library(library).build()
internal func mergeUserWithProvider(_ providedUser: ExperimentUser? = nil) -> ExperimentUser {
userQueue.sync {
var libraryUser: ExperimentUser = self.user ?? ExperimentUser()
if self.user?.library == nil {
let library = "\(ExperimentConfig.Constants.Library)/\(ExperimentConfig.Constants.Version)"
libraryUser = libraryUser.copyToBuilder().library(library).build()
}
return libraryUser.merge(providedUser ?? userProvider?.getUser())
}
return libraryUser.merge(userProvider?.getUser())
}

internal func mergeUserWithProviderOrWait(timeout: DispatchTimeInterval) throws -> ExperimentUser {
Expand All @@ -589,12 +594,7 @@ internal class DefaultExperimentClient : NSObject, ExperimentClient {
} else {
providedUser = self.userProvider?.getUser()
}
var libraryUser: ExperimentUser = self.user ?? ExperimentUser()
if self.user?.library == nil {
let library = "\(ExperimentConfig.Constants.Library)/\(ExperimentConfig.Constants.Version)"
libraryUser = libraryUser.copyToBuilder().library(library).build()
}
return libraryUser.merge(providedUser)
return mergeUserWithProvider(providedUser)
}

private func parseResponseData(_ data: Data?) throws -> [String: Variant] {
Expand Down
6 changes: 4 additions & 2 deletions Sources/Experiment/Variant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,10 @@ import Foundation
if let objectPayload = self.payload as? [String: Any], let otherObjectPayload = other.payload as? [String: Any] {
return NSDictionary(dictionary: objectPayload).isEqual(to: otherObjectPayload)
}
let lhsData = try? JSONEncoder().encode(self)
let rhsData = try? JSONEncoder().encode(other)
let encoder = JSONEncoder()
encoder.outputFormatting = .sortedKeys
let lhsData = try? encoder.encode(self)
let rhsData = try? encoder.encode(other)
return lhsData == rhsData
}

Expand Down
1 change: 1 addition & 0 deletions Tests/ExperimentTests/ExperimentClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ class ExperimentClientTests: XCTestCase {
.library("\(ExperimentConfig.Constants.Library)/\(ExperimentConfig.Constants.Version)")
.build()
XCTAssertEqual(expectedUserAfterMerge, mergedUser)

}

func testMergeUserWithConfiguredProvider() {
Expand Down
Loading