Skip to content

Commit

Permalink
feat: configurable storage
Browse files Browse the repository at this point in the history
  • Loading branch information
kwasniew committed Oct 2, 2023
1 parent 68b954a commit 5447338
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
33 changes: 26 additions & 7 deletions Sources/UnleashProxyClientSwift/Poller.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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)
}
}

Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions UnleashProxyClientSwift.podspec
Original file line number Diff line number Diff line change
@@ -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" }
Expand All @@ -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
end

0 comments on commit 5447338

Please sign in to comment.