Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: remove redundant async when refreshing #78

Merged
merged 2 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -1111,7 +1111,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 @@ -1150,11 +1150,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 @@ -1167,6 +1164,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 @@ -1177,9 +1178,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
Loading