Skip to content

Commit b866cdc

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

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

src/LiveComponent/src/Util/LiveComponentStack.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,11 @@ public function __construct(private readonly ComponentStack $componentStack)
3030

3131
public function getCurrentLiveComponent(): ?MountedComponent
3232
{
33+
static $cacheLive = [];
34+
3335
foreach ($this->componentStack as $mountedComponent) {
34-
if ($this->isLiveComponent($mountedComponent->getComponent()::class)) {
36+
$componentClass = $mountedComponent->getComponent()::class;
37+
if ($cacheLive[$componentClass] ??= $this->isLiveComponent($componentClass)) {
3538
return $mountedComponent;
3639
}
3740
}
@@ -41,9 +44,7 @@ public function getCurrentLiveComponent(): ?MountedComponent
4144

4245
private function isLiveComponent(string $classname): bool
4346
{
44-
static $cache = [];
45-
46-
return $cache[$classname] ??= [] !== (new \ReflectionClass($classname))->getAttributes(AsLiveComponent::class);
47+
return [] !== (new \ReflectionClass($classname))->getAttributes(AsLiveComponent::class);
4748
}
4849

4950
public function getCurrentComponent(): ?MountedComponent

0 commit comments

Comments
 (0)