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
16 changes: 12 additions & 4 deletions src/Once.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,17 @@ final class Once
private ?Future $future = null;

/**
* @var ?T
* @var T
*/
private mixed $value = null;
private mixed $value;

/**
* @var \Closure(T): bool
*/
private readonly mixed $isAlive;

private bool $isResolved = false;

/**
* @param \Closure(): T $function
* @param ?\Closure(T): bool $isAlive
Expand All @@ -45,16 +47,22 @@ public function __construct(
*/
public function await(?Cancellation $cancellation = null): mixed
{
if ($this->value !== null && ($this->isAlive)($this->value)) {
if ($this->isResolved && ($this->isAlive)($this->value)) {
return $this->value;
}

$this->isResolved = false;

$this->future ??= async($this->function);

try {
return $this->value = $this->future->await($cancellation);
$this->value = $this->future->await($cancellation);
} finally {
$this->future = null;
}

$this->isResolved = true;

return $this->value;
}
}
Empty file removed tests/.gitignore
Empty file.
70 changes: 70 additions & 0 deletions tests/OnceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

declare(strict_types=1);

namespace Thesis\Sync;

use Amp\DeferredFuture;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use function Amp\async;

#[CoversClass(Once::class)]
final class OnceTest extends TestCase
{
public function testReturnsOnceSameValue(): void
{
/** @var DeferredFuture<null> */
$deferred = new DeferredFuture();
$once = new Once(static function () use ($deferred): string {
$deferred->getFuture()->await();

return random_bytes(8);
});
$future1 = async(static fn() => $once->await());
$future2 = async(static fn() => $once->await());

async(static function () use ($deferred, $future1, $future2): void {
self::assertFalse($future1->isComplete());
self::assertFalse($future2->isComplete());

$deferred->complete();

self::assertSame($future1->await(), $future2->await());
})->await();
}

public function testWorksWithNull(): void
{
/** @var DeferredFuture<null> */
$deferred = new DeferredFuture();
$once = new Once(static fn(): null => $deferred->getFuture()->await());
$future = async(static fn() => $once->await());

async(static function () use ($deferred, $future): void {
self::assertFalse($future->isComplete());

$deferred->complete();

self::assertNull($future->await());
})->await();
}

public function testIsAlive(): void
{
$once = new Once(
static function (): int {
/** @var int */
static $i = 0;

return ++$i;
},
static fn(int $i): bool => $i > 1,
);

self::assertSame(1, $once->await());
self::assertSame(2, $once->await());
self::assertSame(2, $once->await());
self::assertSame(2, $once->await());
}
}