mirror of
https://github.com/danog/postgres.git
synced 2024-11-27 04:24:45 +01:00
Make fetch callback a coroutine
This commit is contained in:
parent
1d6780d19b
commit
0cb2da3956
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
namespace Amp\Postgres;
|
namespace Amp\Postgres;
|
||||||
|
|
||||||
use Amp\{ CallableMaker, Coroutine, Deferred, Emitter, function pipe };
|
use Amp\{ CallableMaker, Coroutine, Deferred, Emitter };
|
||||||
use AsyncInterop\{ Loop, Promise };
|
use AsyncInterop\{ Loop, Promise };
|
||||||
use pq;
|
use pq;
|
||||||
|
|
||||||
@ -82,7 +82,7 @@ class PqExecutor implements Executor {
|
|||||||
Loop::disable($this->await);
|
Loop::disable($this->await);
|
||||||
|
|
||||||
$this->send = $this->callableFromInstanceMethod("send");
|
$this->send = $this->callableFromInstanceMethod("send");
|
||||||
$this->fetch = $this->callableFromInstanceMethod("fetch");
|
$this->fetch = \Amp\coroutine($this->callableFromInstanceMethod("fetch"));
|
||||||
$this->unlisten = $this->callableFromInstanceMethod("unlisten");
|
$this->unlisten = $this->callableFromInstanceMethod("unlisten");
|
||||||
$this->release = $this->callableFromInstanceMethod("release");
|
$this->release = $this->callableFromInstanceMethod("release");
|
||||||
}
|
}
|
||||||
@ -248,7 +248,7 @@ class PqExecutor implements Executor {
|
|||||||
$emitter->emit($notification);
|
$emitter->emit($notification);
|
||||||
}));
|
}));
|
||||||
|
|
||||||
return pipe($promise, function () use ($emitter, $channel): Listener {
|
return \Amp\pipe($promise, function () use ($emitter, $channel): Listener {
|
||||||
$this->listeners[$channel] = $emitter;
|
$this->listeners[$channel] = $emitter;
|
||||||
Loop::enable($this->poll);
|
Loop::enable($this->poll);
|
||||||
return new Listener($emitter->stream(), $channel, $this->unlisten);
|
return new Listener($emitter->stream(), $channel, $this->unlisten);
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
namespace Amp\Postgres;
|
namespace Amp\Postgres;
|
||||||
|
|
||||||
use Amp\{ Coroutine, Producer };
|
use Amp\Producer;
|
||||||
use pq;
|
use pq;
|
||||||
|
|
||||||
class PqUnbufferedResult extends TupleResult implements Operation {
|
class PqUnbufferedResult extends TupleResult implements Operation {
|
||||||
@ -12,7 +12,7 @@ class PqUnbufferedResult extends TupleResult implements Operation {
|
|||||||
private $numCols;
|
private $numCols;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param callable(): \Generator $fetch Coroutine function to fetch next result row.
|
* @param callable(): \AsyncInterop\Promise $fetch Function to fetch next result row.
|
||||||
* @param \pq\Result $result PostgreSQL result object.
|
* @param \pq\Result $result PostgreSQL result object.
|
||||||
*/
|
*/
|
||||||
public function __construct(callable $fetch, pq\Result $result) {
|
public function __construct(callable $fetch, pq\Result $result) {
|
||||||
@ -21,7 +21,7 @@ class PqUnbufferedResult extends TupleResult implements Operation {
|
|||||||
$count = 0;
|
$count = 0;
|
||||||
try {
|
try {
|
||||||
do {
|
do {
|
||||||
$next = new Coroutine($fetch()); // Request next result before current is consumed.
|
$next = $fetch(); // Request next result before current is consumed.
|
||||||
++$count;
|
++$count;
|
||||||
yield $emit($result->fetchRow(pq\Result::FETCH_ASSOC));
|
yield $emit($result->fetchRow(pq\Result::FETCH_ASSOC));
|
||||||
$result = yield $next;
|
$result = yield $next;
|
||||||
|
Loading…
Reference in New Issue
Block a user