Skip to content

Commit

Permalink
Use DeferProp
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiasweiss committed Jan 24, 2025
1 parent 3365346 commit 4a1ccd2
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 14 deletions.
3 changes: 2 additions & 1 deletion src/Lazy.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Closure;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Traits\Macroable;
use Inertia\DeferProp;
use Spatie\LaravelData\Support\Lazy\ClosureLazy;
use Spatie\LaravelData\Support\Lazy\ConditionalLazy;
use Spatie\LaravelData\Support\Lazy\DefaultLazy;
Expand Down Expand Up @@ -38,7 +39,7 @@ public static function inertia(Closure $value): InertiaLazy
return new InertiaLazy($value);
}

public static function inertiaDeferred(Closure $value): InertiaDeferred
public static function inertiaDeferred(DeferProp $value): InertiaDeferred
{
return new InertiaDeferred($value);
}
Expand Down
5 changes: 2 additions & 3 deletions src/Support/Lazy/InertiaDeferred.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@

namespace Spatie\LaravelData\Support\Lazy;

use Closure;
use Inertia\DeferProp;

class InertiaDeferred extends ConditionalLazy
{
public function __construct(
Closure $value,
DeferProp $value,
) {
parent::__construct(fn () => true, $value);
parent::__construct(fn () => true, fn () => $value());
}

public function resolve(): DeferProp
Expand Down
21 changes: 12 additions & 9 deletions tests/CreationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use Illuminate\Support\Enumerable;
use Illuminate\Support\Facades\Route;
use Illuminate\Validation\ValidationException;
use Inertia\DeferProp;
use Inertia\Inertia;
use Inertia\LazyProp;

use function Pest\Laravel\postJson;
Expand Down Expand Up @@ -1461,11 +1463,12 @@ class TestAutoLazyClassAttributeData extends Data

public function __construct()
{
$this->deferred = Lazy::inertiaDeferred(fn () => 'Deferred Value');
}
};

$data = $dataClass::from([]);
$data = $dataClass::from([
"deferred" => Lazy::inertiaDeferred(Inertia::defer(fn () => 'Deferred Value')),
]);

expect($data->deferred)->toBeInstanceOf(InertiaDeferred::class);
expect($data->deferred->resolve()())->toBe('Deferred Value');
Expand All @@ -1474,24 +1477,24 @@ public function __construct()
it('can use auto deferred to construct a deferred property', function () {
$dataClass = new class () extends Data {
#[AutoInertiaDeferred]
public string|InertiaDeferred $string;
public InertiaDeferred $string;
};

$data = $dataClass::from(['string' => 'Hello World']);
$data = $dataClass::from(['string' => Lazy::inertiaDeferred(Inertia::defer(fn () => 'Deferred Value'))]);

expect($data->string)->toBeInstanceOf(InertiaDeferred::class);
expect($data->toArray()['string'])->toBeInstanceOf(\Inertia\DeferProp::class);
expect($data->toArray()['string'])->toBeInstanceOf(DeferProp::class);
});

it('can use class level auto deferred to construct a deferred property', function () {
#[AutoInertiaDeferred]
class AutoDeferredData extends Data
{
public string|InertiaDeferred $string;
public InertiaDeferred $deferred;
};

$data = AutoDeferredData::from(['string' => 'Hello World']);
$data = AutoDeferredData::from(['string' => Inertia::defer(fn () => 'Deferred Value')]);

expect($data->string)->toBeInstanceOf(InertiaDeferred::class);
expect($data->toArray()['string'])->toBeInstanceOf(\Inertia\DeferProp::class);
expect($data->deferred)->toBeInstanceOf(InertiaDeferred::class);
expect($data->toArray()['string'])->toBeInstanceOf(DeferProp::class);
})->todo();
3 changes: 2 additions & 1 deletion tests/Resolvers/VisibleDataFieldsResolverTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use Inertia\DeferProp;
use Inertia\Inertia;
use Inertia\LazyProp;
use Spatie\LaravelData\Attributes\DataCollectionOf;
use Spatie\LaravelData\Attributes\Hidden;
Expand Down Expand Up @@ -227,7 +228,7 @@ public function __construct(
public static function create(string $name): static
{
return new self(
Lazy::inertiaDeferred(fn () => $name)
Lazy::inertiaDeferred(Inertia::defer(fn () => $name))
);
}
};
Expand Down

0 comments on commit 4a1ccd2

Please sign in to comment.