Skip to content

Commit

Permalink
Check for iterator initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
trowski committed Nov 9, 2015
1 parent 3c17dab commit 5aed8c4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
12 changes: 12 additions & 0 deletions src/Observable/Exception/UninitializedError.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

/*
* This file is part of Icicle, a library for writing asynchronous code in PHP using promises and coroutines.
*
* @copyright 2014-2015 Aaron Piotrowski. All rights reserved.
* @license MIT See the LICENSE file that was distributed with this source code for more information.
*/

namespace Icicle\Observable\Exception;

class UninitializedError extends Error {}
1 change: 0 additions & 1 deletion src/Observable/Internal/Placeholder.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ public function __construct(PromiseInterface $promise, $backPressure)
public function getPromise()
{
++$this->waiting;

return $this->promise;
}

Expand Down
9 changes: 7 additions & 2 deletions src/Observable/ObservableIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Exception;
use Icicle\Observable\Exception\CompletedError;
use Icicle\Observable\Exception\SynchronousIterationError;
use Icicle\Observable\Exception\UninitializedError;
use Icicle\Promise;

class ObservableIterator implements ObservableIteratorInterface
Expand Down Expand Up @@ -59,9 +60,9 @@ public function __destruct()
*/
public function wait()
{
if (null !== $this->promise && $this->promise->isPending()) {
if (null !== $this->promise) {
throw new SynchronousIterationError(
'The returned promise should be resolved before calling this method again.'
'The coroutine created from this method should be resolved before calling this method again.'
);
}

Expand Down Expand Up @@ -100,6 +101,10 @@ public function getCurrent()
throw new CompletedError('The observable has completed and the iterator is invalid.');
}

if (null === $this->placeholder) {
throw new UninitializedError('wait() must be called before calling this method.');
}

return $this->current;
}

Expand Down

0 comments on commit 5aed8c4

Please sign in to comment.