Skip to content
This repository was archived by the owner on Feb 17, 2025. It is now read-only.

Commit 94a133d

Browse files
committed
Shared channel
When rapidly creating and discarding object proxies the channel for communicating to the main thread stays behind. By using a shared channel that problem is solved.
1 parent d8f85bc commit 94a133d

File tree

5 files changed

+29
-12
lines changed

5 files changed

+29
-12
lines changed

src/AbstractGeneratedProxy.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
abstract class AbstractGeneratedProxy
1111
{
1212
private Channel $out;
13+
private string $hash;
1314

14-
final public function __construct(Channel $out)
15+
final public function __construct(Channel $out, string $hash)
1516
{
16-
$this->out = $out;
17+
$this->out = $out;
18+
$this->hash = $hash;
1719
}
1820

1921
/**
@@ -26,6 +28,7 @@ final protected function proxyCallToMainThread(string $method, array $args)
2628
$input = new Channel(1);
2729
$this->out->send(new Call(
2830
$input,
31+
$this->hash,
2932
$method,
3033
$args,
3134
));

src/Message/Call.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
final class Call
1010
{
1111
private Channel $channel;
12+
private string $hash;
1213
private string $method;
1314

1415
/** @var mixed[] */
@@ -17,9 +18,10 @@ final class Call
1718
/**
1819
* @param mixed[] $args
1920
*/
20-
public function __construct(Channel $channel, string $method, array $args)
21+
public function __construct(Channel $channel, string $hash, string $method, array $args)
2122
{
2223
$this->channel = $channel;
24+
$this->hash = $hash;
2325
$this->method = $method;
2426
$this->args = $args;
2527
}
@@ -29,6 +31,11 @@ public function channel(): Channel
2931
return $this->channel;
3032
}
3133

34+
public function hash(): string
35+
{
36+
return $this->hash;
37+
}
38+
3239
public function method(): string
3340
{
3441
return $this->method;

src/Proxy.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,26 @@
77
use parallel\Channel;
88
use ReactParallel\Factory;
99
use ReactParallel\ObjectProxy\Generated\ProxyList;
10+
use ReactParallel\ObjectProxy\Message\Call;
1011
use ReactParallel\ObjectProxy\Proxy\CallHandler;
12+
use Rx\Observable;
1113

1214
use function array_key_exists;
15+
use function spl_object_hash;
1316

1417
final class Proxy extends ProxyList
1518
{
1619
private const HASNT_PROXYABLE_INTERFACE = false;
1720

18-
private Factory $factory;
21+
private Channel $output;
22+
private Observable $outputStream;
1923
private CallHandler $callHandler;
2024

2125
public function __construct(Factory $factory)
2226
{
23-
$this->factory = $factory;
24-
$this->callHandler = new CallHandler($this);
27+
$this->output = new Channel(Channel::Infinite);
28+
$this->outputStream = $factory->streams()->channel($this->output)->share();
29+
$this->callHandler = new CallHandler($this);
2530
}
2631

2732
public function has(string $interface): bool
@@ -35,15 +40,14 @@ public function create(object $object, string $interface): object
3540
throw NonExistentInterface::create($interface);
3641
}
3742

38-
$output = new Channel(Channel::Infinite);
39-
40-
$this->factory->streams()->channel($output)->subscribe(
43+
$hash = spl_object_hash($object);
44+
$this->outputStream->filter(static fn (Call $call): bool => $call->hash() === $hash)->subscribe(
4145
($this->callHandler)($object, $interface)
4246
);
4347

4448
$class = self::KNOWN_INTERFACE[$interface];
4549

4650
/** @psalm-suppress InvalidStringClass */
47-
return new $class($output);
51+
return new $class($this->output, $hash);
4852
}
4953
}

tests/Message/CallTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ final class CallTest extends AsyncTestCase
1818
public function getters(): void
1919
{
2020
$channel = new Channel(1);
21+
$hash = 'haajshdkjlsajkl';
2122
$method = 'hammer';
2223
$args = [time()];
2324

24-
$call = new Call($channel, $method, $args);
25+
$call = new Call($channel, $hash, $method, $args);
2526

2627
self::assertSame($channel, $call->channel());
28+
self::assertSame($hash, $call->hash());
2729
self::assertSame($method, $call->method());
2830
self::assertSame($args, $call->args());
2931
}

tests/Proxy/CallHandlerTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,9 @@ public function logger(): void
4040
});
4141
$callHandler = new Proxy\CallHandler($proxy);
4242
$time = time();
43+
$hash = 'asljdjaslkdhklasdslkadskl';
4344
$channel = new Channel(1);
44-
$call = new Call($channel, 'get', [LoggerInterface::class]);
45+
$call = new Call($channel, $hash, 'get', [LoggerInterface::class]);
4546

4647
$loop->futureTick(static function () use ($container, $callHandler, $call): void {
4748
($callHandler)($container, ContainerInterface::class)($call);

0 commit comments

Comments
 (0)