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

Commit b549a6a

Browse files
authored
Merge pull request #66 from reactphp-parallel/add-thread-method-to-registry
Add thread method to registry
2 parents 64898b5 + 7ed9042 commit b549a6a

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

src/Proxy.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
use WyriHaximus\Metrics\Registry\Counters;
2020

2121
use function array_key_exists;
22-
use function bin2hex;
23-
use function random_bytes;
2422
use function serialize;
2523
use function unserialize;
2624

@@ -29,7 +27,6 @@
2927

3028
final class Proxy extends ProxyList
3129
{
32-
private const RANDOM_BYTES_LENGTH = 13;
3330
private const HASNT_PROXYABLE_INTERFACE = false;
3431

3532
private Factory $factory;
@@ -124,7 +121,7 @@ public function thread(object $object, string $interface): object
124121
$in = new Channel(Channel::Infinite);
125122
$destruct = new Channel(Channel::Infinite);
126123
$this->destruct[] = $destruct;
127-
$instance = new Instance($object, $interface, TRUE_, $in, bin2hex(random_bytes(self::RANDOM_BYTES_LENGTH)));
124+
$instance = $this->registry->thread($object, $interface, $in);
128125

129126
$this->factory->call(
130127
static function (string $object, string $interface, string $hash, Channel $in, Channel $destruct): void {

src/Proxy/Registry.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,16 @@
88
use ReactParallel\ObjectProxy\Generated\ProxyList;
99

1010
use function array_key_exists;
11+
use function bin2hex;
12+
use function random_bytes;
1113

1214
use const WyriHaximus\Constants\Boolean\FALSE_;
1315
use const WyriHaximus\Constants\Boolean\TRUE_;
1416

1517
final class Registry extends ProxyList
1618
{
19+
private const RANDOM_BYTES_LENGTH = 13;
20+
1721
private Channel $in;
1822

1923
/** @var array<string, Instance> */
@@ -76,4 +80,13 @@ public function share(object $object, string $interface): Instance
7680

7781
return $instance;
7882
}
83+
84+
public function thread(object $object, string $interface, Channel $in): Instance
85+
{
86+
$instance = new Instance($object, $interface, TRUE_, $in, bin2hex(random_bytes(self::RANDOM_BYTES_LENGTH)));
87+
$this->instances[$instance->hash()] = $instance;
88+
$this->shared[$interface] = $instance->hash();
89+
90+
return $instance;
91+
}
7992
}

0 commit comments

Comments
 (0)