Skip to content

Commit

Permalink
Move it into Storage scope
Browse files Browse the repository at this point in the history
  • Loading branch information
ncooke3 committed Aug 1, 2024
1 parent ea1f31f commit 3613214
Showing 1 changed file with 27 additions and 25 deletions.
52 changes: 27 additions & 25 deletions FirebaseStorage/Sources/Storage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
}
}

0 comments on commit 3613214

Please sign in to comment.