diff --git a/app/NativeComponents/CommitDetail.php b/app/NativeComponents/CommitDetail.php index e2032fcf..45c62b55 100644 --- a/app/NativeComponents/CommitDetail.php +++ b/app/NativeComponents/CommitDetail.php @@ -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; @@ -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 @@ -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), diff --git a/app/NativeComponents/Profile.php b/app/NativeComponents/Profile.php index e4ea5918..86d58f3b 100644 --- a/app/NativeComponents/Profile.php +++ b/app/NativeComponents/Profile.php @@ -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; @@ -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 @@ -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 + */ + private array $rowCaches = []; + /** * Whether the tab has had commits to show, kept or fresh. Until then it shows skeleton rows. */ @@ -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); @@ -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, @@ -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; + } } diff --git a/app/NativeComponents/Starred.php b/app/NativeComponents/Starred.php index 84ef7c93..b611ea5e 100644 --- a/app/NativeComponents/Starred.php +++ b/app/NativeComponents/Starred.php @@ -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; @@ -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(); @@ -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(), diff --git a/resources/views/native/commit-detail.blade.php b/resources/views/native/commit-detail.blade.php index 225a1ebf..05ac9da5 100644 --- a/resources/views/native/commit-detail.blade.php +++ b/resources/views/native/commit-detail.blade.php @@ -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(), @@ -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(), @@ -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 diff --git a/resources/views/native/partials/profile-page.blade.php b/resources/views/native/partials/profile-page.blade.php index 87006e8b..9b1c5f66 100644 --- a/resources/views/native/partials/profile-page.blade.php +++ b/resources/views/native/partials/profile-page.blade.php @@ -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) diff --git a/resources/views/native/starred.blade.php b/resources/views/native/starred.blade.php index 32e4d4e7..edc7165e 100644 --- a/resources/views/native/starred.blade.php +++ b/resources/views/native/starred.blade.php @@ -34,7 +34,7 @@ ]) @endif - @include('native.partials.feed', ['rows' => $rows]) + @include('native.partials.feed', ['rows' => $rows, 'rowCache' => $rowCache]) @if ($lastCommit) @if ($nextCursor !== null) diff --git a/tests/Feature/Screens/RowCacheScreensTest.php b/tests/Feature/Screens/RowCacheScreensTest.php new file mode 100644 index 00000000..d0cc2c9d --- /dev/null +++ b/tests/Feature/Screens/RowCacheScreensTest.php @@ -0,0 +1,96 @@ + '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 + */ +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); +});