Skip to content

Commit 77e4390

Browse files
committed
lazy fork state
1 parent 826c385 commit 77e4390

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

src/Internal/Service/StateService.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,30 @@ final class StateService implements StateServiceInterface
1111
*/
1212
private array $forks = [];
1313

14-
public function fork(string $uuid, string $value): void
14+
/**
15+
* @var array<string, callable>
16+
*/
17+
private array $forkCallables = [];
18+
19+
public function fork(string $uuid, callable $value): void
1520
{
16-
$this->forks[$uuid] ??= $value;
21+
$this->forkCallables[$uuid] ??= $value;
1722
}
1823

1924
public function get(string $uuid): ?string
2025
{
26+
if ($this->forkCallables[$uuid] ?? null) {
27+
$callable = $this->forkCallables[$uuid];
28+
unset($this->forkCallables[$uuid]);
29+
30+
$this->forks[$uuid] = $callable();
31+
}
32+
2133
return $this->forks[$uuid] ?? null;
2234
}
2335

2436
public function drop(string $uuid): void
2537
{
26-
unset($this->forks[$uuid]);
38+
unset($this->forks[$uuid], $this->forkCallables[$uuid]);
2739
}
2840
}

src/Internal/Service/StateServiceInterface.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66

77
interface StateServiceInterface
88
{
9-
public function fork(string $uuid, string $value): void;
9+
/**
10+
* @param callable(): string $value
11+
*/
12+
public function fork(string $uuid, callable $value): void;
1013

1114
public function get(string $uuid): ?string;
1215

src/Services/AtomicService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function blocks(array $objects, callable $callback): mixed
5252

5353
$callable = function () use ($blockObjects, $callback) {
5454
foreach ($blockObjects as $uuid => $wallet) {
55-
$this->stateService->fork($uuid, $this->bookkeeperService->amount($wallet));
55+
$this->stateService->fork($uuid, fn () => $this->bookkeeperService->amount($wallet));
5656
}
5757
return $this->databaseService->transaction($callback);
5858
};

0 commit comments

Comments
 (0)