Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/eager-load-assets' into eager-lo…
Browse files Browse the repository at this point in the history
…ad-assets
  • Loading branch information
markhuot committed Nov 17, 2023
2 parents 1bbaa6d + 3363d9a commit cfada64
Showing 1 changed file with 33 additions and 28 deletions.
61 changes: 33 additions & 28 deletions src/models/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Component extends ActiveRecord
const AFTER_POPULATE_TREE = 'afterPopulateTree';

/** @var array<Component> */
protected ?array $slotted = null;
protected ?Collection $slotted = null;

protected array $context = [];

Expand Down Expand Up @@ -136,7 +136,7 @@ public static function tableName()
/**
* @param array<Component> $components
*/
public function setSlotted(array $components): self
public function setSlotted(Collection $components): self
{
$this->slotted = $components;

Expand All @@ -152,9 +152,9 @@ public function afterPopulateTree(Collection $components)
}

/**
* @return array<Component>|null
* @return Collection<Component>|null
*/
public function getSlotted(): ?array
public function getSlotted(): ?Collection
{
return $this->slotted;
}
Expand Down Expand Up @@ -262,6 +262,11 @@ public function __toString(): string
return $html;
}

public function isDiscendantOf(Component $component, string $slotName = null): bool
{
return str_starts_with($this->path ?? '', $component->getChildPath() ?? '');
}

public function isDirectDiscendantOf(Component $component, string $slotName = null): bool
{
return $component->getChildPath() === $this->path && $slotName === $this->slot;
Expand Down Expand Up @@ -297,35 +302,35 @@ public function getSlot(string $name = null): SlotCollection
->collect();

$this->afterPopulateTree($components);
$this->setSlotted($components->all());
$this->setSlotted($components);
}

$components = collect($this->slotted)
$components = ($this->slotted ?? collect())
->where(fn (Component $component) => $component->isDirectDiscendantOf($this, $name))
->each(function (Component $component) {
$components = collect($this->slotted)
->where(fn (Component $c) => str_starts_with($c->path ?? '', $component->getChildPath() ?? ''))
->all();

$component->setSlotted($components);
});

// As we delve through the render tree pass some state around so we know
// where each child is rendering and can act accordingly. For example,
//
// 1. we set pass the context down so if a section sets a context of "bg: blue"
// then any child components will also see that same context.
// 2. set the render parent so child components know who is initiating
// the rendering. This allows us to affect children based on their
// parent tree.
$components = $components
->each(fn (Component $component) => $component
->mergeContext($this->context)
->setRenderParent($this)
)
->values()->all();

return new SlotCollection($components, $this, $name);
->where(fn (Component $c) => $c->isDiscendantOf($component));

$component->setSlotted($components)

// As we delve through the render tree pass some state around so we know
// where each child is rendering and can act accordingly. For example,
//
// 1. we set pass the context down so if a section sets a context of "bg: blue"
// then any child components will also see that same context.
// 2. set the render parent so child components know who is initiating
// the rendering. This allows us to affect children based on their
// parent tree.
->mergeContext($this->context)
->setRenderParent($this);
})

// re-key components so they are indexed sequentially since the ->where
// call above may have removed some of the keys.
->values();


return new SlotCollection($components->all(), $this, $name);
}

public function getChildPath(): ?string
Expand Down

0 comments on commit cfada64

Please sign in to comment.