Skip to content

Commit 9abd930

Browse files
Cleanup
1 parent 42fe33a commit 9abd930

File tree

3 files changed

+92
-123
lines changed

3 files changed

+92
-123
lines changed

docs/advanced-usage/use-with-inertia.md

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ title: Use with Inertia
33
weight: 9
44
---
55

6-
> Inertia.js lets you quickly build modern single-page React, Vue, and Svelte apps using classic server-side routing and controllers.
6+
> Inertia.js lets you quickly build modern single-page React, Vue, and Svelte apps using classic server-side routing and
7+
> controllers.
78
89
Laravel Data works excellent with [Inertia](https://inertiajs.com).
910

@@ -15,10 +16,13 @@ return Inertia::render('Song', SongsData::from($song));
1516

1617
## Lazy properties
1718

18-
This package supports [lazy](https://spatie.be/docs/laravel-data/v4/as-a-resource/lazy-properties) properties, which can be manually included or excluded.
19+
This package supports [lazy](https://spatie.be/docs/laravel-data/v4/as-a-resource/lazy-properties) properties, which can
20+
be manually included or excluded.
1921

20-
Inertia has a similar concept called [lazy data evaluation](https://inertiajs.com/partial-reloads#lazy-data-evaluation), where some properties wrapped in a closure only get evaluated and included in the response when explicitly asked.
21-
Inertia v2 introduced the concept of [deferred props](https://inertiajs.com/deferred-props), which allows to defer the loading of certain data until after the initial page render.
22+
Inertia has a similar concept called [lazy data evaluation](https://inertiajs.com/partial-reloads#lazy-data-evaluation),
23+
where some properties wrapped in a closure only get evaluated and included in the response when explicitly asked.
24+
Inertia v2 introduced the concept of [deferred props](https://inertiajs.com/deferred-props), which allows to defer the
25+
loading of certain data until after the initial page render.
2226

2327
This package can output specific properties as Inertia lazy or deferred props as such:
2428

@@ -57,9 +61,36 @@ router.reload((url, {
5761
});
5862
```
5963
64+
#### Deferred property groups
65+
66+
It is possible to group deferred properties together, so that all grouped properties are loaded at the same time. In
67+
order to achieve this, you can pass a group name as the second argument to `Lazy::inertiaDeferred()`:
68+
69+
```php
70+
class SongData extends Data
71+
{
72+
public function __construct(
73+
public Lazy|string $title,
74+
public Lazy|string $artist,
75+
public Lazy|string $lyrics,
76+
) {
77+
}
78+
79+
public static function fromModel(Song $song): self
80+
{
81+
return new self(
82+
Lazy::inertiaDeferred(fn() => $song->title),
83+
Lazy::inertiaDeferred(fn() => $song->artist, 'details')
84+
Lazy::inertiaDeferred(fn() => $song->lyrics, 'details')
85+
);
86+
}
87+
}
88+
```
89+
6090
### Auto lazy Inertia properties
6191
62-
We already saw earlier that the package can automatically make properties Lazy, the same can be done for Inertia properties.
92+
We already saw earlier that the package can automatically make properties Lazy, the same can be done for Inertia
93+
properties.
6394
6495
It is possible to rewrite the previous example as follows:
6596
@@ -82,7 +113,8 @@ class SongData extends Data
82113
}
83114
```
84115
85-
If all the properties of a class should be either Inertia or closure lazy, you can use the attributes on the class level:
116+
If all the properties of a class should be either Inertia or closure lazy, you can use the attributes on the class
117+
level:
86118
87119
```php
88120
#[AutoInertiaLazy]
@@ -95,3 +127,23 @@ class SongData extends Data
95127
}
96128
}
97129
```
130+
131+
#### Deferred property groups
132+
133+
For the `AutoInertiaDeferred` attribute, it is also possible to specify a group name in order to group deferred
134+
properties together:
135+
136+
```php
137+
class SongData extends Data
138+
{
139+
public function __construct(
140+
#[AutoInertiaDeferred()]
141+
public Lazy|string $title,
142+
#[AutoInertiaDeferred('details')]
143+
public Lazy|string $artist,
144+
#[AutoInertiaDeferred('details')]
145+
public Lazy|string $lyrics,
146+
) {
147+
}
148+
}
149+
```

src/Support/Lazy/InertiaDeferred.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ public function __construct(
2020

2121
parent::__construct(fn () => true, $callback);
2222

23-
$this->group = $value instanceof DeferProp && $value->group()!=='default' ? $value->group() : $group;
23+
$this->group = $value instanceof DeferProp && $value->group() !== 'default'
24+
? $value->group()
25+
: $group;
2426
}
2527

2628
public function resolve(): DeferProp

tests/CreationTest.php

Lines changed: 31 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -1502,70 +1502,100 @@ public function __construct(
15021502
$dataClass = new class () extends Data {
15031503
public InertiaDeferred|string $deferred;
15041504

1505+
public InertiaDeferred|string $deferredWithGroup;
1506+
15051507
public function __construct()
15061508
{
15071509
}
15081510
};
15091511

15101512
$data = $dataClass::from([
15111513
"deferred" => Lazy::inertiaDeferred(Inertia::defer(fn () => 'Deferred Value')),
1514+
"deferredWithGroup" => Lazy::inertiaDeferred(Inertia::defer(fn () => 'Deferred Value', 'deferred-group')),
15121515
]);
15131516

15141517
expect($data->deferred)->toBeInstanceOf(InertiaDeferred::class);
15151518
expect($data->all()['deferred'])->toBeInstanceOf(DeferProp::class);
15161519
expect($data->all()['deferred']())->toBe('Deferred Value');
15171520

1521+
expect($data->deferredWithGroup)->toBeInstanceOf(InertiaDeferred::class);
1522+
expect($data->all()['deferredWithGroup'])->toBeInstanceOf(DeferProp::class);
1523+
expect($data->all()['deferredWithGroup']())->toBe('Deferred Value');
1524+
expect($data->all()['deferredWithGroup']->group())->toBe('deferred-group');
15181525
});
15191526

15201527
it('can create a data object with inertia deferred closure', function () {
15211528
$dataClass = new class () extends Data {
15221529
public InertiaDeferred|string $deferred;
15231530

1531+
public InertiaDeferred|string $deferredWithGroup;
1532+
15241533
public function __construct()
15251534
{
15261535
}
15271536
};
15281537

15291538
$data = $dataClass::from([
15301539
"deferred" => Lazy::inertiaDeferred(fn () => 'Deferred Value'),
1540+
"deferredWithGroup" => Lazy::inertiaDeferred(fn () => 'Deferred Value', 'deferred-group'),
15311541
]);
15321542

15331543
expect($data->deferred)->toBeInstanceOf(InertiaDeferred::class);
15341544
expect($data->all()['deferred'])->toBeInstanceOf(DeferProp::class);
15351545
expect($data->all()['deferred']())->toBe('Deferred Value');
15361546

1547+
expect($data->deferredWithGroup)->toBeInstanceOf(InertiaDeferred::class);
1548+
expect($data->all()['deferredWithGroup'])->toBeInstanceOf(DeferProp::class);
1549+
expect($data->all()['deferredWithGroup']())->toBe('Deferred Value');
1550+
expect($data->all()['deferredWithGroup']->group())->toBe('deferred-group');
1551+
15371552
});
15381553

15391554
it('can create a data object with inertia deferred value', function () {
15401555
$dataClass = new class () extends Data {
15411556
public InertiaDeferred|string $deferred;
15421557

1558+
public InertiaDeferred|string $deferredWithGroup;
1559+
15431560
public function __construct()
15441561
{
15451562
}
15461563
};
15471564

15481565
$data = $dataClass::from([
15491566
"deferred" => Lazy::inertiaDeferred('Deferred Value'),
1567+
"deferredWithGroup" => Lazy::inertiaDeferred('Deferred Value', 'deferred-group'),
15501568
]);
15511569

15521570
expect($data->deferred)->toBeInstanceOf(InertiaDeferred::class);
15531571
expect($data->all()['deferred'])->toBeInstanceOf(DeferProp::class);
15541572
expect($data->all()['deferred']())->toBe('Deferred Value');
15551573

1574+
expect($data->deferredWithGroup)->toBeInstanceOf(InertiaDeferred::class);
1575+
expect($data->all()['deferredWithGroup'])->toBeInstanceOf(DeferProp::class);
1576+
expect($data->all()['deferredWithGroup']())->toBe('Deferred Value');
1577+
expect($data->all()['deferredWithGroup']->group())->toBe('deferred-group');
15561578
});
15571579

15581580
it('can use auto deferred to construct a inertia deferred property', function () {
15591581
$dataClass = new class () extends Data {
15601582
#[AutoInertiaDeferred]
15611583
public InertiaDeferred|string $string;
1584+
1585+
#[AutoInertiaDeferred('deferred-group')]
1586+
public InertiaDeferred|string $deferredWithGroup;
15621587
};
15631588

1564-
$data = $dataClass::from(['string' => 'Deferred Value']);
1589+
$data = $dataClass::from(['string' => 'Deferred Value', 'deferredWithGroup' => 'Deferred Value']);
15651590

15661591
expect($data->string)->toBeInstanceOf(InertiaDeferred::class);
15671592
expect($data->all()['string'])->toBeInstanceOf(DeferProp::class);
15681593
expect($data->all()['string']())->toBe('Deferred Value');
1594+
1595+
expect($data->deferredWithGroup)->toBeInstanceOf(InertiaDeferred::class);
1596+
expect($data->all()['deferredWithGroup'])->toBeInstanceOf(DeferProp::class);
1597+
expect($data->all()['deferredWithGroup']())->toBe('Deferred Value');
1598+
expect($data->all()['deferredWithGroup']->group())->toBe('deferred-group');
15691599
});
15701600

15711601
it('can use class level auto deferred to construct a inertia deferred property', function () {
@@ -1575,128 +1605,13 @@ class AutoDeferredData extends Data
15751605
public InertiaDeferred|string $string;
15761606
}
15771607

1578-
;
1579-
15801608
$data = AutoDeferredData::from(['string' => 'Deferred Value']);
15811609

15821610
expect($data->string)->toBeInstanceOf(InertiaDeferred::class);
15831611
expect($data->all()['string'])->toBeInstanceOf(DeferProp::class);
15841612
expect($data->all()['string']())->toBe('Deferred Value');
15851613
});
15861614

1587-
it('can create a data object with inertia deferred properties with a group as Inertia::defer argument', function () {
1588-
$dataClass = new class () extends Data {
1589-
public InertiaDeferred|string $deferredWithGroup;
1590-
1591-
public function __construct()
1592-
{
1593-
}
1594-
};
1595-
1596-
$data = $dataClass::from([
1597-
"deferredWithGroup" => Lazy::inertiaDeferred(Inertia::defer(fn () => 'Deferred Value', 'deferred-group')),
1598-
]);
1599-
1600-
expect($data->deferredWithGroup)->toBeInstanceOf(InertiaDeferred::class);
1601-
expect($data->all()['deferredWithGroup'])->toBeInstanceOf(DeferProp::class);
1602-
expect($data->all()['deferredWithGroup']())->toBe('Deferred Value');
1603-
expect($data->all()['deferredWithGroup']->group())->toBe('deferred-group');
1604-
1605-
});
1606-
1607-
it('can create a data object with inertia deferred properties with a group as Lazy::inertiaDeferred argument', function () {
1608-
$dataClass = new class () extends Data {
1609-
public InertiaDeferred|string $deferredWithGroup;
1610-
1611-
public function __construct()
1612-
{
1613-
}
1614-
};
1615-
1616-
$data = $dataClass::from([
1617-
"deferredWithGroup" => Lazy::inertiaDeferred(Inertia::defer(fn () => 'Deferred Value'), 'deferred-group'),
1618-
]);
1619-
1620-
expect($data->deferredWithGroup)->toBeInstanceOf(InertiaDeferred::class);
1621-
expect($data->all()['deferredWithGroup'])->toBeInstanceOf(DeferProp::class);
1622-
expect($data->all()['deferredWithGroup']())->toBe('Deferred Value');
1623-
expect($data->all()['deferredWithGroup']->group())->toBe('deferred-group');
1624-
1625-
});
1626-
1627-
it('can create a data object with inertia deferred closure with a group', function () {
1628-
$dataClass = new class () extends Data {
1629-
public InertiaDeferred|string $deferredWithGroup;
1630-
1631-
public function __construct()
1632-
{
1633-
}
1634-
};
1635-
1636-
$data = $dataClass::from([
1637-
"deferredWithGroup" => Lazy::inertiaDeferred(fn () => 'Deferred Value', 'deferred-group'),
1638-
]);
1639-
1640-
expect($data->deferredWithGroup)->toBeInstanceOf(InertiaDeferred::class);
1641-
expect($data->all()['deferredWithGroup'])->toBeInstanceOf(DeferProp::class);
1642-
expect($data->all()['deferredWithGroup']())->toBe('Deferred Value');
1643-
expect($data->all()['deferredWithGroup']->group())->toBe('deferred-group');
1644-
1645-
});
1646-
1647-
it('can create a data object with inertia deferred value with a group', function () {
1648-
$dataClass = new class () extends Data {
1649-
public InertiaDeferred|string $deferredWithGroup;
1650-
1651-
public function __construct()
1652-
{
1653-
}
1654-
};
1655-
1656-
$data = $dataClass::from([
1657-
"deferredWithGroup" => Lazy::inertiaDeferred('Deferred Value', 'deferred-group'),
1658-
]);
1659-
1660-
expect($data->deferredWithGroup)->toBeInstanceOf(InertiaDeferred::class);
1661-
expect($data->all()['deferredWithGroup'])->toBeInstanceOf(DeferProp::class);
1662-
expect($data->all()['deferredWithGroup']())->toBe('Deferred Value');
1663-
expect($data->all()['deferredWithGroup']->group())->toBe('deferred-group');
1664-
1665-
});
1666-
1667-
it('can use auto deferred to construct a inertia deferred property with a group', function () {
1668-
$dataClass = new class () extends Data {
1669-
#[AutoInertiaDeferred('deferred-group')]
1670-
public InertiaDeferred|string $deferredWithGroup;
1671-
};
1672-
1673-
$data = $dataClass::from(['deferredWithGroup' => 'Deferred Value']);
1674-
1675-
expect($data->deferredWithGroup)->toBeInstanceOf(InertiaDeferred::class);
1676-
expect($data->all()['deferredWithGroup'])->toBeInstanceOf(DeferProp::class);
1677-
expect($data->all()['deferredWithGroup']())->toBe('Deferred Value');
1678-
expect($data->all()['deferredWithGroup']->group())->toBe('deferred-group');
1679-
1680-
});
1681-
1682-
it('can use class level auto deferred to construct a inertia deferred property with a group', function () {
1683-
#[AutoInertiaDeferred('deferred-group')]
1684-
class AutoDeferredDataWithGroup extends Data
1685-
{
1686-
public InertiaDeferred|string $deferredWithGroup;
1687-
}
1688-
1689-
;
1690-
1691-
$data = AutoDeferredDataWithGroup::from(['deferredWithGroup' => 'Deferred Value']);
1692-
1693-
expect($data->deferredWithGroup)->toBeInstanceOf(InertiaDeferred::class);
1694-
expect($data->all()['deferredWithGroup'])->toBeInstanceOf(DeferProp::class);
1695-
expect($data->all()['deferredWithGroup']())->toBe('Deferred Value');
1696-
expect($data->all()['deferredWithGroup']->group())->toBe('deferred-group');
1697-
1698-
});
1699-
17001615
describe('property-morphable creation tests', function () {
17011616
enum TestPropertyMorphableEnum: string
17021617
{

0 commit comments

Comments
 (0)