Skip to content

Commit

Permalink
refactor: remove redundant async when refreshing (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
polok authored Oct 23, 2024
1 parent cd613b5 commit 6ffc416
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 32 deletions.
49 changes: 23 additions & 26 deletions Sources/FeaturevisorSDK/Instance+Refresh.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,38 +19,35 @@ extension FeaturevisorInstance {

statuses.refreshInProgress = true

Task { [weak self] in
try? await self?
.fetchDatafileContent(
from: datafileUrl,
handleDatafileFetch: handleDatafileFetch
) { [weak self] result in
guard let self else {
return
}
try? fetchDatafileContent(
from: datafileUrl,
handleDatafileFetch: handleDatafileFetch
) { [weak self] result in
guard let self else {
return
}

switch result {
case .success(let datafileContent):
let currentRevision = self.getRevision()
let newRevision = datafileContent.revision
let isNotSameRevision = currentRevision != newRevision
switch result {
case .success(let datafileContent):
let currentRevision = self.getRevision()
let newRevision = datafileContent.revision
let isNotSameRevision = currentRevision != newRevision

self.datafileReader = DatafileReader(datafileContent: datafileContent)
logger.info("refreshed datafile")
self.datafileReader = DatafileReader(datafileContent: datafileContent)
logger.info("refreshed datafile")

self.emitter.emit(.refresh)
self.emitter.emit(.refresh)

if isNotSameRevision {
self.emitter.emit(.update)
}
if isNotSameRevision {
self.emitter.emit(.update)
}

self.statuses.refreshInProgress = false
self.statuses.refreshInProgress = false

case .failure(let error):
self.logger.error("failed to refresh datafile", ["error": error])
self.statuses.refreshInProgress = false
}
}
case .failure(let error):
self.logger.error("failed to refresh datafile", ["error": error])
self.statuses.refreshInProgress = false
}
}
}

Expand Down
14 changes: 8 additions & 6 deletions Tests/FeaturevisorSDKTests/InstanceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1115,7 +1115,7 @@ class FeaturevisorInstanceTests: XCTestCase {
func testHandleDatafileFetchReturnsValidResponse() {

// GIVEN
let expectation = expectation(description: "datafile_error_response_expectation")
let expectation = expectation(description: "datafile_success_response_expectation")
var options = InstanceOptions.default
options.datafileUrl = "https://featurevisor.datafilecontent.com"
options.onReady = { _ in
Expand Down Expand Up @@ -1154,11 +1154,8 @@ class FeaturevisorInstanceTests: XCTestCase {
let expectation = expectation(description: "datafile_error_response_expectation")
var wasDatafileContentFetchErrorThrown = false
var errorThrownDetails: String?

var options = InstanceOptions.default
options.datafileUrl = "https://featurevisor.datafilecontent.com"
options.handleDatafileFetch = { _ in
return .failure(FeaturevisorError.unparseableJSON(data: nil, errorMessage: "Error :("))
}
options.logger = createLogger { level, message, details in
guard case .error = level else {
return
Expand All @@ -1171,6 +1168,10 @@ class FeaturevisorInstanceTests: XCTestCase {

expectation.fulfill()
}
options.datafileUrl = "https://featurevisor.datafilecontent.com"
options.handleDatafileFetch = { _ in
.failure(FeaturevisorError.unparseableJSON(data: nil, errorMessage: "Error :("))
}
options.datafile = DatafileContent(
schemaVersion: "1",
revision: "0.0.1",
Expand All @@ -1181,9 +1182,10 @@ class FeaturevisorInstanceTests: XCTestCase {

// WHEN
let sdk = try! createInstance(options: options)
waitForExpectations(timeout: 1)

// THEN
waitForExpectations(timeout: 1)
XCTAssertFalse(sdk.isReady())
XCTAssertTrue(wasDatafileContentFetchErrorThrown)
XCTAssertEqual(
errorThrownDetails,
Expand Down

0 comments on commit 6ffc416

Please sign in to comment.