From 36132147b311c48f101975a5c312f2cb3def1f2f Mon Sep 17 00:00:00 2001 From: Nick Cooke Date: Thu, 1 Aug 2024 17:50:43 -0400 Subject: [PATCH] Move it into Storage scope --- FirebaseStorage/Sources/Storage.swift | 52 ++++++++++++++------------- 1 file changed, 27 insertions(+), 25 deletions(-) diff --git a/FirebaseStorage/Sources/Storage.swift b/FirebaseStorage/Sources/Storage.swift index b1ca92af8aa..d0d52a586e5 100644 --- a/FirebaseStorage/Sources/Storage.swift +++ b/FirebaseStorage/Sources/Storage.swift @@ -21,31 +21,6 @@ import FirebaseCore // Avoids exposing internal FirebaseCore APIs to Swift users. @_implementationOnly import FirebaseCoreExtension -private final class StorageInstanceCache: @unchecked Sendable { - static let shared = StorageInstanceCache() - - /// 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 - } -} - /// Firebase Storage is a service that supports uploading and downloading binary objects, /// such as images, videos, and other files to Google Cloud Storage. Instances of `Storage` /// are not thread-safe, but can be accessed from any thread. @@ -350,3 +325,30 @@ private final class StorageInstanceCache: @unchecked Sendable { } } } + +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 + } + } +}