-
-
Notifications
You must be signed in to change notification settings - Fork 315
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6e6c903
commit d93cc29
Showing
15 changed files
with
257 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
src/LiveComponent/src/EventListener/DeferLiveComponentSubscriber.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Symfony\UX\LiveComponent\EventListener; | ||
|
||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use Symfony\UX\TwigComponent\Event\PostMountEvent; | ||
use Symfony\UX\TwigComponent\Event\PreRenderEvent; | ||
|
||
final class DeferLiveComponentSubscriber implements EventSubscriberInterface | ||
{ | ||
private const DEFAULT_LOADING_TAG = 'div'; | ||
|
||
private const DEFAULT_LOADING_TEMPLATE = null; | ||
|
||
public function onPostMount(PostMountEvent $event): void | ||
{ | ||
$data = $event->getData(); | ||
if (\array_key_exists('defer', $data)) { | ||
$event->addExtraMetadata('defer', true); | ||
unset($event->getData()['defer']); | ||
} | ||
|
||
if (\array_key_exists('loading-template', $data)) { | ||
$event->addExtraMetadata('loading-template', $data['loading-template']); | ||
unset($event->getData()['loading-template']); | ||
} | ||
|
||
if (\array_key_exists('loading-tag', $data)) { | ||
$event->addExtraMetadata('loading-tag', $data['loading-tag']); | ||
unset($event->getData()['loading-tag']); | ||
} | ||
|
||
$event->setData($data); | ||
} | ||
|
||
public function onPreRender(PreRenderEvent $event): void | ||
{ | ||
$mountedComponent = $event->getMountedComponent(); | ||
|
||
if (!$mountedComponent->hasExtraMetadata('defer')) { | ||
return; | ||
} | ||
|
||
$event->setTemplate('@LiveComponent/deferred.html.twig'); | ||
|
||
$variables = $event->getVariables(); | ||
$variables['loadingTemplate'] = self::DEFAULT_LOADING_TEMPLATE; | ||
$variables['loadingTag'] = self::DEFAULT_LOADING_TAG; | ||
|
||
if ($mountedComponent->hasExtraMetadata('loading-template')) { | ||
$variables['loadingTemplate'] = $mountedComponent->getExtraMetadata('loading-template'); | ||
} | ||
|
||
if ($mountedComponent->hasExtraMetadata('loading-tag')) { | ||
$variables['loadingTag'] = $mountedComponent->getExtraMetadata('loading-tag'); | ||
} | ||
|
||
$event->setVariables($variables); | ||
} | ||
|
||
public static function getSubscribedEvents(): array | ||
{ | ||
return [ | ||
PostMountEvent::class => ['onPostMount'], | ||
PreRenderEvent::class => ['onPreRender'], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<{{ loadingTag }} {{ attributes }} data-action="live:connect->live#$render"> | ||
{% block loadingContent %} | ||
{% if loadingTemplate != null %} | ||
{{ include(loadingTemplate) }} | ||
{% endif %} | ||
{% endblock %} | ||
</{{ loadingTag }}> |
19 changes: 19 additions & 0 deletions
19
src/LiveComponent/tests/Fixtures/Component/DeferredComponent.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Symfony\UX\LiveComponent\Tests\Fixtures\Component; | ||
|
||
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent; | ||
use Symfony\UX\LiveComponent\DefaultActionTrait; | ||
|
||
#[AsLiveComponent('deferred_component')] | ||
final class DeferredComponent | ||
{ | ||
use DefaultActionTrait; | ||
|
||
public function getLongAwaitedData(): string | ||
{ | ||
return 'Long awaited data'; | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
src/LiveComponent/tests/Fixtures/templates/components/deferred_component.html.twig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<div {{ attributes }}>{{ computed.longAwaitedData }}</div> |
1 change: 1 addition & 0 deletions
1
src/LiveComponent/tests/Fixtures/templates/dummy/loading.html.twig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
I'm loading a reaaaally slow live component |
1 change: 1 addition & 0 deletions
1
src/LiveComponent/tests/Fixtures/templates/render_deferred_component.html.twig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<twig:deferred_component defer /> |
1 change: 1 addition & 0 deletions
1
src/LiveComponent/tests/Fixtures/templates/render_deferred_component_with_li_tag.html.twig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<twig:deferred_component defer loading-tag='li' /> |
1 change: 1 addition & 0 deletions
1
src/LiveComponent/tests/Fixtures/templates/render_deferred_component_with_template.html.twig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<twig:deferred_component defer loading-template='dummy/loading.html.twig' /> |
82 changes: 82 additions & 0 deletions
82
src/LiveComponent/tests/Functional/EventListener/DeferLiveComponentSubscriberTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Symfony\UX\LiveComponent\Tests\Functional\EventListener; | ||
|
||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; | ||
use Symfony\UX\LiveComponent\Tests\LiveComponentTestHelper; | ||
use Zenstruck\Browser\Test\HasBrowser; | ||
|
||
final class DeferLiveComponentSubscriberTest extends KernelTestCase | ||
{ | ||
use HasBrowser; | ||
use LiveComponentTestHelper; | ||
|
||
public function testItSetsDeferredTemplateIfLiveIdNotPassed(): void | ||
{ | ||
$div = $this->browser() | ||
->visit('/render-template/render_deferred_component') | ||
->assertSuccessful() | ||
->crawler() | ||
->filter('div') | ||
; | ||
|
||
$this->assertSame('', trim($div->html())); | ||
$this->assertSame('live:connect->live#$render', $div->attr('data-action')); | ||
|
||
$component = $this->mountComponent('deferred_component', [ | ||
'data-live-id' => $div->attr('data-live-id'), | ||
]); | ||
|
||
$dehydrated = $this->dehydrateComponent($component); | ||
|
||
$div = $this->browser() | ||
->visit('/_components/deferred_component?props='.urlencode(json_encode($dehydrated->getProps()))) | ||
->assertSuccessful() | ||
->crawler() | ||
->filter('div') | ||
; | ||
|
||
$this->assertSame('Long awaited data', $div->html()); | ||
} | ||
|
||
public function testItIncludesGivenTemplateWhileLoadingDeferredComponent(): void | ||
{ | ||
$div = $this->browser() | ||
->visit('/render-template/render_deferred_component_with_template') | ||
->assertSuccessful() | ||
->crawler() | ||
->filter('div') | ||
; | ||
|
||
$this->assertSame('I\'m loading a reaaaally slow live component', trim($div->html())); | ||
|
||
$component = $this->mountComponent('deferred_component', [ | ||
'data-live-id' => $div->attr('data-live-id'), | ||
]); | ||
|
||
$dehydrated = $this->dehydrateComponent($component); | ||
|
||
$div = $this->browser() | ||
->visit('/_components/deferred_component?props='.urlencode(json_encode($dehydrated->getProps()))) | ||
->assertSuccessful() | ||
->crawler() | ||
->filter('div') | ||
; | ||
|
||
$this->assertStringContainsString('Long awaited data', $div->html()); | ||
} | ||
|
||
public function testItAllowsToSetCustomLoadingHtmlTag(): void | ||
{ | ||
$crawler = $this->browser() | ||
->visit('/render-template/render_deferred_component_with_li_tag') | ||
->assertSuccessful() | ||
->crawler() | ||
; | ||
|
||
$this->assertSame(0, $crawler->filter('div')->count()); | ||
$this->assertSame(1, $crawler->filter('li')->count()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters