Skip to content

Commit

Permalink
Revert actor changes; don't use any thread-safety wrapper on config c…
Browse files Browse the repository at this point in the history
…allback array
  • Loading branch information
scannillo committed Aug 6, 2024
1 parent 8f51234 commit d546f2e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Foundation

/// Used to store, access, and manage an array of to-be-invoked `BTConfiguration` GET result callbacks in a thread-safe manner
actor ConfigurationCallbackStorage {
/// Used to store, access, and manage an array of to-be-invoked `BTConfiguration` GET result callbacks
class ConfigurationCallbackStorage {

private var pendingCompletions: [(BTConfiguration?, Error?) -> Void] = []

Expand Down
50 changes: 24 additions & 26 deletions Sources/BraintreeCore/Configuration/ConfigurationLoader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,32 +41,30 @@ class ConfigurationLoader {
return
}

Task {
await pendingCompletions.add(completion)

// If this is the 1st `v1/config` GET attempt, proceed with firing the network request.
// Otherwise, there is already a pending network request.
if await pendingCompletions.count == 1 {
http.get(configPath, parameters: BTConfigurationRequest()) { [weak self] body, response, error in
guard let self else {
self?.notifyCompletions(nil, BTAPIClientError.deallocated)
return
}
pendingCompletions.add(completion)

// If this is the 1st `v1/config` GET attempt, proceed with firing the network request.
// Otherwise, there is already a pending network request.
if pendingCompletions.count == 1 {
http.get(configPath, parameters: BTConfigurationRequest()) { [weak self] body, response, error in
guard let self else {
self?.notifyCompletions(nil, BTAPIClientError.deallocated)
return
}

if let error {
notifyCompletions(nil, error)
return
} else if response?.statusCode != 200 || body == nil {
notifyCompletions(nil, BTAPIClientError.configurationUnavailable)
return
} else {
let configuration = BTConfiguration(json: body)

try? configurationCache.putInCache(authorization: http.authorization.bearer, configuration: configuration)

if let error {
notifyCompletions(nil, error)
return
} else if response?.statusCode != 200 || body == nil {
notifyCompletions(nil, BTAPIClientError.configurationUnavailable)
return
} else {
let configuration = BTConfiguration(json: body)

try? configurationCache.putInCache(authorization: http.authorization.bearer, configuration: configuration)

notifyCompletions(configuration, nil)
return
}
notifyCompletions(configuration, nil)
return
}
}
}
Expand All @@ -87,6 +85,6 @@ class ConfigurationLoader {
// MARK: - Private Methods

func notifyCompletions(_ configuration: BTConfiguration?, _ error: Error?) {
Task { await pendingCompletions.invoke(configuration, error) }
pendingCompletions.invoke(configuration, error)
}
}

0 comments on commit d546f2e

Please sign in to comment.