Exemplo n.º 1
0
 public function ensureActive()
 {
     $isOwner = false;
     if (!$this->hasFaulted && count($this->queue) > 0) {
         $isOwner = !$this->isAcquired;
         $this->isAcquired = true;
     }
     if (!$isOwner) {
         return;
     }
     $this->disposable->setDisposable($this->scheduler->scheduleRecursive(function ($recurse) {
         $parent = $this;
         if (count($parent->queue) > 0) {
             $work = array_shift($parent->queue);
         } else {
             $parent->isAcquired = false;
             return;
         }
         try {
             if (!is_callable($work)) {
                 throw new Exception("work is not callable");
             }
             $res = $work();
         } catch (Exception $e) {
             $res = $e;
         }
         if ($res instanceof Exception) {
             $parent->queue = [];
             $parent->hasFaulted = true;
             throw $res;
         }
         $recurse($parent);
     }));
 }
Exemplo n.º 2
0
 public function dispose()
 {
     if ($this->isDisposed) {
         return;
     }
     $this->isDisposed = true;
     $this->scheduler->schedule(function () {
         $this->disposable->dispose();
     });
 }
Exemplo n.º 3
0
 /**
  * @inheritDoc
  */
 public function __invoke(ObservableInterface $observable, ObserverInterface $observer, SchedulerInterface $scheduler = null)
 {
     $disposable = new SerialDisposable();
     $singleDisposable = new SingleAssignmentDisposable();
     $disposable->setDisposable($singleDisposable);
     $singleDisposable->setDisposable($this->scheduler->schedule(function () use($disposable, $observer, $observable, $scheduler) {
         $subscription = $observable->subscribe($observer, $scheduler);
         $disposable->setDisposable(new ScheduledDisposable($this->scheduler, $subscription));
     }));
     return $disposable;
 }
Exemplo n.º 4
0
 private function trim()
 {
     if (count($this->queue) > $this->bufferSize) {
         array_shift($this->queue);
     }
     if (null !== $this->scheduler) {
         $now = $this->scheduler->now();
         while (count($this->queue) > 0 && $now - $this->queue[0]["interval"] > $this->windowSize) {
             array_shift($this->queue);
         }
     }
 }