Skip to content

Commit

Permalink
review
Browse files Browse the repository at this point in the history
  • Loading branch information
ncooke3 committed Aug 2, 2024
1 parent 3613214 commit e1b5a84
Showing 1 changed file with 26 additions and 28 deletions.
54 changes: 26 additions & 28 deletions FirebaseStorage/Sources/Storage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ import FirebaseCore
}

private class func storage(app: FirebaseApp, bucket: String) -> Storage {
return StorageInstanceCache.shared.storage(app: app, bucket: bucket)
return InstanceCache.shared.storage(app: app, bucket: bucket)
}

/// The `FirebaseApp` associated with this Storage instance.
Expand Down Expand Up @@ -240,6 +240,31 @@ import FirebaseCore
}

// MARK: - Internal and Private APIs

private final class InstanceCache: @unchecked Sendable {
static let shared = InstanceCache()

/// A map of active instances, grouped by app. Keys are FirebaseApp names and values are
/// instances of Storage associated with the given app.
private var instances: [String: Storage] = [:]

/// Lock to manage access to the instances array to avoid race conditions.
private var instancesLock: os_unfair_lock = .init()

private init() {}

func storage(app: FirebaseApp, bucket: String) -> Storage {
os_unfair_lock_lock(&instancesLock)
defer { os_unfair_lock_unlock(&instancesLock) }

if let instance = instances[bucket] {
return instance
}
let newInstance = FirebaseStorage.Storage(app: app, bucket: bucket)
instances[bucket] = newInstance
return newInstance
}
}

let fetcherService = StorageFetcherService()

Expand Down Expand Up @@ -325,30 +350,3 @@ import FirebaseCore
}
}
}

extension Storage {
private final class InstanceCache: @unchecked Sendable {
static let shared = InstanceCache()

/// A map of active instances, grouped by app. Keys are FirebaseApp names and values are
/// instances of Storage associated with the given app.
private var instances: [String: Storage] = [:]

/// Lock to manage access to the instances array to avoid race conditions.
private var instancesLock: os_unfair_lock = .init()

private init() {}

func storage(app: FirebaseApp, bucket: String) -> Storage {
os_unfair_lock_lock(&instancesLock)
defer { os_unfair_lock_unlock(&instancesLock) }

if let instance = instances[bucket] {
return instance
}
let newInstance = FirebaseStorage.Storage(app: app, bucket: bucket)
instances[bucket] = newInstance
return newInstance
}
}
}

0 comments on commit e1b5a84

Please sign in to comment.