2022-03-17 16:00:42 -07:00
|
|
|
// Copyright 2022 Google LLC
|
|
|
|
|
//
|
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
|
//
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
//
|
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
|
|
|
|
|
/**
|
2022-04-26 11:04:38 -07:00
|
|
|
* An extended `StorageTask` providing observable semantics that can be used for responding to changes
|
|
|
|
|
* in task state.
|
2024-02-01 14:57:41 -08:00
|
|
|
*
|
2022-04-26 11:04:38 -07:00
|
|
|
* Observers produce a `StorageHandle`, which is used to keep track of and remove specific
|
2022-03-17 16:00:42 -07:00
|
|
|
* observers at a later date.
|
|
|
|
|
*/
|
2026-07-08 19:09:40 -07:00
|
|
|
@objc(FIRStorageObservableTask) open class StorageObservableTask: StorageTask, @unchecked Sendable {
|
2022-03-17 16:00:42 -07:00
|
|
|
/**
|
|
|
|
|
* Observes changes in the upload status: Resume, Pause, Progress, Success, and Failure.
|
2022-04-26 11:04:38 -07:00
|
|
|
* - Parameters:
|
|
|
|
|
* - status: The `StorageTaskStatus` change to observe.
|
|
|
|
|
* - handler: A callback that fires every time the status event occurs,
|
|
|
|
|
* containing a `StorageTaskSnapshot` describing task state.
|
|
|
|
|
* - Returns: A task handle that can be used to remove the observer at a later date.
|
2022-03-17 16:00:42 -07:00
|
|
|
*/
|
|
|
|
|
@objc(observeStatus:handler:) @discardableResult
|
|
|
|
|
open func observe(_ status: StorageTaskStatus,
|
|
|
|
|
handler: @escaping (StorageTaskSnapshot) -> Void) -> String {
|
2022-06-29 10:29:49 -07:00
|
|
|
let callback = handler
|
2026-07-08 19:09:40 -07:00
|
|
|
let uuidString = UUID().uuidString
|
2022-06-29 10:29:49 -07:00
|
|
|
|
2026-07-08 19:09:40 -07:00
|
|
|
let snapshot = stateLock.withLock { () -> StorageTaskSnapshot in
|
2026-10-09 14:25:31 -07:00
|
|
|
let snapshot = snapshotUnderLock()
|
|
|
|
|
// A task that already succeeded or failed won't raise that event again, but it may still
|
|
|
|
|
// be notifying its existing observers. Don't register the handler in that case, so that it
|
|
|
|
|
// gets the event only once, from the immediate callback below.
|
|
|
|
|
let alreadyTerminal = (status == .success && snapshot.state == .success) ||
|
|
|
|
|
(status == .failure && (snapshot.state == .failed || snapshot.state == .failing))
|
|
|
|
|
if !alreadyTerminal {
|
|
|
|
|
handlerDictionaries[status]?[uuidString] = callback
|
|
|
|
|
handleToStatusMap[uuidString] = status
|
|
|
|
|
}
|
|
|
|
|
return snapshot
|
2026-07-08 19:09:40 -07:00
|
|
|
}
|
2022-06-29 10:29:49 -07:00
|
|
|
|
2026-07-08 19:09:40 -07:00
|
|
|
var shouldFire = false
|
|
|
|
|
switch status {
|
|
|
|
|
case .pause:
|
|
|
|
|
shouldFire = snapshot.state == .pausing || snapshot.state == .paused
|
|
|
|
|
case .resume:
|
|
|
|
|
shouldFire = snapshot.state == .resuming || snapshot.state == .running
|
|
|
|
|
case .progress:
|
|
|
|
|
shouldFire = snapshot.state == .running || snapshot.state == .progress
|
|
|
|
|
case .success:
|
|
|
|
|
shouldFire = snapshot.state == .success
|
|
|
|
|
case .failure:
|
|
|
|
|
shouldFire = snapshot.state == .failed || snapshot.state == .failing
|
|
|
|
|
case .unknown:
|
|
|
|
|
fatalError(
|
|
|
|
|
"Invalid observer status requested, use one of: Pause, Resume, Progress, Complete, or Failure"
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if shouldFire {
|
|
|
|
|
reference.storage.callbackQueue.async {
|
|
|
|
|
callback(snapshot)
|
2022-03-17 16:00:42 -07:00
|
|
|
}
|
2022-06-29 10:29:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return uuidString
|
2022-03-17 16:00:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Removes the single observer with the provided handle.
|
2024-02-01 14:57:41 -08:00
|
|
|
* - Parameter handle: The handle of the task to remove.
|
2022-03-17 16:00:42 -07:00
|
|
|
*/
|
2022-03-31 15:20:12 -07:00
|
|
|
@objc(removeObserverWithHandle:) open func removeObserver(withHandle handle: String) {
|
2026-07-08 19:09:40 -07:00
|
|
|
stateLock.withLock {
|
|
|
|
|
if let status = handleToStatusMap[handle] {
|
|
|
|
|
handlerDictionaries[status]?.removeValue(forKey: handle)
|
|
|
|
|
handleToStatusMap.removeValue(forKey: handle)
|
|
|
|
|
}
|
2022-06-29 10:29:49 -07:00
|
|
|
}
|
2022-03-17 16:00:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Removes all observers for a single status.
|
2024-02-01 14:57:41 -08:00
|
|
|
* - Parameter status: A `StorageTaskStatus` to remove all listeners for.
|
2022-03-17 16:00:42 -07:00
|
|
|
*/
|
|
|
|
|
@objc(removeAllObserversForStatus:)
|
2022-03-31 15:20:12 -07:00
|
|
|
open func removeAllObservers(for status: StorageTaskStatus) {
|
2026-07-08 19:09:40 -07:00
|
|
|
stateLock.withLock {
|
|
|
|
|
if let handlerDictionary = handlerDictionaries[status] {
|
|
|
|
|
for (key, _) in handlerDictionary {
|
|
|
|
|
handleToStatusMap.removeValue(forKey: key)
|
|
|
|
|
}
|
|
|
|
|
handlerDictionaries[status]?.removeAll()
|
2022-06-29 10:29:49 -07:00
|
|
|
}
|
|
|
|
|
}
|
2022-03-17 16:00:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Removes all observers.
|
|
|
|
|
*/
|
|
|
|
|
@objc open func removeAllObservers() {
|
2026-07-08 19:09:40 -07:00
|
|
|
stateLock.withLock {
|
|
|
|
|
for (status, _) in handlerDictionaries {
|
|
|
|
|
handlerDictionaries[status]?.removeAll()
|
|
|
|
|
}
|
|
|
|
|
handleToStatusMap.removeAll()
|
2022-06-29 10:29:49 -07:00
|
|
|
}
|
2022-03-17 16:00:42 -07:00
|
|
|
}
|
|
|
|
|
|
2022-06-29 10:29:49 -07:00
|
|
|
// MARK: - Private Handler Dictionaries
|
|
|
|
|
|
|
|
|
|
var handlerDictionaries: [StorageTaskStatus: [String: (StorageTaskSnapshot) -> Void]]
|
|
|
|
|
var handleToStatusMap: [String: StorageTaskStatus]
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The file to download to or upload from
|
|
|
|
|
*/
|
|
|
|
|
let fileURL: URL?
|
|
|
|
|
|
|
|
|
|
// MARK: - Internal Implementations
|
|
|
|
|
|
2023-11-21 12:12:05 -08:00
|
|
|
init(reference: StorageReference,
|
|
|
|
|
queue: DispatchQueue,
|
|
|
|
|
file: URL?) {
|
2022-06-29 10:29:49 -07:00
|
|
|
handlerDictionaries = [
|
|
|
|
|
.resume: [String: (StorageTaskSnapshot) -> Void](),
|
|
|
|
|
.pause: [String: (StorageTaskSnapshot) -> Void](),
|
|
|
|
|
.progress: [String: (StorageTaskSnapshot) -> Void](),
|
|
|
|
|
.success: [String: (StorageTaskSnapshot) -> Void](),
|
|
|
|
|
.failure: [String: (StorageTaskSnapshot) -> Void](),
|
|
|
|
|
]
|
|
|
|
|
handleToStatusMap = [:]
|
|
|
|
|
fileURL = file
|
2024-08-01 10:04:43 -07:00
|
|
|
super.init(reference: reference, queue: queue)
|
2022-06-29 10:29:49 -07:00
|
|
|
}
|
|
|
|
|
|
2023-11-21 12:12:05 -08:00
|
|
|
func fire(for status: StorageTaskStatus, snapshot: StorageTaskSnapshot) {
|
2026-07-08 19:09:40 -07:00
|
|
|
if let observerDictionary = stateLock.withLock({ handlerDictionaries[status] }) {
|
2022-06-29 10:29:49 -07:00
|
|
|
fire(handlers: observerDictionary, snapshot: snapshot)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-21 12:12:05 -08:00
|
|
|
func fire(handlers: [String: (StorageTaskSnapshot) -> Void],
|
|
|
|
|
snapshot: StorageTaskSnapshot) {
|
2026-07-08 19:09:40 -07:00
|
|
|
for handler in handlers.values {
|
2024-07-27 10:10:26 -07:00
|
|
|
reference.storage.callbackQueue.async {
|
2026-07-08 19:09:40 -07:00
|
|
|
handler(snapshot)
|
2022-06-29 10:29:49 -07:00
|
|
|
}
|
|
|
|
|
}
|
2022-03-17 16:00:42 -07:00
|
|
|
}
|
|
|
|
|
}
|