<?php

namespace React\Promise\Queue;

class SynchronousQueue implements QueueInterface
{
    private $queue = [];

    public function enqueue(callable $task)
    {
        if (1 === array_push($this->queue, $task)) {
            $this->drain();
        }
    }

    private function drain()
    {
        /** @var callable $task */
        while ($task = array_shift($this->queue)) {
            $task();
        }
    }
}

<?php

namespace React\Promise\Queue;

class SynchronousQueue implements QueueInterface
{
    private $queue = [];

    public function enqueue(callable $task)
    {
        if (1 === array_push($this->queue, $task)) {
            $this->drain();
        }
    }

    private function drain()
    {
        for ($i = key($this->queue); isset($this->queue[$i]); $i++) {
            $task = $this->queue[$i];

            $exception = null;

            try {
                $task();
            } catch (\Exception $exception) {
            }

            unset($this->queue[$i]);

            if ($exception) {
                throw $exception;
            }
        }

        $this->queue = [];
    }
}

Revert "Simplify queue draining code"
Revert "Simplify queue draining code"

This reverts commit cc6f1c3846a77f42c3d5cab013dd012bab2a682a.

Note: The task must be kept on the queue until after it is called.
Otherwise drain() will be called recursively.
