StorageObservableTask.swift 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Copyright 2022 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. import Foundation
  15. import FirebaseStorageInternal
  16. /**
  17. * An extended `StorageTask` providing observable semantics that can be used for responding to changes
  18. * in task state.
  19. * Observers produce a `StorageHandle`, which is used to keep track of and remove specific
  20. * observers at a later date.
  21. * This class is not thread safe and can only be called on the main thread.
  22. */
  23. @objc(FIRStorageObservableTask) open class StorageObservableTask: StorageTask {
  24. /**
  25. * Observes changes in the upload status: Resume, Pause, Progress, Success, and Failure.
  26. * - Parameters:
  27. * - status: The `StorageTaskStatus` change to observe.
  28. * - handler: A callback that fires every time the status event occurs,
  29. * containing a `StorageTaskSnapshot` describing task state.
  30. * - Returns: A task handle that can be used to remove the observer at a later date.
  31. */
  32. @objc(observeStatus:handler:) @discardableResult
  33. open func observe(_ status: StorageTaskStatus,
  34. handler: @escaping (StorageTaskSnapshot) -> Void) -> String {
  35. return (impl as! FIRIMPLStorageObservableTask)
  36. .observe(FIRIMPLStorageTaskStatus(rawValue: status.rawValue)!) { snapshot in
  37. handler(StorageTaskSnapshot(impl: snapshot, task: StorageTask(impl: snapshot.task)))
  38. }
  39. }
  40. /**
  41. * Removes the single observer with the provided handle.
  42. * - Parameter handle The handle of the task to remove.
  43. */
  44. @objc(removeObserverWithHandle:) open func removeObserver(withHandle handle: String) {
  45. (impl as! FIRIMPLStorageObservableTask).removeObserver(withHandle: handle)
  46. }
  47. /**
  48. * Removes all observers for a single status.
  49. * - Parameter status A `StorageTaskStatus` to remove all listeners for.
  50. */
  51. @objc(removeAllObserversForStatus:)
  52. open func removeAllObservers(for status: StorageTaskStatus) {
  53. (impl as! FIRIMPLStorageObservableTask)
  54. .removeAllObservers(for: FIRIMPLStorageTaskStatus(rawValue: status.rawValue)!)
  55. }
  56. /**
  57. * Removes all observers.
  58. */
  59. @objc open func removeAllObservers() {
  60. (impl as! FIRIMPLStorageObservableTask).removeAllObservers()
  61. }
  62. internal init(impl: FIRIMPLStorageObservableTask) {
  63. super.init(impl: impl)
  64. }
  65. }