Skip to content

Commit

Permalink
Merge pull request #58 from growthbook/fix-bug-disconnection-sse
Browse files Browse the repository at this point in the history
Fixed bug disconnection SSE
  • Loading branch information
vazarkevych authored May 2, 2024
2 parents 3a027f8 + d62e4a7 commit c31b2cb
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 9 deletions.
9 changes: 7 additions & 2 deletions Sources/CommonMain/Caching/CachingManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,13 @@ public class CachingManager: CachingLayer {
// Check if file exists
if fileManager.fileExists(atPath: filePath) {
// Read File Contents
guard let jsonContents = fileManager.contents(atPath: filePath) else { return nil }
return jsonContents
if let jsonContents = fileManager.contents(atPath: filePath) {
return jsonContents
} else {
logger.error("Can't read file content")
}
} else {
logger.error("File doesn't exist")
}

return nil
Expand Down
24 changes: 18 additions & 6 deletions Sources/CommonMain/Features/FeaturesViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ class FeaturesViewModel {
self?.prepareFeaturesData(data: jsonData)
}
streamingUpdate.connect()

streamingUpdate.onDissconnect { _, shouldReconnect, _ in
if let shouldReconnect = shouldReconnect, shouldReconnect {
streamingUpdate.connect()
}
}
}

/// Fetch Features
Expand All @@ -45,7 +51,7 @@ class FeaturesViewModel {
}
} else {
delegate?.featuresFetchFailed(error: .failedToLoadData, isRemote: false)
logger.error("Failed load local data")
logger.error("Failed load data from local storage or data is empty")
}

if let apiUrl = apiUrl {
Expand Down Expand Up @@ -88,12 +94,18 @@ class FeaturesViewModel {
if let encryptedString = jsonPetitions.encryptedFeatures, !encryptedString.isEmpty {
if let encryptionKey = encryptionKey, !encryptionKey.isEmpty {
let crypto: CryptoProtocol = Crypto()
guard let features = crypto.getFeaturesFromEncryptedFeatures(encryptedString: encryptedString, encryptionKey: encryptionKey) else { return }

if let featureData = try? JSONEncoder().encode(features) {
manager.putData(fileName: Constants.featureCache, content: featureData)
if let features = crypto.getFeaturesFromEncryptedFeatures(encryptedString: encryptedString, encryptionKey: encryptionKey) {
if let featureData = try? JSONEncoder().encode(features) {
manager.putData(fileName: Constants.featureCache, content: featureData)
} else {
logger.error("Failed encode features")
}
delegate?.featuresFetchedSuccessfully(features: features, isRemote: true)
} else {
delegate?.featuresFetchFailed(error: .failedEncryptedFeatures, isRemote: true)
logger.error("Failed get features from encrypted features")
return
}
delegate?.featuresFetchedSuccessfully(features: features, isRemote: true)
} else {
delegate?.featuresFetchFailed(error: .failedMissingKey, isRemote: true)
return
Expand Down
2 changes: 1 addition & 1 deletion Sources/CommonMain/Network/SSEHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ extension SSEHandler {
private func shouldReconnect(statusCode: Int) -> Bool {
switch statusCode {
case 200:
return false
return true
case _ where statusCode > 200 && statusCode < 300:
return true
default:
Expand Down
1 change: 1 addition & 0 deletions Sources/CommonMain/Utils/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public struct BucketRange: Codable {
case failedToLoadData = 0
case failedParsedData = 1
case failedMissingKey = 2
case failedEncryptedFeatures = 3
}

/// Meta info about the variations
Expand Down

0 comments on commit c31b2cb

Please sign in to comment.