Deferred.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. namespace React\Promise;
  3. class Deferred implements PromisorInterface
  4. {
  5. private $promise;
  6. private $resolveCallback;
  7. private $rejectCallback;
  8. private $notifyCallback;
  9. private $canceller;
  10. public function __construct(callable $canceller = null)
  11. {
  12. $this->canceller = $canceller;
  13. }
  14. public function promise()
  15. {
  16. if (null === $this->promise) {
  17. $this->promise = new Promise(function ($resolve, $reject, $notify) {
  18. $this->resolveCallback = $resolve;
  19. $this->rejectCallback = $reject;
  20. $this->notifyCallback = $notify;
  21. }, $this->canceller);
  22. $this->canceller = null;
  23. }
  24. return $this->promise;
  25. }
  26. public function resolve($value = null)
  27. {
  28. $this->promise();
  29. \call_user_func($this->resolveCallback, $value);
  30. }
  31. public function reject($reason = null)
  32. {
  33. $this->promise();
  34. \call_user_func($this->rejectCallback, $reason);
  35. }
  36. /**
  37. * @deprecated 2.6.0 Progress support is deprecated and should not be used anymore.
  38. * @param mixed $update
  39. */
  40. public function notify($update = null)
  41. {
  42. $this->promise();
  43. \call_user_func($this->notifyCallback, $update);
  44. }
  45. /**
  46. * @deprecated 2.2.0
  47. * @see Deferred::notify()
  48. */
  49. public function progress($update = null)
  50. {
  51. $this->notify($update);
  52. }
  53. }