StorageObservableTask.swift 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. * Extends FIRStorageTask to provide observable semantics such as adding and removing observers.
  18. * Observers produce a FIRStorageHandle, which is used to keep track of and remove specific
  19. * observers at a later date.
  20. * This class is currently not thread safe and can only be called on the main thread.
  21. */
  22. @objc(FIRStorageObservableTask) open class StorageObservableTask: StorageTask {
  23. /**
  24. * Observes changes in the upload status: Resume, Pause, Progress, Success, and Failure.
  25. * @param status The StorageTaskStatus change to observe.
  26. * @param handler A callback that fires every time the status event occurs,
  27. * returns a StorageTaskSnapshot containing the state of the task.
  28. * @return A task handle that can be used to remove the observer at a later date.
  29. */
  30. @objc(observeStatus:handler:) @discardableResult
  31. open func observe(_ status: StorageTaskStatus,
  32. handler: @escaping (StorageTaskSnapshot) -> Void) -> String {
  33. return (impl as! FIRIMPLStorageObservableTask)
  34. .observe(FIRIMPLStorageTaskStatus(rawValue: status.rawValue)!) { snapshot in
  35. handler(StorageTaskSnapshot(impl: snapshot, task: StorageTask(impl: snapshot.task)))
  36. }
  37. }
  38. // - (FIRStorageHandle)observeStatus:(FIRIMPLStorageTaskStatus)status
  39. // handler:(void (^)(FIRIMPLStorageTaskSnapshot *snapshot))handler;
  40. /**
  41. * Removes the single observer with the provided handle.
  42. * @param 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. * @param status A StorageTaskStatus to remove 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. }