Skip to content
This repository has been archived by the owner on Feb 27, 2024. It is now read-only.

Commit

Permalink
Merge pull request #17 from protonemedia/resolve-once
Browse files Browse the repository at this point in the history
Resolve Component data only once and late
  • Loading branch information
pascalbaljet authored Dec 21, 2023
2 parents e205c99 + 3afc5ab commit c4b3de9
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 8 deletions.
24 changes: 21 additions & 3 deletions src/BladeViewExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,23 +161,41 @@ protected function attributesAreCustomBound(): bool
*/
protected function getBladeFunctions(): array
{
return $this->data['spladeBridge']['functions'] ?? [];
$functions = $this->data['spladeBridge']['functions'] ?? [];

if ($functions instanceof ResolveOnce) {
$functions = $functions();
}

return $functions;
}

/**
* Get the properties that are passed from the Blade Component.
*/
protected function getBladeProperties(): array
{
return array_keys($this->data['spladeBridge']['data'] ?? []);
$data = $this->data['spladeBridge']['data'] ?? [];

if ($data instanceof ResolveOnce) {
$data = $data();
}

return array_keys($data);
}

/**
* Get the properties that are passed from the Blade Component.
*/
protected function getBladePropsThatArePassedAsVueProps(): array
{
return $this->data['spladeBridge']['props'] ?? [];
$props = $this->data['spladeBridge']['props'] ?? [];

if ($props instanceof ResolveOnce) {
$props = $props();
}

return $props;
}

/**
Expand Down
40 changes: 40 additions & 0 deletions src/ResolveOnce.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace ProtoneMedia\SpladeCore;

use Illuminate\Container\Container;

class ResolveOnce
{
private bool $resolved = false;

private $result;

public function __construct(
private $callback
) {
//
}

public static function make(callable $callback): self
{
return new static($callback);
}

private function resolve(): array
{
if ($this->resolved) {
return $this->result;
}

return tap(Container::getInstance()->call($this->callback), function ($result) {
$this->result = $result;
$this->resolved = true;
});
}

public function __invoke(): array
{
return $this->resolve();
}
}
18 changes: 13 additions & 5 deletions src/View/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,17 +116,25 @@ public function renderComponent()

$output = parent::renderComponent();

$templateId = $componentData['spladeBridge']['template_hash'];
$spladeBridge = $componentData['spladeBridge'];

$templateId = $spladeBridge['template_hash'];

if (static::$trackSpladeComponents) {
static::$spladeComponents[$templateId] = $output;
}

$this->pushSpladeTemplate($templateId, $output);

$spladeBridge = Js::from($componentData['spladeBridge'])->toHtml();
foreach (['data', 'props', 'functions'] as $key) {
if (is_callable($spladeBridge[$key])) {
$spladeBridge[$key] = $spladeBridge[$key]();
}
}

$spladeBridgeHtml = Js::from($spladeBridge)->toHtml();

collect($componentData['spladeBridge']['props'])->each(function ($specs, $key) use ($attributes) {
collect($spladeBridge['props'])->each(function ($specs, $key) use ($attributes) {
if (! str_starts_with($key, 'v-bind:')) {
$key = 'v-bind:'.Str::kebab($key);
}
Expand All @@ -137,7 +145,7 @@ public function renderComponent()
$attrs = $attributes->toHtml();

return static::$trackSpladeComponents
? "<!--splade-template-id=\"{$templateId}\"--><generic-splade-component {$attrs} :bridge=\"{$spladeBridge}\"></generic-splade-component>"
: "<generic-splade-component {$attrs} :bridge=\"{$spladeBridge}\"></generic-splade-component>";
? "<!--splade-template-id=\"{$templateId}\"--><generic-splade-component {$attrs} :bridge=\"{$spladeBridgeHtml}\"></generic-splade-component>"
: "<generic-splade-component {$attrs} :bridge=\"{$spladeBridgeHtml}\"></generic-splade-component>";
}
}

0 comments on commit c4b3de9

Please sign in to comment.