Skip to content
65 changes: 44 additions & 21 deletions app/Devnull/CachedRow.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
* when none of its ids is already in the frame. Otherwise, and for a row with no
* keyed parent, whose ids would come from the frame's running count, the row
* makes its node afresh.
*
* With EDGE's subtree reuse on, the row goes out as EDGE sends any node: a part
* of it the phone already holds, by id and content hash, is sent as a reuse
* marker and taken from the phone's last tree. So a row that hasn't changed
* costs the frame one marker, and a starred one only the parts the star touched.
*/
final class CachedRow extends Element
{
Expand Down Expand Up @@ -65,45 +70,63 @@ public function toArray(
&& array_intersect_key($this->nodeIds, $emittedIds) === []) {
$emittedIds += $this->nodeIds;

return $this->node;
return self::send($this->node, $lastNodeHashes);
}

$node = $this->row->toArray($registry, $nextId, $parentKeyPath, $indexInParent, $emittedIds, $lastNodeHashes);
// Built against no hashes, the row's node is whole, so it can be kept and sent again in any later frame.
$sentNothing = [];
$node = $this->row->toArray($registry, $nextId, $parentKeyPath, $indexInParent, $emittedIds, $sentNothing);
$ids = [];
self::collectIds($node, $ids);

$this->node = $node;
$this->nodePlace = $place;
$this->nodeRegistry = $registry;
$this->nodeIds = $ids;

if (self::collectFullIds($node, $ids)) {
$this->node = $node;
$this->nodePlace = $place;
$this->nodeRegistry = $registry;
$this->nodeIds = $ids;
} else {
$this->node = null;
return self::send($node, $lastNodeHashes);
}

/**
* A node as this frame sends it: a reuse marker where the phone already holds
* it, by its id and hash, as EDGE's own walk does, and otherwise in full, its
* children each sent the same way, and its hash noted as what the phone holds
* next. With subtree reuse off the hashes start empty every frame, so every
* node goes in full.
*
* @param array<string, mixed> $node
* @param array<int, string> $lastNodeHashes
* @return array<string, mixed>
*/
private static function send(array $node, array &$lastNodeHashes): array
{
if (($lastNodeHashes[$node['id']] ?? null) === $node['_hash']) {
return ['id' => $node['id'], 'type' => $node['type'], 'flags' => 1, '_hash' => $node['_hash']];
}

$lastNodeHashes[$node['id']] = $node['_hash'];

if (isset($node['children'])) {
foreach ($node['children'] as $i => $child) {
$node['children'][$i] = self::send($child, $lastNodeHashes);
}
}

return $node;
}

/**
* Collects a node's ids, and its children's. False when any of them is a
* reuse marker, which only stands for a node the phone got in an earlier frame.
* Collects a node's ids, and its children's.
*
* @param array<string, mixed> $node
* @param array<int, true> $ids
*/
private static function collectFullIds(array $node, array &$ids): bool
private static function collectIds(array $node, array &$ids): void
{
if ((($node['flags'] ?? 0) & 1) !== 0) {
return false;
}

$ids[$node['id']] = true;

foreach ($node['children'] ?? [] as $child) {
if (! self::collectFullIds($child, $ids)) {
return false;
}
self::collectIds($child, $ids);
}

return true;
}
}
9 changes: 0 additions & 9 deletions app/NativeComponents/CommitDetail.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,15 +225,6 @@ class CommitDetail extends NativeComponent
*/
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
* key, and EDGE's subtree reuse sent its unchanged rows as references to
* nodes the phone dropped with the old one, so the thread went blank as
* you started typing.
*/
protected bool $forceFullFrames = true;

