Skip to content

Commit 95434d6

Browse files
committed
mergeable props
1 parent 336809f commit 95434d6

File tree

7 files changed

+101
-11
lines changed

7 files changed

+101
-11
lines changed

src/DeferProp.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@
44

55
use Illuminate\Support\Facades\App;
66

7-
class DeferProp implements IgnoreFirstLoad
7+
class DeferProp implements IgnoreFirstLoad, Mergeable
88
{
9+
use MergesProps;
10+
911
protected $callback;
1012

1113
protected $group;
1214

15+
protected $merge = false;
16+
1317
public function __construct(callable $callback, ?string $group = null)
1418
{
1519
$this->callback = $callback;

src/MergeProp.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;
4+
5+
use Illuminate\Support\Facades\App;
6+
7+
class MergeProp implements Mergeable
8+
{
9+
use MergesProps;
10+
11+
/** @var mixed */
12+
protected $value;
13+
14+
/**
15+
* @param mixed $value
16+
*/
17+
public function __construct($value)
18+
{
19+
$this->value = $value;
20+
$this->merge = true;
21+
}
22+
23+
public function __invoke()
24+
{
25+
return is_callable($this->value) ? App::call($this->value) : $this->value;
26+
}
27+
}

src/Mergeable.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Inertia;
4+
5+
interface Mergeable
6+
{
7+
public function merge();
8+
9+
public function shouldMerge();
10+
}

src/MergesProps.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Inertia;
4+
5+
6+
trait MergesProps
7+
{
8+
protected $merge = false;
9+
10+
public function merge()
11+
{
12+
$this->merge = true;
13+
14+
return $this;
15+
}
16+
17+
public function shouldMerge()
18+
{
19+
return $this->merge;
20+
}
21+
}

src/Response.php

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,9 @@ public function resolveProperties(Request $request, array $props): array
115115
{
116116
$isPartial = $this->isPartial($request);
117117

118-
if (! $isPartial) {
118+
if (!$isPartial) {
119119
$props = array_filter($this->props, static function ($prop) {
120-
return ! ($prop instanceof IgnoreFirstLoad);
120+
return !($prop instanceof IgnoreFirstLoad);
121121
});
122122
}
123123

@@ -218,6 +218,7 @@ public function resolvePropertyInstances(array $props, Request $request): array
218218
OptionalProp::class,
219219
DeferProp::class,
220220
AlwaysProp::class,
221+
MergeProp::class,
221222
WhenVisible::class,
222223
])->first(fn ($class) => $value instanceof $class);
223224

@@ -248,13 +249,34 @@ public function resolvePropertyInstances(array $props, Request $request): array
248249
*/
249250
public function resolveMeta(Request $request): array
250251
{
251-
$meta = [
252+
return array_merge([
252253
'assetVersion' => $this->version,
253254
'clearHistory' => $this->clearHistory,
254-
];
255+
], $this->resolveMergeProps($request), $this->resolveDeferredProps($request));
256+
}
257+
258+
public function resolveMergeProps(Request $request): array
259+
{
260+
$resetProps = collect(explode(',', $request->header(Header::RESET, '')));
261+
$mergeProps = collect($this->props)
262+
->filter(function ($prop) {
263+
return $prop instanceof Mergeable;
264+
})
265+
->filter(function ($prop) {
266+
return $prop->shouldMerge();
267+
})
268+
->filter(function ($prop, $key) use ($resetProps) {
269+
return !$resetProps->contains($key);
270+
})
271+
->keys();
272+
273+
return $mergeProps->isNotEmpty() ? ['mergeProps' => $mergeProps->toArray()] : [];
274+
}
255275

276+
public function resolveDeferredProps(Request $request): array
277+
{
256278
if ($this->isPartial($request)) {
257-
return $meta;
279+
return [];
258280
}
259281

260282
$deferredProps = collect($this->props)
@@ -271,11 +293,7 @@ public function resolveMeta(Request $request): array
271293
->map
272294
->pluck('key');
273295

274-
if ($deferredProps->isNotEmpty()) {
275-
$meta['deferredProps'] = $deferredProps->toArray();
276-
}
277-
278-
return $meta;
296+
return $deferredProps->isNotEmpty() ? ['deferredProps' => $deferredProps->toArray()] : [];
279297
}
280298

281299
/**

src/ResponseFactory.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,14 @@ public function defer(callable $callback, string $group = 'default'): DeferProp
107107
return new DeferProp($callback, $group);
108108
}
109109

110+
/**
111+
* @param mixed $value
112+
*/
113+
public function merge($value): MergeProp
114+
{
115+
return new MergeProp($value);
116+
}
117+
110118
/**
111119
* @param mixed $value
112120
*/

src/Support/Header.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,6 @@ class Header
1717
public const PARTIAL_ONLY = 'X-Inertia-Partial-Data';
1818

1919
public const PARTIAL_EXCEPT = 'X-Inertia-Partial-Except';
20+
21+
public const RESET = 'X-Inertia-Reset';
2022
}

0 commit comments

Comments
 (0)