Skip to content

Commit e0e2147

Browse files
committed
Pass $loop argument to each function instead of constructor
1 parent 5bdcf62 commit e0e2147

File tree

7 files changed

+32
-39
lines changed

7 files changed

+32
-39
lines changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ function blockingExample()
3535
{
3636
// use a unique event loop instance for all parallel operations
3737
$loop = React\EventLoop\Factory::create();
38-
$blocker = new Blocker($loop);
38+
$blocker = new Blocker();
3939

4040
// this example uses an HTTP client
4141
// this could be pretty much everything that binds to an event loop
@@ -46,7 +46,7 @@ function blockingExample()
4646
$request2 = $browser->get('http://www.google.co.uk/');
4747

4848
// keep the loop running (i.e. block) until the first response arrives
49-
$fasterResponse = $blocker->awaitAny(array($request1, $request2));
49+
$fasterResponse = $blocker->awaitAny(array($request1, $request2), $loop);
5050

5151
return $fasterResponse->getBody();
5252
}
@@ -62,24 +62,24 @@ in order to make it run (block) until your conditions are fulfilled.
6262

6363
```php
6464
$loop = React\EventLoop\Factory::create();
65-
$blocker = new Blocker($loop);
65+
$blocker = new Blocker();
6666
```
6767

6868
#### sleep()
6969

70-
The `sleep($seconds)` method can be used to wait/sleep for $time seconds.
70+
The `sleep($seconds, LoopInterface $loop)` method can be used to wait/sleep for $time seconds.
7171

7272
#### await()
7373

74-
The `await(PromiseInterface $promise)` method can be used to block waiting for the given $promise to resolve.
74+
The `await(PromiseInterface $promise, LoopInterface $loop)` method can be used to block waiting for the given $promise to resolve.
7575

7676
#### awaitAny()
7777

78-
The `awaitAny(array $promises)` method can be used to wait for ANY of the given promises to resolve.
78+
The `awaitAny(array $promises, LoopInterface $loop)` method can be used to wait for ANY of the given promises to resolve.
7979

8080
#### awaitAll()
8181

82-
The `awaitAll(array $promises)` method can be used to wait for ALL of the given promises to resolve.
82+
The `awaitAll(array $promises, LoopInterface $loop)` method can be used to wait for ALL of the given promises to resolve.
8383

8484
## Install
8585

src/Blocker.php

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,15 @@
1010

