Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion infection.json.dist
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"@default": true,
"MethodCallRemoval": {
"ignore": [
"ReactParallel\\Factory::call::32"
"ReactParallel\\Factory::call::39"
]
}
}
Expand Down
27 changes: 19 additions & 8 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,24 @@

final class Factory
{
public static function futureToPromiseConverter(LoopInterface $loop): FutureToPromiseConverter
private ?FutureToPromiseConverter $futureToPromiseConverter = null;
private ?Infinite $infinitePool = null;

public function futureToPromiseConverter(LoopInterface $loop): FutureToPromiseConverter
{
return new FutureToPromiseConverter($loop);
if ($this->futureToPromiseConverter === null) {
$this->futureToPromiseConverter = new FutureToPromiseConverter($loop);
}

return $this->futureToPromiseConverter;
}

/**
* @param mixed[] $args
*/
public static function call(LoopInterface $loop, Closure $closure, array $args = []): PromiseInterface
public function call(LoopInterface $loop, Closure $closure, array $args = []): PromiseInterface
{
$runtime = Runtime::create(self::futureToPromiseConverter($loop));
$runtime = Runtime::create($this->futureToPromiseConverter($loop));

/**
* @psalm-suppress UndefinedInterfaceMethod
Expand All @@ -33,13 +40,17 @@ public static function call(LoopInterface $loop, Closure $closure, array $args =
});
}

public static function infinitePool(LoopInterface $loop): Infinite
public function infinitePool(LoopInterface $loop): Infinite
{
return new Infinite($loop, ONE);
if ($this->infinitePool === null) {
$this->infinitePool = new Infinite($loop, ONE);
}

return $this->infinitePool;
}

public static function limitedPool(LoopInterface $loop, int $threadCount): Limited
public function limitedPool(LoopInterface $loop, int $threadCount): Limited
{
return Limited::create($loop, $threadCount);
return Limited::createWithPool($this->infinitePool($loop), $threadCount);
}
}
17 changes: 9 additions & 8 deletions tests/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ final class FactoryTest extends AsyncTestCase
public function call(): void
{
$loop = EventLoopFactory::create();
$factory = new Factory();

/**
* Infection trick time
*/
Factory::futureToPromiseConverter($loop);
$factory->futureToPromiseConverter($loop);

self::assertSame(666, $this->await(Factory::call($loop, function (int $a, int $b): int {
self::assertSame(666, $this->await($factory->call($loop, function (int $a, int $b): int {
return $a * $b;
}, [333, 2]), $loop));
}
Expand All @@ -33,15 +34,15 @@ public function call(): void
*/
public function infinitePool(): void
{

$loop = EventLoopFactory::create();
$factory = new Factory();

/**
* Infection trick time
*/
Factory::futureToPromiseConverter($loop);
$factory->futureToPromiseConverter($loop);

$pool = Factory::infinitePool($loop);
$pool = $factory->infinitePool($loop);
self::assertSame(666, $this->await($pool->run(function (int $a, int $b): int {
return $a * $b;
}, [333, 2]), $loop));
Expand All @@ -53,15 +54,15 @@ public function infinitePool(): void
*/
public function limitedPool(): void
{

$loop = EventLoopFactory::create();
$factory = new Factory();

/**
* Infection trick time
*/
Factory::futureToPromiseConverter($loop);
$factory->futureToPromiseConverter($loop);

$pool = Factory::limitedPool($loop, 1);
$pool = $factory->limitedPool($loop, 1);
self::assertSame(666, $this->await($pool->run(function (int $a, int $b): int {
return $a * $b;
}, [333, 2]), $loop));
Expand Down