Skip to content

Commit

Permalink
[#148] Removed saveChanges() (#149)
Browse files Browse the repository at this point in the history
* The `saveChanges()` method was not needed for the majority of feature-flag stores
* Only `UserDefaultsStore` had this method implemented
* This change removes the method from protocols for mutable feature-flag stores
* `UserDefaultsStore` synchronizes the changes in `deinit`
* If a custom feature-flag store needs to save changes explicitly, it should implement an internal way to do so
  • Loading branch information
yakovmanshin authored May 12, 2024
1 parent 068bad7 commit 90460f3
Show file tree
Hide file tree
Showing 7 changed files with 4 additions and 78 deletions.
9 changes: 0 additions & 9 deletions Sources/YMFF/FeatureFlagResolver/FeatureFlagResolver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,6 @@ final public class FeatureFlagResolver {
self.init(configuration: configuration)
}

deinit {
let mutableStores = getMutableStores()
Task { [mutableStores] in
for store in mutableStores {
try? await store.saveChanges()
}
}
}

}

// MARK: - FeatureFlagResolverProtocol
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ final public class UserDefaultsStore {
self.userDefaults = userDefaults
}

deinit {
userDefaults.synchronize()
}

}

// MARK: - SynchronousMutableFeatureFlagStore
Expand All @@ -48,10 +52,6 @@ extension UserDefaultsStore: SynchronousMutableFeatureFlagStore {
userDefaults.removeObject(forKey: key)
}

public func saveChangesSync() {
userDefaults.synchronize()
}

}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,4 @@ public protocol MutableFeatureFlagStore: AnyObject, FeatureFlagStore {
/// - Parameter key: *Required.* The key used to address the value.
func removeValue(for key: FeatureFlagKey) async throws

/// Immediately saves changed values so they’re not lost.
///
/// + This method can be called when work with the feature flag store is finished.
func saveChanges() async throws

}

// MARK: - Default Implementation

extension MutableFeatureFlagStore {

// Not all kinds of feature flag stores need this method, so it’s optional to implement.
public func saveChanges() async throws { }

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ public protocol SynchronousMutableFeatureFlagStore: SynchronousFeatureFlagStore,
/// - Parameter key: *Required.* The key used to address the value.
func removeValueSync(for key: FeatureFlagKey) throws

/// Immediately saves changed values so they’re not lost.
///
/// + This method can be called when work with the feature flag store is finished.
func saveChangesSync() throws

}

// MARK: - Async Requirements
Expand All @@ -39,16 +34,4 @@ extension SynchronousMutableFeatureFlagStore {
try removeValueSync(for: key)
}

public func saveChanges() async throws {
try saveChangesSync()
}

}

// MARK: - Default Implementation

extension SynchronousMutableFeatureFlagStore {

public func saveChangesSync() throws { }

}
14 changes: 0 additions & 14 deletions Tests/YMFFTests/Cases/FeatureFlagResolverTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1038,18 +1038,4 @@ final class FeatureFlagResolverTests: XCTestCase {
}
}

func test_deinit() async throws {
let store1 = MutableFeatureFlagStoreMock()
let store2 = SynchronousMutableFeatureFlagStoreMock()
configuration.stores = [store1, store2]

configuration = nil
resolver = nil

try await Task.sleep(nanoseconds: 1_000_000)

XCTAssertEqual(store1.saveChanges_invocationCount, 1)
XCTAssertEqual(store2.saveChangesSync_invocationCount, 1)
}

}
10 changes: 0 additions & 10 deletions Tests/YMFFTests/Utilities/MutableFeatureFlagStoreMock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ final class MutableFeatureFlagStoreMock {
var removeValue_keys = [String]()
var removeValue_result: Result<Void, TestFeatureFlagStoreError>!

var saveChanges_invocationCount = 0
var saveChanges_result: Result<Void, TestFeatureFlagStoreError>!

}

// MARK: - MutableFeatureFlagStore
Expand Down Expand Up @@ -67,11 +64,4 @@ extension MutableFeatureFlagStoreMock: MutableFeatureFlagStore {
}
}

func saveChanges() async throws {
saveChanges_invocationCount += 1
if case .failure(let error) = saveChanges_result {
throw error
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ final class SynchronousMutableFeatureFlagStoreMock {
var removeValueSync_keys = [String]()
var removeValueSync_result: Result<Void, TestFeatureFlagStoreError>!

var saveChangesSync_invocationCount = 0
var saveChangesSync_result: Result<Void, TestFeatureFlagStoreError>!

}

// MARK: - SynchronousMutableFeatureFlagStore
Expand Down Expand Up @@ -67,11 +64,4 @@ extension SynchronousMutableFeatureFlagStoreMock: SynchronousMutableFeatureFlagS
}
}

func saveChangesSync() throws {
saveChangesSync_invocationCount += 1
if case .failure(let error) = saveChangesSync_result {
throw error
}
}

}

0 comments on commit 90460f3

Please sign in to comment.