1
0
mirror of https://github.com/danog/amp.git synced 2024-12-11 17:09:40 +01:00
amp/lib/Internal/PrivateStream.php

52 lines
1.1 KiB
PHP
Raw Normal View History

<?php
2016-08-16 06:46:26 +02:00
2016-05-27 01:20:05 +02:00
namespace Amp\Internal;
2017-01-04 02:10:27 +01:00
use Amp\Stream;
use AsyncInterop\Promise;
2016-05-27 01:20:05 +02:00
/**
2017-01-04 02:10:27 +01:00
* An stream that cannot externally emit values. Used by Emitter in development mode.
2016-06-01 18:37:12 +02:00
*
* @internal
2016-05-27 01:20:05 +02:00
*/
2017-01-04 02:10:27 +01:00
final class PrivateStream implements Stream {
2016-05-27 22:44:01 +02:00
use Producer;
2016-05-27 01:20:05 +02:00
/**
2017-01-04 02:10:27 +01:00
* @param callable(callable $emit, callable $complete, callable $fail): void $producer
2016-05-27 01:20:05 +02:00
*/
2017-01-04 02:10:27 +01:00
public function __construct(callable $producer) {
2016-05-27 01:20:05 +02:00
/**
2017-01-04 02:10:27 +01:00
* Emits a value from the stream.
2016-05-27 01:20:05 +02:00
*
* @param mixed $value
*
* @return \AsyncInterop\Promise
2016-05-27 01:20:05 +02:00
*/
2016-11-14 20:59:21 +01:00
$emit = function ($value = null): Promise {
2016-05-27 01:20:05 +02:00
return $this->emit($value);
};
/**
2017-01-04 02:10:27 +01:00
* Completes the stream with the given value.
2016-05-27 01:20:05 +02:00
*
* @param mixed $value
*/
2016-05-29 18:35:09 +02:00
$resolve = function ($value = null) {
$this->resolve($value);
2016-05-27 01:20:05 +02:00
};
/**
2017-01-04 02:10:27 +01:00
* Fails the stream with the given exception.
2016-05-27 01:20:05 +02:00
*
2016-08-11 21:35:58 +02:00
* @param \Throwable $reason
2016-05-27 01:20:05 +02:00
*/
2016-08-11 21:35:58 +02:00
$fail = function (\Throwable $reason) {
2016-05-29 18:35:09 +02:00
$this->fail($reason);
2016-05-27 01:20:05 +02:00
};
2017-01-04 02:10:27 +01:00
$producer($emit, $resolve, $fail);
2016-05-27 01:20:05 +02:00
}
}