Skip to content

Commit 75c0d09

Browse files
committed
Remove promise progress events, use streaming API instead
1 parent 00391b4 commit 75c0d09

5 files changed

Lines changed: 22 additions & 29 deletions

File tree

README.md

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,7 @@ What this means is that these endpoints actually emit any number of progress
153153
events (individual JSON objects).
154154

155155
The user-facing API hides this fact by resolving with an array of all individual
156-
progress events once the stream ends.
157-
158-
These progress events can also be accessed individually via the Promise
159-
progress handler like this:
156+
progress events once the stream ends:
160157

161158
```php
162159
$client->imageCreate('clue/streamripper')->then(
@@ -165,9 +162,6 @@ $client->imageCreate('clue/streamripper')->then(
165162
},
166163
function ($error) {
167164
// an error occurred (possibly after receiving *some* elements)
168-
},
169-
function ($element) {
170-
// will be invoked for *each* complete $element in the JSON stream
171165
}
172166
);
173167
```
@@ -177,8 +171,9 @@ this API has to keep all event objects in memory until the Promise resolves.
177171
This is easy to get started and usually works reasonably well for the above
178172
API endpoints.
179173

180-
If you're dealing with lots of concurrent requests (100+),
181-
it could be a better idea to use a streaming approach,
174+
If you're dealing with lots of concurrent requests (100+) or
175+
if you want to access the individual progress events as they happen, you
176+
should consider using a streaming approach instead,
182177
where only individual progress event objects have to be kept in memory.
183178
The following API endpoints complement the default Promise-based API and return
184179
a [`Stream`](https://github.com/reactphp/stream) instance instead:

examples/pull.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<?php
2+
// this example shows how the imageCreateStream() call can be used to pull a given image.
3+
// demonstrates the JSON streaming API, individual progress events will be printed as they happen.
24

35
require __DIR__ . '/../vendor/autoload.php';
46

@@ -13,14 +15,14 @@
1315
$factory = new Factory($loop);
1416
$client = $factory->createClient();
1517

16-
$client->imageCreate($image)->then(
17-
function ($response) {
18-
echo 'response: ' . json_encode($response) . PHP_EOL;
19-
},
20-
'var_dump',
21-
function ($info) {
22-
echo 'update: ' . json_encode($info) . PHP_EOL;
23-
}
24-
);
18+
$stream = $client->imageCreateStream($image);
19+
20+
$stream->on('progress', function ($progress) {
21+
echo 'progress: '. json_encode($progress) . PHP_EOL;
22+
});
23+
24+
$stream->on('close', function () {
25+
echo 'stream closed' . PHP_EOL;
26+
});
2527

2628
$loop->run();

examples/push.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,7 @@
2020
function ($response) {
2121
echo 'response: ' . json_encode($response) . PHP_EOL;
2222
},
23-
'var_dump',
24-
function ($info) {
25-
echo 'update: ' . json_encode($info) . PHP_EOL;
26-
}
23+
'var_dump'
2724
);
2825

2926
$loop->run();

src/Client.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -387,8 +387,8 @@ public function imageList($all = false)
387387
* This is a JSON streaming API endpoint that resolves with an array of all
388388
* individual progress events.
389389
*
390-
* These progress events can also be accessed individually via the Promise
391-
* progress handler.
390+
* If you want to access the individual progress events as they happen, you
391+
* should consider using `imageCreateStream()` instead.
392392
*
393393
* Pulling a private image from a remote registry will likely require authorization, so make
394394
* sure to pass the $registryAuth parameter, see `self::authHeaders()` for
@@ -477,8 +477,8 @@ public function imageHistory($image)
477477
* This is a JSON streaming API endpoint that resolves with an array of all
478478
* individual progress events.
479479
*
480-
* These progress events can also be accessed individually via the Promise
481-
* progress handler.
480+
* If you need to access the individual progress events as they happen, you
481+
* should consider using `imagePushStream()` instead.
482482
*
483483
* Pushing to a remote registry will likely require authorization, so make
484484
* sure to pass the $registryAuth parameter, see `self::authHeaders()` for

src/Io/StreamingParser.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,10 @@ public function deferredStream(ReadableStreamInterface $stream, $progressEventNa
9999
});
100100

101101
if ($stream->isReadable()) {
102-
// buffer all data events and emit as progress
102+
// buffer all data events for deferred resolving
103103
$buffered = array();
104-
$stream->on($progressEventName, function ($data) use ($deferred, &$buffered) {
104+
$stream->on($progressEventName, function ($data) use (&$buffered) {
105105
$buffered []= $data;
106-
$deferred->progress($data);
107106
});
108107

109108
// error event rejects

0 commit comments

Comments
 (0)