Skip to content

Commit

Permalink
Feat: workerman IteratorResponse.
Browse files Browse the repository at this point in the history
  • Loading branch information
cclilshy committed Oct 10, 2024
1 parent 894e957 commit 8cf899a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cloudtay/p-ripple-drive",
"version": "v1.2.9",
"version": "v1.2.11",
"license": "MIT",
"autoload": {
"psr-4": {
Expand Down
25 changes: 21 additions & 4 deletions src/Workerman/Extensions/IteratorResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,18 @@
class IteratorResponse extends Response
{
/*** @var \Iterator|mixed */
private Iterator $iterator;
protected Iterator $iterator;

/**
* @param Iterator|Closure $iterator 迭代器
* @param TcpConnection $tcpConnection TCP连接
* @param bool $autopilot 自动驾驶
*/
protected function __construct(
public function __construct(
Iterator|Closure $iterator,
protected readonly TcpConnection $tcpConnection,
bool $autopilot = true,
protected readonly bool $autopilot = true,
protected readonly bool $closeWhenFinish = false,
) {
if ($iterator instanceof Closure) {
$iterator = $iterator();
Expand Down Expand Up @@ -107,19 +108,35 @@ public function processIterator(): static
$this->tcpConnection->send($frame, true);
}

if ($this->closeWhenFinish) {
$this->close();
}

return $this;
}

/**
* @return void
*/
public function close(): void
{
$this->tcpConnection->close();
}

/**
* @param Iterator|Closure $iterator
* @param TcpConnection $tcpConnection
* @param bool $closeWhenFinish
* @param bool $autopilot
*
* @return IteratorResponse
*/
public static function create(
Iterator|Closure $iterator,
TcpConnection $tcpConnection,
bool $closeWhenFinish = false,
bool $autopilot = true,
): IteratorResponse {
return new self($iterator, $tcpConnection);
return new static($iterator, $tcpConnection, $closeWhenFinish, $autopilot);
}
}
6 changes: 5 additions & 1 deletion src/Workerman/RPL.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,17 @@ class RPL
/**
* @param Iterator|Closure $iterator
* @param TcpConnection $tcpConnection
* @param bool $closeWhenFinish
* @param bool $autopilot
*
* @return IteratorResponse
*/
public static function iteratorResponse(
Iterator|Closure $iterator,
TcpConnection $tcpConnection,
bool $closeWhenFinish = false,
bool $autopilot = true,
): IteratorResponse {
return IteratorResponse::create($iterator, $tcpConnection);
return IteratorResponse::create($iterator, $tcpConnection, $closeWhenFinish, $autopilot);
}
}

0 comments on commit 8cf899a

Please sign in to comment.