1111
class Blocker
1212
{
13-
private $loop;
14-
15-
public function __construct(LoopInterface $loop)
16-
{
17-
$this->loop = $loop;
18-
}
19-
2013
/**
2114
* wait/sleep for $time seconds
2215
*
2316
* @param float $time
17+
* @param LoopInterface $loop
2418
*/
25-
public function sleep($time)
19+
public function sleep($time, LoopInterface $loop)
2620
{
2721
$wait = true;
28-
$loop = $this->loop;
2922
$loop->addTimer($time, function () use ($loop, &$wait) {
3023
$loop->stop();
3124
$wait = false;
@@ -40,15 +33,15 @@ public function sleep($time)
4033
* block waiting for the given $promise to resolve
4134
*
4235
* @param PromiseInterface $promise
36+
* @param LoopInterface $loop
4337
* @return mixed returns whatever the promise resolves to
4438
* @throws Exception when the promise is rejected
4539
*/
46-
public function await(PromiseInterface $promise)
40+
public function await(PromiseInterface $promise, LoopInterface $loop)
4741
{
4842
$wait = true;
4943
$resolved = null;
5044
$exception = null;
51-
$loop = $this->loop;
5245

5346
$promise->then(
5447
function ($c) use (&$resolved, &$wait, $loop) {
@@ -82,16 +75,16 @@ function ($error) use (&$exception, &$wait, $loop) {
8275
*
8376
* If ALL promises fail to resolve, this will fail and throw an Exception.
8477
*
85-
* @param array $promises
78+
* @param array $promises
79+
* @param LoopInterface $loop
8680
* @return mixed returns whatever the first promise resolves to
8781
* @throws Exception if ALL promises are rejected
8882
*/
89-
public function awaitAny(array $promises)
83+
public function awaitAny(array $promises, LoopInterface $loop)
9084
{
9185
$wait = count($promises);
9286
$value = null;
9387
$success = false;
94-
$loop = $this->loop;
9588

9689
foreach ($promises as $key => $promise) {
9790
/* @var $promise PromiseInterface */
@@ -149,16 +142,16 @@ function ($e) use (&$wait, $loop) {
149142
* If ANY promise fails to resolve, this will try to cancel() all
150143
* remaining promises and throw an Exception.
151144
*
152-
* @param array $promises
145+
* @param array $promises
146+
* @param LoopInterface $loop
153147
* @return array returns an array with whatever each promise resolves to
154148
* @throws Exception when ANY promise is rejected
155149
*/
156-
public function awaitAll(array $promises)
150+
public function awaitAll(array $promises, LoopInterface $loop)
157151
{
158152
$wait = count($promises);
159153
$exception = null;
160154
$values = array();
161-
$loop = $this->loop;
162155

163156
foreach ($promises as $key => $promise) {
164157
/* @var $promise PromiseInterface */

tests/FunctionAwaitAllTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ class FunctionAwaitAllTest extends TestCase
44
{
55
public function testAwaitAllEmpty()
66
{
7-
$this->assertEquals(array(), $this->block->awaitAll(array()));
7+
$this->assertEquals(array(), $this->block->awaitAll(array(), $this->loop));
88
}
99

1010
public function testAwaitAllAllResolved()
@@ -14,7 +14,7 @@ public function testAwaitAllAllResolved()
1414
'second' => $this->createPromiseResolved(2)
1515
);
1616

17-
$this->assertEquals(array('first' => 1, 'second' => 2), $this->block->awaitAll($all));
17+
$this->assertEquals(array('first' => 1, 'second' => 2), $this->block->awaitAll($all, $this->loop));
1818
}
1919

2020
public function testAwaitAllRejected()
@@ -25,7 +25,7 @@ public function testAwaitAllRejected()
2525
);
2626

2727
$this->setExpectedException('Exception', 'test');
28-
$this->block->awaitAll($all);
28+
$this->block->awaitAll($all, $this->loop);
2929
}
3030

3131
public function testAwaitAllOnlyRejected()
@@ -36,14 +36,14 @@ public function testAwaitAllOnlyRejected()
3636
);
3737

3838
$this->setExpectedException('Exception', 'first');
39-
$this->block->awaitAll($all);
39+
$this->block->awaitAll($all, $this->loop);
4040
}
4141

4242
public function testAwaitAllInterrupted()
4343
{
4444
$promise = $this->createPromiseResolved(2, 0.02);
4545
$this->createTimerInterrupt(0.01);
4646

47-
$this->assertEquals(array(2), $this->block->awaitAll(array($promise)));
47+
$this->assertEquals(array(2), $this->block->awaitAll(array($promise), $this->loop));
4848
}
4949
}

tests/FunctionAwaitAnyTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class FunctionAwaitAnyTest extends TestCase
99
*/
1010
public function testAwaitAnyEmpty()
1111
{
12-
$this->block->awaitAny(array());
12+
$this->block->awaitAny(array(), $this->loop);
1313
}
1414

1515
public function testAwaitAnyFirstResolved()
@@ -20,7 +20,7 @@ public function testAwaitAnyFirstResolved()
2020
$this->createPromiseResolved(3, 0.02)
2121
);
2222

23-
$this->assertEquals(2, $this->block->awaitAny($all));
23+
$this->assertEquals(2, $this->block->awaitAny($all, $this->loop));
2424
}
2525

2626
public function testAwaitAnyFirstResolvedConcurrently()
@@ -41,7 +41,7 @@ public function testAwaitAnyFirstResolvedConcurrently()
4141
$d3->promise()
4242
);
4343

44-
$this->assertEquals(2, $this->block->awaitAny($all));
44+
$this->assertEquals(2, $this->block->awaitAny($all, $this->loop));
4545
}
4646

4747
public function testAwaitAnyAllRejected()
@@ -52,14 +52,14 @@ public function testAwaitAnyAllRejected()
5252
);
5353

5454
$this->setExpectedException('UnderflowException');
55-
$this->block->awaitAny($all);
55+
$this->block->awaitAny($all, $this->loop);
5656
}
5757

5858
public function testAwaitAnyInterrupted()
5959
{
6060
$promise = $this->createPromiseResolved(2, 0.02);
6161
$this->createTimerInterrupt(0.01);
6262

63-
$this->assertEquals(2, $this->block->awaitAny(array($promise)));
63+
$this->assertEquals(2, $this->block->awaitAny(array($promise), $this->loop));
6464
}
6565
}

tests/FunctionAwaitTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,21 @@ public function testAwaitOneRejected()
77
$promise = $this->createPromiseRejected(new Exception('test'));
88

99
$this->setExpectedException('Exception', 'test');
10-
$this->block->await($promise);
10+
$this->block->await($promise, $this->loop);
1111
}
1212

1313
public function testAwaitOneResolved()
1414
{
1515
$promise = $this->createPromiseResolved(2);
1616

17-
$this->assertEquals(2, $this->block->await($promise));
17+
$this->assertEquals(2, $this->block->await($promise, $this->loop));
1818
}
1919

2020
public function testAwaitOneInterrupted()
2121
{
2222
$promise = $this->createPromiseResolved(2, 0.02);
2323
$this->createTimerInterrupt(0.01);
2424

25-
$this->assertEquals(2, $this->block->await($promise));
25+
$this->assertEquals(2, $this->block->await($promise, $this->loop));
2626
}
2727
}

tests/FunctionSleepTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class FunctionSleepTest extends TestCase
55
public function testSleep()
66
{
77
$time = microtime(true);
8-
$this->block->sleep(0.2);
8+
$this->block->sleep(0.2, $this->loop);
99
$time = microtime(true) - $time;
1010

1111
$this->assertEquals(0.2, $time, '', 0.1);

tests/bootstrap.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class TestCase extends PHPUnit_Framework_TestCase
1515
public function setUp()
1616
{
1717
$this->loop = React\EventLoop\Factory::create();
18-
$this->block = new Blocker($this->loop);
18+
$this->block = new Blocker();
1919
}
2020

2121
protected function createPromiseResolved($value = null, $delay = 0.01)

0 commit comments

Comments
 (0)