Skip to content

Commit

Permalink
fix: use storage provider
Browse files Browse the repository at this point in the history
  • Loading branch information
kwasniew committed Oct 25, 2023
1 parent 889c7f2 commit 0297b43
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
8 changes: 7 additions & 1 deletion Sources/UnleashProxyClientSwift/Poller.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public enum PollerError: Error {
public protocol StorageProvider {
func set(value: Toggle?, key: String)
func value(key: String) -> Toggle?
func clear()
}

public class DictionaryStorageProvider: StorageProvider {
Expand All @@ -31,6 +32,10 @@ public class DictionaryStorageProvider: StorageProvider {
public func value(key: String) -> Toggle? {
return storage[key]
}

public func clear() {
storage = [:]
}
}

public class Poller {
Expand All @@ -42,7 +47,7 @@ public class Poller {
var etag: String;

private let session: PollerSession
public var storageProvider: StorageProvider
var storageProvider: StorageProvider

public init(refreshInterval: Int? = nil, unleashUrl: URL, apiKey: String, session: PollerSession = URLSession.shared, storageProvider: StorageProvider = DictionaryStorageProvider()) {
self.refreshInterval = refreshInterval
Expand Down Expand Up @@ -79,6 +84,7 @@ public class Poller {
}

private func createFeatureMap(features: FeatureResponse) {
self.storageProvider.clear()
features.toggles.forEach { toggle in
self.storageProvider.set(value: toggle, key: toggle.name)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public struct Payload: Codable {
public class UnleashClientBase {
public var context: Context
var timer: Timer?
public var poller: Poller
var poller: Poller
var metrics: Metrics

public init(unleashUrl: String, clientKey: String, refreshInterval: Int = 15, metricsInterval: Int = 30, disableMetrics: Bool = false, appName: String = "unleash-swift-client", environment: String? = nil, poller: Poller? = nil, metrics: Metrics? = nil) {
Expand Down
4 changes: 4 additions & 0 deletions Tests/UnleashProxyClientSwiftTests/MockPoller.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ public class MockDictionaryStorageProvider: StorageProvider {
public func value(key: String) -> Toggle? {
return storage[key]
}

public func clear() {
storage = [:]
}
}

class MockPoller: Poller {
Expand Down

0 comments on commit 0297b43

Please sign in to comment.