public function mount(): void
{
$this->focusReply = $this->data('reply', false) === true;
Expand Down
6 changes: 0 additions & 6 deletions app/NativeComponents/Conversation.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,6 @@ class Conversation extends NativeComponent

public const int PinnedLines = 3;

/**
* Every frame goes in full, as on the Log (see Log::$forceFullFrames): messages come in at the bottom and their
* reactions change in place, which EDGE's subtree reuse would otherwise miss.
*/
protected bool $forceFullFrames = true;

/**
* Who the thread is with, from the route: a username, or an org's id.
*/
Expand Down
8 changes: 0 additions & 8 deletions app/NativeComponents/Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,6 @@ class Log extends NativeComponent
use TalksToDevnull;
use WatchesForCalls;

/**
* Every frame goes in full. EDGE's subtree reuse sometimes refers to rows
* the phone never got once a list has grown or reordered, and the phone
* drops them, which drew the log blank under a pull to refresh. EDGE's own
* answer for a screen whose lists change often is to send it in full.
*/
protected bool $forceFullFrames = true;

public const array Logs = ['following', 'latest', 'trending'];

/**
Expand Down
5 changes: 0 additions & 5 deletions app/NativeComponents/Notifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,6 @@ class Notifications extends NativeComponent
use WatchesForCalls;
use WritesInBackground;

/**
* Every frame goes in full, as on the Log (see Log::$forceFullFrames).
*/
protected bool $forceFullFrames = true;

/**
* How long a group's notifications take to fade in or out, and its
* chevron to turn, in milliseconds: as long as replies take on the Log.
Expand Down
5 changes: 0 additions & 5 deletions app/NativeComponents/Organization.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,6 @@ class Organization extends NativeComponent
use ShowsCommits;
use TalksToDevnull;

/**
* Every frame goes in full, as on a profile (see Profile::$forceFullFrames).
*/
protected bool $forceFullFrames = true;

public const array Tabs = ['commits', 'mentions'];

/**
Expand Down
7 changes: 0 additions & 7 deletions app/NativeComponents/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,6 @@ class Profile extends NativeComponent
use TalksToDevnull;
use WatchesForCalls;

/**
* Every frame goes in full, as on the Log (see Log::$forceFullFrames). A tab
* tapped again goes back to the top in a list with a new key and the same
* rows, which EDGE's subtree reuse would otherwise never send.
*/
protected bool $forceFullFrames = true;

public string $username = '';

public const array Tabs = ['commits', 'replies'];
Expand Down
6 changes: 0 additions & 6 deletions app/NativeComponents/Remotes.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,6 @@ class Remotes extends NativeComponent

public const string LeaveAlert = 'remotes-leave';

/**
* Every frame goes in full, as on the Log (see Log::$forceFullFrames), since a row moves to the top when it
* has a new message.
*/
protected bool $forceFullFrames = true;

/** @var list<ConversationData> */
public array $conversations = [];

Expand Down
5 changes: 0 additions & 5 deletions app/NativeComponents/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,6 @@ class Search extends NativeComponent
*/
public const int ShortestCommitTerm = 3;

/**
* Every frame goes in full, as on Starred, since a new term replaces every row.
*/
protected bool $forceFullFrames = true;

public string $query = '';

/**
Expand Down
5 changes: 0 additions & 5 deletions app/NativeComponents/Starred.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,6 @@ class Starred extends NativeComponent
}
use TalksToDevnull;

/**
* Every frame goes in full, as on the Log (see Log::$forceFullFrames), since rows leave the list from the middle.
*/
protected bool $forceFullFrames = true;

/** @var list<Commit> */
public array $commits = [];

Expand Down
14 changes: 9 additions & 5 deletions tests/Feature/Screens/LogsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@
use App\Devnull\ResponseCache;
use App\Devnull\Timeline;
use App\NativeComponents\CommitDetail;
use App\NativeComponents\Conversation;
use App\NativeComponents\Log;
use App\NativeComponents\Notifications;
use App\NativeComponents\Organization;
use App\NativeComponents\Profile;
use App\NativeComponents\Remotes;
use App\NativeComponents\Search;
use App\NativeComponents\Starred;
use Illuminate\Http\Client\Request;
use Illuminate\Support\Facades\Http;
use Native\Mobile\Events\Async\AsyncTaskFailed;
Expand Down Expand Up @@ -355,13 +360,12 @@ function ada(): array
->and(nodeByRef($screen, 'commit-c1')['id'])->toBe($row);
});

it('sends the Log, Notifications, profiles and commit pages in full every frame, as EDGE advises for lists that change often', function () {
it('sends only what changed on the timeline screens, not every frame in full', function () {
Http::fake(['devnull.test/*' => Http::response(pageJson([]))]);
$sendsInFull = fn (string $screen): bool => (fn (): bool => $this->forceFullFrames)->call(Native::test($screen)->instance());

foreach ([Log::class, Notifications::class, Profile::class, CommitDetail::class] as $screen) {
$instance = Native::test($screen)->instance();

expect((fn (): bool => $this->forceFullFrames)->call($instance))->toBeTrue();
foreach ([Log::class, Profile::class, Organization::class, CommitDetail::class, Starred::class, Notifications::class, Remotes::class, Search::class, Conversation::class] as $screen) {
expect($sendsInFull($screen))->toBeFalse();
}
});

Expand Down
Loading
Loading