Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions app/NativeComponents/CommitDetail.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Devnull\Data\CommitThread;
use App\Devnull\Data\Page;
use App\Devnull\Display;
use App\Devnull\RowCache;
use App\Devnull\Stash;
use App\Devnull\Timeline;
use App\Icons\Lucide;
Expand Down Expand Up @@ -102,6 +103,11 @@ class CommitDetail extends NativeComponent
*/
public int $scrollKey = 0;

/**
* The replies from the last frame, so a render only builds the replies that changed, as the Log does its rows.
*/
private ?RowCache $replyRows = null;

/**
* Every frame goes in full, as on the Log (see Log::$forceFullFrames). A
* tap on the reply field mounts the thread's scroll view again under a new
Expand Down Expand Up @@ -389,8 +395,11 @@ public function render(): View
{
$earlier = $this->earlierShown ? [] : $this->ancestors;
$viewer = $this->me();
$this->replyRows ??= new RowCache;
$this->replyRows->startFrame();

return view('native.commit-detail', [
'replyRows' => $this->replyRows,
'myColour' => $this->myColour(),
'viewer' => $viewer,
'stashed' => app(Stash::class)->names($viewer?->id),
Expand Down
23 changes: 21 additions & 2 deletions app/NativeComponents/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use App\Devnull\Data\User;
use App\Devnull\ErrorCode;
use App\Devnull\Exceptions\ApiException;
use App\Devnull\RowCache;
use App\Devnull\ShownTabs;
use App\Devnull\Stash;
use App\Devnull\Timeline;
Expand Down Expand Up @@ -88,9 +89,10 @@ class Profile extends NativeComponent
public const array Labels = ['commits' => 'Commits', 'replies' => 'Replies'];

/**
* How many kept commits the tab you're not on shows, enough to fill the screen while you swipe to it.
* How many kept commits the tab you're not on shows, enough for the top of the screen while you swipe to it, as
* Log::PreviewCommits: every row sent is one the phone builds.
*/
public const int PreviewCommits = 5;
public const int PreviewCommits = 3;

/**
* The alerts the safety menu confirms with, as the web words them. Each one's id names the profile too, so an
Expand Down Expand Up @@ -121,6 +123,13 @@ class Profile extends NativeComponent
*/
public array $listKeys = [];

/**
* Each tab's rows from the last frame, so a render only builds the rows that changed, as on the Log.
*
* @var array<string, RowCache>
*/
private array $rowCaches = [];

/**
* Whether the tab has had commits to show, kept or fresh. Until then it shows skeleton rows.
*/
Expand Down Expand Up @@ -749,6 +758,10 @@ private function commitsQuery(?string $tab = null): array

