Skip to content

Commit

Permalink
Improve processRows performance
Browse files Browse the repository at this point in the history
  • Loading branch information
luanfreitasdev committed Sep 24, 2024
1 parent 6ced65c commit 096a135
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 51 deletions.
4 changes: 0 additions & 4 deletions src/Concerns/HasActions.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,6 @@ public function prepareActionRulesForRows(mixed $row, ?object $loop = null): arr

public function resolveActionRules(mixed $row): array
{
if (!method_exists($this, 'actionRules')) {
return [];
}

return collect($this->actionRules($row)) // @phpstan-ignore-line
->transform(function ($rule) use ($row) {
$when = data_get($rule, 'rule.when');
Expand Down
91 changes: 44 additions & 47 deletions src/DataSource/Processors/DataSourceBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use Illuminate\Support\{Collection as BaseCollection, Str};
use Illuminate\View\Concerns\ManagesLoops;
use Laravel\Scout\Builder as ScoutBuilder;
use PowerComponents\LivewirePowerGrid\{Button, Column, Concerns\SoftDeletes, ManageLoops, PowerGridComponent};
use PowerComponents\LivewirePowerGrid\{Column, Concerns\SoftDeletes, ManageLoops, PowerGridComponent};

class DataSourceBase
{
Expand Down Expand Up @@ -114,7 +114,8 @@ public static function transform(

private static function processRows(BaseCollection $results, PowerGridComponent $component): BaseCollection
{
$fields = collect($component->fields()->fields);
$fields = collect($component->fields()->fields);
$cookiePrefix = 'pg_cookie_' . $component->tableName . '_row_';

if ($component->paginateRaw) {
$results = collect((array) data_get($results, 'hits'))->pluck('document');
Expand All @@ -123,73 +124,59 @@ private static function processRows(BaseCollection $results, PowerGridComponent
$loopInstance = app(ManageLoops::class);
$loopInstance->addLoop($results);

$renderActions = false;
$renderActions = method_exists($component, 'actions');
$renderActionRules = method_exists($component, 'actionRules');
$actionsHtml = &static::$actionsHtml;

if (method_exists($component, 'actions')) {
$renderActions = true;
}

return $results->map(function ($row, $index) use ($component, $fields, $loopInstance, $renderActions) {
$data = $fields->map(fn ($field) => $field((object) $row, $index));

$loopInstance->incrementLoopIndices();
return $results->map(function ($row, $index) use ($component, $fields, $loopInstance, $renderActions, $renderActionRules, $cookiePrefix, &$actionsHtml) {
$row = (object) $row;
$data = $fields->map(fn ($field) => $field($row, $index));

$rowId = data_get($row, $component->realPrimaryKey);

$hasCookieActionsForRow = isset($_COOKIE['pg_cookie_' . $component->tableName . '_row_' . $rowId]);
$rowId = data_get($row, $component->realPrimaryKey);
$hasCookieActionsForRow = isset($_COOKIE[$cookiePrefix . $rowId]);

if ($renderActions && !$hasCookieActionsForRow) {
try {
$actions = collect($component->actions((object) $row)) // @phpstan-ignore-line
->transform(function (Button|array $action) use ($row, $component) {
/** @var bool|\Closure $can */
$actions = collect($component->actions($row)) // @phpstan-ignore-line
->map(function ($action) use ($row, $component, $renderActionRules) {
$can = data_get($action, 'can');

if ($can instanceof \Closure) {
$allowed = $can($row);
}

return [
'action' => data_get($action, 'action'),
'can' => $allowed ?? $can,
'can' => $can instanceof \Closure ? $can($row) : $can,
'slot' => data_get($action, 'slot'),
'tag' => data_get($action, 'tag'),
'icon' => data_get($action, 'icon'),
'iconAttributes' => data_get($action, 'iconAttributes'),
'attributes' => data_get($action, 'attributes'),
'rules' => $component->resolveActionRules($row),
'rules' => $renderActionRules ? $component->resolveActionRules($row) : [],
];
});

static::$actionsHtml[$rowId] = $actions->toArray();
$actionsHtml[$rowId] = $actions->toArray();
} catch (\ArgumentCountError $exception) {
$trace = $exception->getTrace();

if (str(strval(data_get($trace, '0.file')))->contains('Macroable')) {
$file = str(
data_get($trace, '1.file') . ':' . data_get($trace, '1.line')
)->after(base_path() . DIRECTORY_SEPARATOR);

$method = strval(data_get($trace, '1.args.0'));

throw new \Exception("ArgumentCountError - method: [{$method}] - file: [{$file}]");
}
static::throwArgumentCountError($exception); // @phpstan-ignore-line
}
}

$mergedData = $data->merge([
'__powergrid_loop' => $loop = $loopInstance->getLastLoop(),
'__powergrid_rules' => $component->prepareActionRulesForRows($row, $loop),
]);

if ($component->supportModel && $row instanceof Model) {
return (object) tap($row)->forceFill($mergedData->toArray());
}

return (object) $mergedData->toArray();
return tap($data->merge([
'__powergrid_loop' => $loopInstance->getLastLoop(),
'__powergrid_rules' => $component->prepareActionRulesForRows($row, $loopInstance->getLastLoop()),
]), function () use ($loopInstance) {
$loopInstance->incrementLoopIndices();
})->pipe(function ($mergedData) use ($component, $row) {
return $component->supportModel && $row instanceof Model // @phpstan-ignore-line
? (object) tap($row)->forceFill($mergedData->toArray()) // @phpstan-ignore-line
: (object) $mergedData->toArray();
});
});
}

public function transformTime(): float
{
return self::$transformTime;
}

protected function setCurrentTable(mixed $datasource): void
{
if ($datasource instanceof QueryBuilder) {
Expand Down Expand Up @@ -269,8 +256,18 @@ protected function applySummaries(MorphToMany|EloquentBuilder|BaseCollection|Que
})->toArray();
}

public function transformTime(): float
private static function throwArgumentCountError(\Exception|\ArgumentCountError $exception): void
{
return self::$transformTime;
$trace = $exception->getTrace();

if (str(strval(data_get($trace, '0.file')))->contains('Macroable')) {
$file = str(
data_get($trace, '1.file') . ':' . data_get($trace, '1.line')
)->after(base_path() . DIRECTORY_SEPARATOR);

$method = strval(data_get($trace, '1.args.0'));

throw new \Exception("ArgumentCountError - method: [{$method}] - file: [{$file}]");
}
}
}

0 comments on commit 096a135

Please sign in to comment.