Skip to content

Commit cc7f4fa

Browse files
committed
[LiveComponent] Optimize LiveComponentStack::getCurrentLiveComponent()
1 parent 9bb9137 commit cc7f4fa

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

src/LiveComponent/src/Util/LiveComponentStack.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,18 @@
2424
*/
2525
final class LiveComponentStack extends ComponentStack
2626
{
27+
/** @var array<class-string, bool> */
28+
private array $cacheLiveComponents = [];
29+
2730
public function __construct(private readonly ComponentStack $componentStack)
2831
{
2932
}
3033

3134
public function getCurrentLiveComponent(): ?MountedComponent
3235
{
3336
foreach ($this->componentStack as $mountedComponent) {
34-
if ($this->isLiveComponent($mountedComponent->getComponent()::class)) {
37+
$componentClass = $mountedComponent->getComponent()::class;
38+
if ($this->cacheLiveComponents[$componentClass] ??= $this->isLiveComponent($componentClass)) {
3539
return $mountedComponent;
3640
}
3741
}
@@ -41,9 +45,7 @@ public function getCurrentLiveComponent(): ?MountedComponent
4145

4246
private function isLiveComponent(string $classname): bool
4347
{
44-
static $cache = [];
45-
46-
return $cache[$classname] ??= [] !== (new \ReflectionClass($classname))->getAttributes(AsLiveComponent::class);
48+
return [] !== (new \ReflectionClass($classname))->getAttributes(AsLiveComponent::class);
4749
}
4850

4951
public function getCurrentComponent(): ?MountedComponent

0 commit comments

Comments
 (0)