From 54473383fc548a378f7699c53b93babd6398afed Mon Sep 17 00:00:00 2001 From: kwasniew Date: Mon, 2 Oct 2023 13:48:01 +0200 Subject: [PATCH] feat: configurable storage --- Sources/UnleashProxyClientSwift/Poller.swift | 33 +++++++++++++++----- UnleashProxyClientSwift.podspec | 4 +-- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/Sources/UnleashProxyClientSwift/Poller.swift b/Sources/UnleashProxyClientSwift/Poller.swift index 2ef284e..6d83c09 100644 --- a/Sources/UnleashProxyClientSwift/Poller.swift +++ b/Sources/UnleashProxyClientSwift/Poller.swift @@ -14,6 +14,25 @@ public enum PollerError: Error { case unhandledStatusCode } +public protocol StorageProvider { + func set(_ value: Toggle?, for key: String) + func value(for key: String) -> Toggle? +} + +public class DictionaryStorageProvider: StorageProvider { + private var storage: [String: Toggle] = [:] + + public init() {} + + public func set(_ value: Toggle?, for key: String) { + storage[key] = value + } + + public func value(for key: String) -> Toggle? { + return storage[key] + } +} + public class Poller { var refreshInterval: Int? var unleashUrl: URL @@ -24,8 +43,9 @@ public class Poller { var etag: String; private let session: PollerSession + private let storageProvider: StorageProvider - public init(refreshInterval: Int? = nil, unleashUrl: URL, apiKey: String, session: PollerSession = URLSession.shared) { + public init(refreshInterval: Int? = nil, unleashUrl: URL, apiKey: String, session: PollerSession = URLSession.shared, storageProvider: StorageProvider = DictionaryStorageProvider()) { self.refreshInterval = refreshInterval self.unleashUrl = unleashUrl self.apiKey = apiKey @@ -34,6 +54,7 @@ public class Poller { self.ready = false self.etag = "" self.session = session + self.storageProvider = storageProvider } public func start(context: Context, completionHandler: ((PollerError?) -> Void)? = nil) -> Void { @@ -59,11 +80,9 @@ public class Poller { return components?.url } - private func createFeatureMap(features: FeatureResponse) -> [String: Toggle] { - return features.toggles.reduce([String: Toggle]()) { result, toggle in - var updatedResult = result - updatedResult[toggle.name] = toggle - return updatedResult + private func createFeatureMap(features: FeatureResponse) { + features.toggles.forEach { toggle in + self.storageProvider.set(toggle, for: toggle.name) } } @@ -129,7 +148,7 @@ public class Poller { return } - self.toggles = self.createFeatureMap(features: json) + self.createFeatureMap(features: json) if (self.ready) { SwiftEventBus.post("update") } else { diff --git a/UnleashProxyClientSwift.podspec b/UnleashProxyClientSwift.podspec index e0b01d0..c168fb7 100644 --- a/UnleashProxyClientSwift.podspec +++ b/UnleashProxyClientSwift.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |spec| spec.name = "UnleashProxyClientSwift" -spec.version = "1.0.1" +spec.version = "1.0.2" spec.summary = "Allows frontend clients to talk to unleash through the unleash edge, frontend API or the (deprecated) unleash proxy" spec.homepage = "https://www.getunleash.io" spec.license = { :type => "MIT", :file => "LICENSE" } @@ -12,4 +12,4 @@ spec.source = { :git => "https://github.com/Unleash/unleash-proxy-client-s spec.source_files = "Sources/UnleashProxyClientSwift/**/*.swift" spec.xcconfig = { "SWIFT_VERSION" => "$(inherited)" } spec.dependency 'SwiftEventBus' -end \ No newline at end of file +end