From a7920d429a9b94f9b958a21528bc9b9559c19c3c Mon Sep 17 00:00:00 2001 From: Hugo Alliaume Date: Wed, 4 Jun 2025 23:30:44 +0200 Subject: [PATCH] [LiveComponent] Optimize `LiveComponentStack::getCurrentLiveComponent()` --- src/LiveComponent/src/Util/LiveComponentStack.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/LiveComponent/src/Util/LiveComponentStack.php b/src/LiveComponent/src/Util/LiveComponentStack.php index b7bf23f5f76..90d49e9a659 100644 --- a/src/LiveComponent/src/Util/LiveComponentStack.php +++ b/src/LiveComponent/src/Util/LiveComponentStack.php @@ -24,6 +24,9 @@ */ final class LiveComponentStack extends ComponentStack { + /** @var array */ + private array $cacheLiveComponents = []; + public function __construct(private readonly ComponentStack $componentStack) { } @@ -31,7 +34,8 @@ public function __construct(private readonly ComponentStack $componentStack) public function getCurrentLiveComponent(): ?MountedComponent { foreach ($this->componentStack as $mountedComponent) { - if ($this->isLiveComponent($mountedComponent->getComponent()::class)) { + $componentClass = $mountedComponent->getComponent()::class; + if ($this->cacheLiveComponents[$componentClass] ??= $this->isLiveComponent($componentClass)) { return $mountedComponent; } }