Skip to content

Commit c271615

Browse files
committed
Update: delete stream transaction
1 parent 617b767 commit c271615

File tree

5 files changed

+21
-92
lines changed

5 files changed

+21
-92
lines changed

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,5 +71,3 @@ Provides standard coroutine architecture and tools for rapid development or pack
7171
`Email` jingnigg@gmail.com
7272

7373
`WeChat` jingnigg
74-
75-
---

README.zh_cn.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,5 +78,3 @@ workbunny团队体统的webman协程扩展, 为Webman提供了协程支持
7878
`电邮` jingnigg@gmail.com
7979

8080
`微信` jingnigg
81-
82-
---

example/suspension.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php declare(strict_types=1);
2+
3+
include __DIR__ . '/../vendor/autoload.php';
4+
5+
\Co\async(static function () {
6+
$suspension = \Co\getSuspension();
7+
8+
\Co\async(function () use ($suspension) {
9+
\Co\sleep(1);
10+
$suspension->resume();
11+
});
12+
13+
$suspension->suspend();
14+
15+
echo 'Coroutine 1', \PHP_EOL;
16+
});
17+
18+
\Co\wait();

src/Coroutine/Coroutine.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ public function async(Closure $closure): Promise
151151
return promise(function (Closure $resolve, Closure $reject, Promise $promise) use ($closure) {
152152
$suspension = new Suspension(function () use ($closure, $resolve, $reject) {
153153
try {
154-
$resolve($closure($resolve, $reject));
154+
$resolve($closure());
155155
} catch (EscapeException $exception) {
156156
throw $exception;
157157
} catch (Throwable $exception) {

src/Stream.php

Lines changed: 2 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,12 @@
1313
namespace Ripple;
1414

1515
use Closure;
16-
use Exception;
1716
use Revolt\EventLoop;
1817
use Ripple\Coroutine\Coroutine;
1918
use Ripple\Coroutine\Suspension;
2019
use Ripple\Stream\Exception\ConnectionCloseException;
21-
use Ripple\Stream\Exception\ConnectionException;
2220
use Ripple\Stream\Exception\ConnectionTimeoutException;
2321
use Ripple\Stream\Stream as StreamBase;
24-
use Ripple\Stream\Transaction;
2522
use Ripple\Utils\Format;
2623
use Ripple\Utils\Output;
2724
use Throwable;
@@ -79,22 +76,12 @@ class Stream extends StreamBase
7976
*/
8077
protected int $index = 0;
8178

82-
/**
83-
* @var Transaction
84-
*/
85-
protected Transaction $transaction;
86-
8779
/**
8880
* @param mixed $resource
8981
*/
9082
public function __construct(mixed $resource)
9183
{
9284
parent::__construct($resource);
93-
94-
$this->onClose(function () {
95-
$this->cancelReadable();
96-
$this->cancelWriteable();
97-
});
9885
}
9986

10087
/**
@@ -140,57 +127,6 @@ public function setBlocking(bool $bool): bool
140127
return stream_set_blocking($this->stream, $bool);
141128
}
142129

143-
/**
144-
* @param Closure $closure
145-
*
146-
* @return void
147-
* @throws Throwable
148-
*/
149-
public function transaction(Closure $closure): void
150-
{
151-
if (isset($this->transaction) && $this->transaction->getPromise()->getStatus() === Promise::PENDING) {
152-
throw new Exception('Transaction has been completed');
153-
}
154-
155-
$this->setTransaction(new Transaction($this));
156-
call_user_func_array($closure, [$this->getTransaction()]);
157-
}
158-
159-
/**
160-
* @return Transaction|null
161-
*/
162-
public function getTransaction(): Transaction|null
163-
{
164-
if (isset($this->transaction)) {
165-
return $this->transaction;
166-
}
167-
return null;
168-
}
169-
170-
/**
171-
* @param Transaction $transaction
172-
*
173-
* @return void
174-
*/
175-
protected function setTransaction(Transaction $transaction): void
176-
{
177-
if (isset($this->transaction)) {
178-
$this->completeTransaction();
179-
unset($this->transaction);
180-
}
181-
$this->transaction = $transaction;
182-
}
183-
184-
/**
185-
* @return void
186-
*/
187-
public function completeTransaction(): void
188-
{
189-
if (isset($this->transaction)) {
190-
$this->transaction->complete();
191-
}
192-
}
193-
194130
/**
195131
* Wait for readable events. This method is only valid when there are no readable events to listen for.
196132
* After enabling this method, it is forbidden to use the onReadable method elsewhere unless you know what you are doing.
@@ -208,7 +144,7 @@ public function waitForReadable(int $timeout = 0): bool
208144
{
209145
$suspension = getSuspension();
210146
if (!isset($this->onReadable)) {
211-
$this->onReadable(fn () => Coroutine::resume($suspension, true));
147+
$this->onReadable(static fn () => Coroutine::resume($suspension, true));
212148
$suspension instanceof Suspension && $suspension->promise->finally(fn () => $this->cancelReadable());
213149
}
214150

@@ -268,15 +204,6 @@ public function close(): void
268204
$this->cancelReadable();
269205
$this->cancelWriteable();
270206

271-
if (isset($this->transaction)) {
272-
$this->failTransaction(new ConnectionException(
273-
'Stream has been closed',
274-
ConnectionException::CONNECTION_CLOSED,
275-
null,
276-
$this
277-
));
278-
}
279-
280207
foreach ($this->onCloseCallbacks as $callback) {
281208
try {
282209
call_user_func($callback);
@@ -294,18 +221,6 @@ public function isClosed(): bool
294221
return !is_resource($this->stream);
295222
}
296223

297-
/**
298-
* @param Throwable $exception
299-
*
300-
* @return void
301-
*/
302-
public function failTransaction(Throwable $exception): void
303-
{
304-
if (isset($this->transaction)) {
305-
$this->transaction->fail($exception);
306-
}
307-
}
308-
309224
/**
310225
* @param string $key
311226
*
@@ -335,7 +250,7 @@ public function waitForWriteable(int $timeout = 0): bool
335250
{
336251
$suspension = getSuspension();
337252
if (!isset($this->onWriteable)) {
338-
$this->onWriteable(fn () => Coroutine::resume($suspension, true));
253+
$this->onWriteable(static fn () => Coroutine::resume($suspension, true));
339254
$suspension instanceof Suspension && $suspension->promise->finally(fn () => $this->cancelWriteable());
340255
}
341256

0 commit comments

Comments
 (0)