public function render(): View
{
foreach (self::Tabs as $tab) {
$this->rowCache($tab)->startFrame();
}

$pages = array_map(fn (string $tab): array => $tab === $this->tab
? $this->page($tab, $this->commits, $this->nextCursor, $this->commitsLoaded, $this->listKey)
: $this->previewPage($tab), self::Tabs);
Expand Down Expand Up @@ -798,6 +811,7 @@ private function page(string $tab, array $commits, ?string $nextCursor, bool $lo

return [
'tab' => $tab,
'rowCache' => $this->rowCache($tab),
'showing' => $showing,
'listKey' => $listKey,
'loaded' => $loaded,
Expand All @@ -810,4 +824,9 @@ private function page(string $tab, array $commits, ?string $nextCursor, bool $lo
'threads' => $showing ? $this->threads : [],
];
}

private function rowCache(string $tab): RowCache
{
return $this->rowCaches[$tab] ??= new RowCache;
}
}
9 changes: 9 additions & 0 deletions app/NativeComponents/Starred.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Devnull\Data\Commit;
use App\Devnull\Data\Page;
use App\Devnull\ResponseCache;
use App\Devnull\RowCache;
use App\Devnull\ShownTabs;
use App\Devnull\Stash;
use App\Devnull\Timeline;
Expand Down Expand Up @@ -57,6 +58,11 @@ class Starred extends NativeComponent
*/
public bool $loaded = false;

/**
* The rows from the last frame, so a render only builds the rows that changed, as on the Log.
*/
private ?RowCache $rowCache = null;

public function mount(): void
{
$this->showKept();
Expand Down Expand Up @@ -235,8 +241,11 @@ public function render(): View
{
$lastCommit = $this->commits[array_key_last($this->commits) ?? -1] ?? null;
$viewer = $this->me();
$this->rowCache ??= new RowCache;
$this->rowCache->startFrame();

return view('native.starred', [
'rowCache' => $this->rowCache,
'viewer' => $viewer,
'stashed' => app(Stash::class)->names($viewer?->id),
'myColour' => $this->myColour(),
Expand Down
8 changes: 6 additions & 2 deletions resources/views/native/commit-detail.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
@foreach ($replies as $index => $threadReply)
@php($nested = $tombstoneReplies[$threadReply->id] ?? [])
@php($hasNext = ! $loop->last || $repliesCursor !== null)
@include('native.partials.thread-reply', [
@php($replyRow = [
'reply' => $threadReply,
'lane' => $commit->lane(),
'previousLane' => $index === 0 ? null : $replies[$index - 1]->lane(),
Expand All @@ -64,8 +64,10 @@
'more' => $loop->last && $nested === [] && $repliesCursor !== null ? 'loadMoreReplies' : null,
'last' => $loop->last && $nested === [],
])
@php($rowScope = array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))
@php($replyRows->include('reply-'.$threadReply->id, \App\Devnull\RowInputs::for($threadReply, $rowScope), 'native.partials.thread-reply', $replyRow, $rowScope))
@foreach ($nested as $nestedIndex => $nestedReply)
@include('native.partials.thread-reply', [
@php($nestedRow = [
'reply' => $nestedReply,
'lane' => \App\Devnull\Lane::Rail,
'previousLane' => $nestedIndex === 0 ? null : $nested[$nestedIndex - 1]->lane(),
Expand All @@ -78,6 +80,8 @@
'more' => $loop->last && $loop->parent->last && $repliesCursor !== null ? 'loadMoreReplies' : null,
'last' => $loop->last && $loop->parent->last,
])
@php($rowScope = array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))
@php($replyRows->include('reply-'.$nestedReply->id, \App\Devnull\RowInputs::for($nestedReply, $rowScope), 'native.partials.thread-reply', $nestedRow, $rowScope))
@endforeach
@endforeach
@endif
Expand Down
2 changes: 1 addition & 1 deletion resources/views/native/partials/profile-page.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class="w-full flex-1 bg-theme-surface"
@include('native.partials.commit-row', ['commit' => $page['pinnedCommit'], 'previousLane' => null, 'continuesRun' => false, 'rowKey' => 'pinned-'.$page['pinnedCommit']->id])
@endif

@include('native.partials.feed', ['rows' => $page['rows'], 'threads' => $page['threads']])
@include('native.partials.feed', ['rows' => $page['rows'], 'threads' => $page['threads'], 'rowCache' => $page['rowCache']])

@if (! $page['isEmpty'] || $page['pinnedCommit'])
@if ($page['nextCursor'] !== null)
Expand Down
2 changes: 1 addition & 1 deletion resources/views/native/starred.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
])
@endif

@include('native.partials.feed', ['rows' => $rows])
@include('native.partials.feed', ['rows' => $rows, 'rowCache' => $rowCache])

@if ($lastCommit)
@if ($nextCursor !== null)
Expand Down
96 changes: 96 additions & 0 deletions tests/Feature/Screens/RowCacheScreensTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

use App\NativeComponents\CommitDetail;
use App\NativeComponents\Profile;
use App\NativeComponents\Starred;
use Illuminate\Http\Client\Request;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\View;
use Native\Mobile\Testing\Native;
use Native\Mobile\Testing\TestableComponent;

beforeEach(function () {
config(['devnull.url' => 'http://devnull.test']);
signIn();
});

/**
* The ids of the commit rows and replies that $render builds in Blade, rather than takes from a row cache.
*
* @return list<string>
*/
function rowsBuiltBy(Closure $render): array
{
$built = [];
View::composer(['native.partials.commit-row', 'native.partials.thread-reply'], function ($view) use (&$built) {
$built[] = $view->getData()[$view->name() === 'native.partials.thread-reply' ? 'reply' : 'commit']->id;
});

$render();

return $built;
}

/**
* Draws the screen again with its row cache emptied, and expects the same frame it drew from the rows it kept.
*/
function expectKeptRowsMatchFreshOnes(TestableComponent $screen, string $class, string $cache, mixed $empty): void
{
$kept = $screen->tree();

Closure::bind(fn () => $this->{$cache} = $empty, $screen->instance(), $class)();

expect($screen->call('navTitle')->tree())->toBe($kept);
}

it("keeps a profile's unchanged rows between renders, on both its tabs", function () {
$ada = userJson(['id' => 'u2', 'username' => 'ada', 'name' => 'Ada']);
fakeApi([
'GET /api/v1/users/ada' => [['data' => userWithCountsJson(['id' => 'u2', 'username' => 'ada', 'name' => 'Ada']), 'is_following' => false]],
'GET /api/v1/users/ada/commits' => [pageJson([commitJson(['id' => 'a2', 'author' => $ada]), commitJson(['id' => 'a1', 'body' => 'Older', 'author' => $ada])])],
'POST /api/v1/commits/a1/star' => [['data' => commitJson(['id' => 'a1', 'body' => 'Older', 'author' => $ada, 'stars_count' => 1, 'starred' => true])]],
]);
$screen = Native::visit('/log/users/ada');

expect(rowsBuiltBy(fn () => $screen->call('navTitle')))->toBe([])
->and(rowsBuiltBy(fn () => $screen->tap('star-a1')))->toBe(['a1']);

expectKeptRowsMatchFreshOnes($screen, Profile::class, 'rowCaches', []);
});

it('keeps the unchanged rows of Starred between renders', function () {
fakeApi([
'GET /api/v1/starred' => [pageJson([
commitJson(['id' => 's2', 'starred' => true, 'stars_count' => 1]),
commitJson(['id' => 's1', 'starred' => true, 'stars_count' => 1, 'replies_count' => 1]),
], path: 'http://devnull.test/api/v1/starred')],
'GET /api/v1/commits/s1/replies' => [pageJson([replyJson(['id' => 'r1', 'parent_id' => 's1', 'body' => 'A reply in place'])])],
]);
$screen = finishRefreshes(Native::visit('/profile/starred'));

expect(rowsBuiltBy(fn () => $screen->call('navTitle')))->toBe([])
->and(rowsBuiltBy(fn () => $screen->tap('replies-s1')->assertSee('A reply in place')))->toBe(['s1', 'r1']);

expectKeptRowsMatchFreshOnes($screen, Starred::class, 'rowCache', null);
});

it("keeps a thread's unchanged replies between renders, and only builds the ones a page of replies brings", function () {
Http::fake(function (Request $request) {
$path = parse_url($request->url(), PHP_URL_PATH);

return match (true) {
$path === '/api/v1/commits/c2/replies' => Http::response(str_contains($request->url(), 'cursor=page2')
? pageJson([replyJson(['id' => 'r3', 'parent_id' => 'c2', 'body' => 'Third reply'])])
: pageJson([replyJson(['id' => 'r1', 'parent_id' => 'c2', 'body' => 'First reply']), replyJson(['id' => 'r2', 'parent_id' => 'c2', 'body' => 'Second reply'])], 'page2', 'http://devnull.test/api/v1/commits/c2/replies')),
default => Http::response(commitPageJson(commitJson(['id' => 'c2', 'replies_count' => 3]))),
};
});
$screen = Native::visit('/log/commits/c2')->tap('replies-c2')->assertSee('Second reply');

// The commits above the replies read what the screen shows of the thread, so they're built every time. The last
// reply of a page changes when the next page comes, since it no longer carries "more".
expect(rowsBuiltBy(fn () => $screen->call('navTitle')))->toBe(['c2'])
->and(rowsBuiltBy(fn () => $screen->call('loadMoreReplies')->assertSee('Third reply')))->toBe(['c2', 'r2', 'r3']);

expectKeptRowsMatchFreshOnes($screen, CommitDetail::class, 'replyRows', null);
});
Loading