Skip to content

Commit ec494da

Browse files
authored
Merge pull request #39 from clue-labs/default-loop
Simplify usage by supporting new default loop
2 parents 4344a59 + dca335f commit ec494da

File tree

5 files changed

+30
-28
lines changed

5 files changed

+30
-28
lines changed

README.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,7 @@ existing SQLite database file (or automatically create it on first run) and then
6464
`INSERT` a new record to the database:
6565

6666
```php
67-
$loop = React\EventLoop\Factory::create();
68-
$factory = new Clue\React\SQLite\Factory($loop);
67+
$factory = new Clue\React\SQLite\Factory();
6968

7069
$db = $factory->openLazy('users.db');
7170
$db->exec('CREATE TABLE IF NOT EXISTS foo (id INTEGER PRIMARY KEY AUTOINCREMENT, bar STRING)');
@@ -78,8 +77,6 @@ $db->query('INSERT INTO foo (bar) VALUES (?)', [$name])->then(
7877
);
7978

8079
$db->quit();
81-
82-
$loop->run();
8380
```
8481

8582
See also the [examples](examples).
@@ -89,13 +86,17 @@ See also the [examples](examples).
8986
### Factory
9087

9188
The `Factory` is responsible for opening your [`DatabaseInterface`](#databaseinterface) instance.
92-
It also registers everything with the main [`EventLoop`](https://github.com/reactphp/event-loop#usage).
9389

9490
```php
95-
$loop = React\EventLoop\Factory::create();
96-
$factory = new Clue\React\SQLite\Factory($loop);
91+
$factory = new Clue\React\SQLite\Factory();
9792
```
9893

94+
This class takes an optional `LoopInterface|null $loop` parameter that can be used to
95+
pass the event loop instance to use for this object. You can use a `null` value
96+
here in order to use the [default loop](https://github.com/reactphp/event-loop#loop).
97+
This value SHOULD NOT be given unless you're sure you want to explicitly use a
98+
given event loop instance.
99+
99100
#### open()
100101

101102
The `open(string $filename, int $flags = null): PromiseInterface<DatabaseInterface>` method can be used to

composer.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,21 @@
1010
"email": "christian@clue.engineering"
1111
}
1212
],
13-
"autoload": {
14-
"psr-4": { "Clue\\React\\SQLite\\": "src/" }
15-
},
16-
"autoload-dev": {
17-
"psr-4": { "Clue\\Tests\\React\\SQLite\\": "tests/" }
18-
},
1913
"require": {
2014
"php": ">=5.4",
2115
"ext-sqlite3": "*",
2216
"clue/ndjson-react": "^1.0",
2317
"react/child-process": "^0.6",
24-
"react/event-loop": "^1.0 || ^0.5 || ^0.4 || ^0.3",
18+
"react/event-loop": "^1.2",
2519
"react/promise": "^2.7 || ^1.2.1"
2620
},
2721
"require-dev": {
2822
"phpunit/phpunit": "^9.3 || ^5.7 || ^4.8.35"
23+
},
24+
"autoload": {
25+
"psr-4": { "Clue\\React\\SQLite\\": "src/" }
26+
},
27+
"autoload-dev": {
28+
"psr-4": { "Clue\\Tests\\React\\SQLite\\": "tests/" }
2929
}
3030
}

examples/insert.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55

66
require __DIR__ . '/../vendor/autoload.php';
77

8-
$loop = React\EventLoop\Factory::create();
9-
$factory = new Factory($loop);
8+
$factory = new Factory();
109

1110
$n = isset($argv[1]) ? $argv[1] : 1;
1211
$db = $factory->openLazy('test.db');
@@ -21,5 +20,3 @@
2120
}
2221

2322
$db->quit();
24-
25-
$loop->run();

examples/search.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66

77
require __DIR__ . '/../vendor/autoload.php';
88

9-
$loop = React\EventLoop\Factory::create();
10-
$factory = new Factory($loop);
9+
$factory = new Factory();
1110

1211
$search = isset($argv[1]) ? $argv[1] : 'foo';
1312
$db = $factory->openLazy('test.db');
@@ -20,5 +19,3 @@
2019
}
2120
}, 'printf');
2221
$db->quit();
23-
24-
$loop->run();

src/Factory.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,37 @@
55
use Clue\React\SQLite\Io\LazyDatabase;
66
use Clue\React\SQLite\Io\ProcessIoDatabase;
77
use React\ChildProcess\Process;
8+
use React\EventLoop\Loop;
89
use React\EventLoop\LoopInterface;
910
use React\Promise\Deferred;
1011
use React\Stream\DuplexResourceStream;
1112

1213
class Factory
1314
{
15+
/** @var LoopInterface */
1416
private $loop;
17+
1518
private $bin = PHP_BINARY;
1619
private $useSocket;
1720

1821
/**
1922
* The `Factory` is responsible for opening your [`DatabaseInterface`](#databaseinterface) instance.
20-
* It also registers everything with the main [`EventLoop`](https://github.com/reactphp/event-loop#usage).
2123
*
2224
* ```php
23-
* $loop = \React\EventLoop\Factory::create();
24-
* $factory = new Factory($loop);
25+
* $factory = new Clue\React\SQLite\Factory();
2526
* ```
2627
*
27-
* @param LoopInterface $loop
28+
* This class takes an optional `LoopInterface|null $loop` parameter that can be used to
29+
* pass the event loop instance to use for this object. You can use a `null` value
30+
* here in order to use the [default loop](https://github.com/reactphp/event-loop#loop).
31+
* This value SHOULD NOT be given unless you're sure you want to explicitly use a
32+
* given event loop instance.
33+
*
34+
* @param ?LoopInterface $loop
2835
*/
29-
public function __construct(LoopInterface $loop)
36+
public function __construct(LoopInterface $loop = null)
3037
{
31-
$this->loop = $loop;
38+
$this->loop = $loop ?: Loop::get();
3239

3340
// use socket I/O for Windows only, use faster process pipes everywhere else
3441
$this->useSocket = DIRECTORY_SEPARATOR === '\\';

0 commit comments

Comments
 (0)