Skip to content

Commit

Permalink
lazy fork state
Browse files Browse the repository at this point in the history
  • Loading branch information
rez1dent3 committed Sep 6, 2022
1 parent 8f51a81 commit bce58b5
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
18 changes: 15 additions & 3 deletions src/Internal/Service/StateService.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,30 @@ final class StateService implements StateServiceInterface
*/
private array $forks = [];

public function fork(string $uuid, string $value): void
/**
* @var array<string, callable>
*/
private array $forkCallables = [];

public function fork(string $uuid, callable $value): void
{
$this->forks[$uuid] ??= $value;
$this->forkCallables[$uuid] ??= $value;
}

public function get(string $uuid): ?string
{
if ($this->forkCallables[$uuid] ?? null) {
$callable = $this->forkCallables[$uuid];
unset($this->forkCallables[$uuid]);

$this->forks[$uuid] = $callable();
}

return $this->forks[$uuid] ?? null;
}

public function drop(string $uuid): void
{
unset($this->forks[$uuid]);
unset($this->forks[$uuid], $this->forkCallables[$uuid]);
}
}
5 changes: 4 additions & 1 deletion src/Internal/Service/StateServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@

interface StateServiceInterface
{
public function fork(string $uuid, string $value): void;
/**
* @param callable(): string $value
*/
public function fork(string $uuid, callable $value): void;

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

Expand Down
2 changes: 1 addition & 1 deletion src/Services/AtomicService.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function blocks(array $objects, callable $callback): mixed

$callable = function () use ($blockObjects, $callback) {
foreach ($blockObjects as $uuid => $wallet) {
$this->stateService->fork($uuid, $this->bookkeeperService->amount($wallet));
$this->stateService->fork($uuid, fn () => $this->bookkeeperService->amount($wallet));
}
return $this->databaseService->transaction($callback);
};
Expand Down

0 comments on commit bce58b5

Please sign in to comment.