Skip to content

Commit

Permalink
fix: add lock to client
Browse files Browse the repository at this point in the history
  • Loading branch information
0xWOF committed Apr 17, 2024
1 parent 354163c commit f157ba0
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion Source/Network/Implement/Client/DataClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public final class DataClient: Client {
public typealias Request = Data?
public typealias Response = Data?

let lock = Lock()
var storage = [ObjectIdentifier: PromisePending<ClientResult<Data?>, Error>]()

let session: URLSession
Expand Down Expand Up @@ -107,10 +108,14 @@ extension DataClient {

task.resume()

lock.lock()
storage[ObjectIdentifier(pending)] = pending
lock.unlock()
let remove = { [weak self, weak pending] in
guard let self, let pending else { return }
self.storage[ObjectIdentifier(pending)] = nil
self.lock.lock()
self.storage.removeValue(forKey: ObjectIdentifier(pending))
self.lock.unlock()
}
pending.promise.subscribe(
onResolved: { _ in remove() },
Expand Down

0 comments on commit f157ba0

Please sign in to comment.