Skip to content

Commit

Permalink
Improve performance lazy loading (#1335)
Browse files Browse the repository at this point in the history
* wip

* Fix lazy loading performance

* remove debug

* Fix phpstan

* Add rootMargin
  • Loading branch information
luanfreitasdev committed Jan 18, 2024
1 parent 786132e commit 7fab9dc
Show file tree
Hide file tree
Showing 12 changed files with 42 additions and 61 deletions.
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
"composer/composer": "^2.6.6",
"laravel/pint": "^1.13.8",
"laradumps/laradumps-core": "^1.1",
"spaze/phpstan-disallowed-calls": "^2.16.1",
"larastan/larastan": "^2.8.1",
"pestphp/pest": "^2.30.0",
"orchestra/testbench": "8.19|^9.0"
Expand Down
2 changes: 1 addition & 1 deletion dist/mix-manifest.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"/powergrid.js": "/powergrid.js?id=3a880f256bcc04019bd6cf2f13f37474",
"/powergrid.js": "/powergrid.js?id=8762f1711fdcea9fbabfbf095ffd50fe",
"/bootstrap5.css": "/bootstrap5.css?id=03aba1df82c23db07c1d1096efcd42ae",
"/tailwind.css": "/tailwind.css?id=8186118970c318f469b566567b8fa4b9",
"/tom-select.css": "/tom-select.css?id=7af730d2c4bf937316d4002948b1571d",
Expand Down
2 changes: 1 addition & 1 deletion dist/powergrid.js

Large diffs are not rendered by default.

28 changes: 0 additions & 28 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,36 +1,8 @@
includes:
- ./vendor/larastan/larastan/extension.neon
- ./vendor/spaze/phpstan-disallowed-calls/extension.neon

parameters:

disallowedFunctionCalls:
-
function: 'ds()'
message: 'You should not be printing or dumping any logs'
-
function: 'dump()'
message: 'You should not be printing or dumping any logs'
-
function: 'logger()'
message: 'You should not be printing or dumping any logs'
-
function: 'logger()'
message: 'You should not be printing or dumping any logs'
-
function: 'var_dump()'
message: 'You should not be printing or dumping any logs'
-
function: 'print_r()'
message: 'You should not be printing or dumping any logs'

disallowedNamespaces:
-
class: 'Illuminate\Support\Facades\Log'
message: 'PowerGrid should not register any log'
allowIn:
- 'src/Commands/UpdateCommand.php'

ignoreErrors:
- '#Cannot call method [a-zA-Z0-9\\_]#'
- '#Variable \$title might not be defined.#'
Expand Down
2 changes: 2 additions & 0 deletions resources/js/components/pg-load-more.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export default () => ({
if (entries[0].isIntersecting) {
this.$wire.call('loadMore')
}
}, {
rootMargin: '300px'
});

observer.observe(this.$el)
Expand Down
15 changes: 8 additions & 7 deletions resources/views/components/table.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,20 @@
@endif
@php
$rowId = data_get($row, $primaryKey);
$class = data_get($theme, 'table.trBodyClass');
$rulesValues = $actionRulesClass->recoverFromAction($row, 'pg:rows');
$applyRulesLoop = true;
$trAttributesBag = new \Illuminate\View\ComponentAttributeBag();
$trAttributesBag = $trAttributesBag->merge(['class' => $class]);
if (method_exists($this, 'actionRules')) {
$applyRulesLoop = $actionRulesClass->loop($this->actionRules($row), $loop);
}
if (filled($rulesValues['setAttributes']) && $applyRulesLoop) {
foreach ($rulesValues['setAttributes'] as $rulesAttributes) {
$trAttributesBag = $trAttributesBag->merge([
Expand Down Expand Up @@ -91,6 +91,7 @@
$skip = $item * data_get($setUp, 'lazy.rowsPerChildren');
$take = data_get($setUp, 'lazy.rowsPerChildren');
@endphp

<livewire:lazy-child
key="{{ $this->getLazyKeys }}"
:child-index="$item"
Expand All @@ -104,7 +105,7 @@
:$tableName
:parentName="$this->getName()"
:columns="$this->visibleColumns"
:data="$this->getCachedData->skip($skip)->take($take)"
:data="\PowerComponents\LivewirePowerGrid\ProcessDataSource::transform($data->skip($skip)->take($take), $this)"
/>
@endforeach
</div>
Expand Down
10 changes: 7 additions & 3 deletions src/Livewire/LazyChild.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class LazyChild extends Component

public string $radioAttribute = 'id';

public mixed $data;
private Collection $data;

public array $theme;

Expand All @@ -38,8 +38,10 @@ class LazyChild extends Component

public string|int $childIndex;

public function mount(): void
public function mount(Collection $data): void
{
$this->data = $data;

$this->resolveDetailRow($this->data);
}

Expand All @@ -54,6 +56,8 @@ public function afterToggleDetail(string $id, string $state): void

public function render(): View
{
return view('livewire-powergrid::livewire.lazy-child');
$data = $this->data;

return view('livewire-powergrid::livewire.lazy-child', compact('data'));
}
}
2 changes: 1 addition & 1 deletion src/PowerGridComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ public function visibleColumns(): BaseCollection
->where('forceHidden', false);
}

#[Computed]
#[Computed(persist: true)]
protected function getCachedData(): mixed
{
if (!Cache::supportsTags() || !boolval(data_get($this->setUp, 'cache.enabled'))) {
Expand Down
31 changes: 17 additions & 14 deletions src/ProcessDataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use PowerComponents\LivewirePowerGrid\DataSource\{Support\Sql};
use PowerComponents\LivewirePowerGrid\Traits\SoftDeletes;

/** @internal */
class ProcessDataSource
{
use SoftDeletes;
Expand Down Expand Up @@ -86,7 +85,7 @@ private function processCollection(mixed $datasource): \Illuminate\Pagination\Le
$this->component->filtered = $results->pluck($this->component->primaryKey)->toArray();

$paginated = Collection::paginate($results, intval(data_get($this->component->setUp, 'footer.perPage')));
$results = $paginated->setCollection($this->transform($paginated->getCollection()));
$results = $paginated->setCollection($this->transform($paginated->getCollection(), $this->component));
};

return $results;
Expand Down Expand Up @@ -124,11 +123,15 @@ private function processModel(EloquentBuilder|MorphToMany|QueryBuilder|BaseColle

$this->setTotalCount($results);

if (filled(data_get($this->component, 'setUp.lazy'))) {
return $results;
}

/** @phpstan-ignore-next-line */
$collection = $results->getCollection();

/** @phpstan-ignore-next-line */
return $results->setCollection($this->transform($collection));
return $results->setCollection($this->transform($collection, $this->component));
}

/**
Expand Down Expand Up @@ -246,23 +249,23 @@ protected function resolveCollection(array|BaseCollection|EloquentBuilder|QueryB
});
}

public function transform(BaseCollection $results): BaseCollection
public static function transform(BaseCollection $results, PowerGridComponent $component): BaseCollection
{
$processedResults = collect();

$results->chunk(3)
->each(function (BaseCollection $collection) use (&$processedResults) {
$processedBatch = $this->processBatch($collection);
->each(function (BaseCollection $collection) use (&$processedResults, $component) {
$processedBatch = self::processBatch($collection, $component);
$processedResults = $processedResults->concat($processedBatch);
});

return $processedResults;
}

private function processBatch(BaseCollection $collection): BaseCollection
private static function processBatch(BaseCollection $collection, PowerGridComponent $component): BaseCollection
{
return $collection->map(function ($row, $index) {
$addColumns = $this->component->addColumns();
return $collection->map(function ($row, $index) use ($component) {
$addColumns = $component->addColumns();
$columns = $addColumns->columns;
$columns = collect($columns);

Expand All @@ -272,14 +275,14 @@ private function processBatch(BaseCollection $collection): BaseCollection
$prepareRules = collect();
$actions = collect();

if (method_exists($this->component, 'actionRules')) {
if (method_exists($component, 'actionRules')) {
$prepareRules = resolve(RulesController::class)
->execute($this->component->actionRules($row), (object)$row);
->execute($component->actionRules($row), (object)$row);
}

if (method_exists($this->component, 'actions')) {
$actions = (new ActionsController($this->component, $prepareRules))
->execute($this->component->actions($row), (object)$row);
if (method_exists($component, 'actions')) {
$actions = (new ActionsController($component, $prepareRules))
->execute($component->actions($row), (object)$row);
}

$mergedData = $data->merge([
Expand Down
2 changes: 1 addition & 1 deletion src/Traits/ExportableJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,6 @@ private function prepareToExport(): Eloquent\Collection|Collection
->orderBy($sortField, $processDataSource->component->sortDirection)
->get();

return $processDataSource->transform($results);
return $processDataSource->transform($results, $this->componentTable);
}
}
2 changes: 1 addition & 1 deletion src/Traits/LazyManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace PowerComponents\LivewirePowerGrid\Traits;

use Livewire\Attributes\{Computed, On};
use Livewire\Attributes\Computed;

/**
* @property-read bool $hasLazyEnabled
Expand Down
6 changes: 3 additions & 3 deletions src/Traits/WithExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,10 @@ public function prepareToExport(bool $selected = false): Eloquent\Collection|Sup
if ($inClause) {
$results = $processDataSource->get()->whereIn($this->primaryKey, $inClause);

return $processDataSource->transform($results);
return $processDataSource->transform($results, $this);
}

return $processDataSource->transform($processDataSource->resolveCollection());
return $processDataSource->transform($processDataSource->resolveCollection(), $this);
}

/** @phpstan-ignore-next-line */
Expand All @@ -194,7 +194,7 @@ public function prepareToExport(bool $selected = false): Eloquent\Collection|Sup
->orderBy($sortField, $processDataSource->component->sortDirection)
->get();

return $processDataSource->transform($results);
return $processDataSource->transform($results, $processDataSource->component);
}

/**
Expand Down

0 comments on commit 7fab9dc

Please sign in to comment.