Skip to content

Commit 7ad2ffb

Browse files
committed
deferred props
1 parent 2db5eb2 commit 7ad2ffb

File tree

5 files changed

+77
-1
lines changed

5 files changed

+77
-1
lines changed

src/DeferProp.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Inertia;
4+
5+
use Illuminate\Support\Facades\App;
6+
7+
class DeferProp
8+
{
9+
protected $callback;
10+
11+
protected $group;
12+
13+
public function __construct(callable $callback, ?string $group = null)
14+
{
15+
$this->callback = $callback;
16+
$this->group = $group;
17+
}
18+
19+
public function group()
20+
{
21+
return $this->group;
22+
}
23+
24+
public function __invoke()
25+
{
26+
return App::call($this->callback);
27+
}
28+
}

src/Inertia.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
* @method static void version(\Closure|string|null $version)
1313
* @method static string getVersion()
1414
* @method static \Inertia\LazyProp lazy(callable $callback)
15+
* @method static \Inertia\DeferProp defer(callable $callback, string $group = 'default')
1516
* @method static \Inertia\AlwaysProp always(mixed $value)
1617
* @method static \Inertia\Response render(string $component, array|\Illuminate\Contracts\Support\Arrayable $props = [])
1718
* @method static \Symfony\Component\HttpFoundation\Response location(string|\Symfony\Component\HttpFoundation\RedirectResponse $url)

src/Response.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public function resolveProperties(Request $request, array $props): array
114114

115115
if (! $isPartial) {
116116
$props = array_filter($this->props, static function ($prop) {
117-
return ! ($prop instanceof LazyProp);
117+
return ! ($prop instanceof LazyProp) && ! ($prop instanceof DeferProp);
118118
});
119119
}
120120

@@ -132,6 +132,21 @@ public function resolveProperties(Request $request, array $props): array
132132

133133
$props = $this->resolvePropertyInstances($props, $request);
134134

135+
if (! $isPartial) {
136+
$deferredProps = collect($this->props)->filter(function ($prop) {
137+
return $prop instanceof DeferProp;
138+
})->map(function ($prop, $key) {
139+
return [
140+
'key' => $key,
141+
'group' => $prop->group(),
142+
];
143+
})->groupBy('group')->map->pluck('key');
144+
145+
if ($deferredProps->isNotEmpty()) {
146+
$props['deferred'] = $deferredProps->toArray();
147+
}
148+
}
149+
135150
return $props;
136151
}
137152

src/ResponseFactory.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ public function lazy(callable $callback): LazyProp
8787
return new LazyProp($callback);
8888
}
8989

90+
public function defer(callable $callback, string $group = 'default'): DeferProp
91+
{
92+
return new DeferProp($callback, $group);
93+
}
94+
9095
/**
9196
* @param mixed $value
9297
*/

tests/DeferPropTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Inertia\Tests;
4+
5+
use Illuminate\Http\Request;
6+
use Inertia\DeferProp;
7+
8+
class DeferPropTest extends TestCase
9+
{
10+
public function test_can_invoke(): void
11+
{
12+
$deferProp = new DeferProp(function () {
13+
return 'A lazy value';
14+
});
15+
16+
$this->assertSame('A lazy value', $deferProp());
17+
}
18+
19+
public function test_can_resolve_bindings_when_invoked(): void
20+
{
21+
$deferProp = new DeferProp(function (Request $request) {
22+
return $request;
23+
});
24+
25+
$this->assertInstanceOf(Request::class, $deferProp());
26+
}
27+
}

0 commit comments

Comments
 (0)