Skip to content

Commit

Permalink
[5.x] Cache results of getComputedCallbacks for a given scope (#9640)
Browse files Browse the repository at this point in the history
Co-authored-by: Jason Varga <[email protected]>
  • Loading branch information
JohnathonKoster and jasonvarga authored Mar 13, 2024
1 parent ffe3e37 commit 4064e25
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/Data/StoresScopedComputedFieldCallbacks.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Closure;
use Illuminate\Support\Collection;
use Statamic\Facades\Blink;
use Statamic\Support\Arr;
use Statamic\Support\Str;

Expand All @@ -23,8 +24,10 @@ public function computed($scopes, string $field, Closure $callback)

public function getComputedCallbacks(string $scope): Collection
{
return collect($this->computedFieldCallbacks)
->filter(fn ($_, $key) => Str::startsWith($key, "{$scope}."))
->keyBy(fn ($_, $key) => Str::after($key, "{$scope}."));
return Blink::once(__CLASS__.'::getComputedCallbacks'.$scope, function () use ($scope) {
return collect($this->computedFieldCallbacks)
->filter(fn ($_, $key) => Str::startsWith($key, "{$scope}."))
->keyBy(fn ($_, $key) => Str::after($key, "{$scope}."));
});
}
}

0 comments on commit 4064e25

Please sign in to comment.