You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

31 lines
751 B

  1. //
  2. // Promise+Progress.swift
  3. // then
  4. //
  5. // Created by Sacha Durand Saint Omer on 20/02/2017.
  6. // Copyright © 2017 s4cha. All rights reserved.
  7. //
  8. import Foundation
  9. public extension Promise {
  10. @discardableResult public func progress(_ block: @escaping (Float) -> Void) -> Promise<T> {
  11. tryStartInitialPromiseAndStartIfneeded()
  12. let p = newLinkedPromise()
  13. syncStateWithCallBacks(
  14. success: p.fulfill,
  15. failure: p.reject,
  16. progress: { f in
  17. block(f)
  18. p.setProgress(f)
  19. }
  20. )
  21. p.start()
  22. return p
  23. }
  24. internal func setProgress(_ value: Float) {
  25. updateState(PromiseState<T>.pending(progress: value))
  26. }
  27. }