Skip to content

Commit

Permalink
Add option to retrieve current max depth
Browse files Browse the repository at this point in the history
  • Loading branch information
trowski committed Jan 5, 2016
1 parent 24f7f7d commit 48ecc86
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/Loop/AbstractLoop.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ public function queue(callable $callback, array $args = [])
/**
* {@inheritdoc}
*/
public function maxQueueDepth($depth)
public function maxQueueDepth($depth = null)
{
return $this->callableQueue->maxDepth($depth);
}
Expand Down
5 changes: 3 additions & 2 deletions src/Loop/Loop.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,12 @@ public function reInit();
/**
* Sets the maximum number of callbacks set with Loop::queue() that will be executed per tick.
*
* @param int $depth Maximum number of functions to execute each tick. Use 0 for unlimited.
* @param int|null $depth Maximum number of functions to execute each tick. Use 0 for unlimited. Use null to
* retrieve the current max depth.
*
* @return int Previous max depth.
*/
public function maxQueueDepth($depth);
public function maxQueueDepth($depth = null);

/**
* Queue a callback function to be run after all I/O has been handled in the current tick.
Expand Down
9 changes: 7 additions & 2 deletions src/Loop/Structures/CallableQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,17 @@ public function clear()
/**
* Sets the maximum number of functions that can be called when the queue is called.
*
* @param int $depth Maximum number of functions to execute when the queue is called. Use 0 for unlimited.
* @param int|null $depth Maximum number of functions to execute when the queue is called. Use 0 for unlimited.
* Use null to just retrieve the current max depth.
*
* @return int Previous max depth.
*/
public function maxDepth($depth)
public function maxDepth($depth = null)
{
if (null === $depth) {
return $this->maxDepth;
}

$previous = $this->maxDepth;

$depth = (int) $depth;
Expand Down
5 changes: 3 additions & 2 deletions src/Loop/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,12 @@ function queue(callable $callback /* , ...$args */)
/**
* Sets the maximum number of callbacks set with queue() that will be executed per tick.
*
* @param int $depth Maximum number of functions to execute each tick. Use 0 for unlimited.
* @param int|null $depth Maximum number of functions to execute each tick. Use 0 for unlimited. Use null to
* retrieve the current max depth.
*
* @return int Previous max depth.
*/
function maxQueueDepth($depth)
function maxQueueDepth($depth = null)
{
return loop()->maxQueueDepth($depth);
}
Expand Down
2 changes: 2 additions & 0 deletions tests/Loop/Structures/CallableQueueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ public function testInvoke()
public function testMaxDepth()
{
$previous = $this->queue->maxDepth(1);

$this->assertSame(1, $this->queue->maxDepth());

$this->queue->insert($this->createCallback(1));
$this->queue->insert($this->createCallback(1));
Expand Down

0 comments on commit 48ecc86

Please sign in to comment.