@@ -100,6 +100,15 @@ export type ThreadCurrentPullRequest =
100100 readonly top : ThreadPullRequestLink ;
101101 } ;
102102
103+ function layersFromTop ( chain : ThreadPullRequestChain ) : Array < ThreadPullRequestLink > {
104+ // `.sort()` on a fresh array, not `.toSorted()`: this runs on Hermes, which has no ES2023
105+ // array methods, and a TypeError here is fatal on every mobile launch that renders a stack.
106+ return chain . layers
107+ . map ( ( link , index ) => ( { link, index, depth : chain . depths [ index ] ! } ) )
108+ . sort ( ( left , right ) => right . depth - left . depth || right . index - left . index )
109+ . map ( ( entry ) => entry . link ) ;
110+ }
111+
103112export function resolveThreadCurrentPullRequest (
104113 links : ReadonlyArray < ThreadPullRequestLink > ,
105114) : ThreadCurrentPullRequest | null {
@@ -109,10 +118,8 @@ export function resolveThreadCurrentPullRequest(
109118 if ( open . length === 1 ) return { kind : "single" , link : open [ 0 ] ! } ;
110119 const chains = resolveThreadPullRequestChains ( visible ) ;
111120 if ( open . length > 1 ) {
112- // `.reverse()` on a copy, not `.toReversed()`: this runs on Hermes, which has no ES2023
113- // array methods, and a TypeError here is fatal on every mobile launch that renders a stack.
114121 const openChains = chains
115- . map ( ( chain ) => [ ... chain . layers ] . reverse ( ) . filter ( isOpen ) )
122+ . map ( ( chain ) => layersFromTop ( chain ) . filter ( isOpen ) )
116123 . filter ( ( layers ) => layers . length > 0 )
117124 . sort (
118125 ( left , right ) =>
@@ -123,7 +130,7 @@ export function resolveThreadCurrentPullRequest(
123130 return { kind : "stack" , open : ordered , top : ordered [ 0 ] ! } ;
124131 }
125132 if ( chains . length === 1 ) {
126- return { kind : "single" , link : chains [ 0 ] ! . layers . at ( - 1 ) ! } ;
133+ return { kind : "single" , link : layersFromTop ( chains [ 0 ] ! ) [ 0 ] ! } ;
127134 }
128135 const terminal = [ ...visible ] . sort (
129136 ( left , right ) => latestUpdatedAt ( right ) - latestUpdatedAt ( left ) ,
@@ -189,6 +196,8 @@ export function legacyLinkedPullRequestOf(
189196export interface ThreadPullRequestChain {
190197 readonly kind : "native" | "derived" ;
191198 readonly layers : ReadonlyArray < ThreadPullRequestLink > ;
199+ /** One per layer: 0 for the base, parent's depth + 1 above it. Siblings share a depth. */
200+ readonly depths : ReadonlyArray < number > ;
192201}
193202
194203export function resolveThreadPullRequestChains (
@@ -211,7 +220,7 @@ export function resolveThreadPullRequestChains(
211220 const order = new Map ( members [ 0 ] ! . stack ! . layers . map ( ( layer , index ) => [ layer . number , index ] ) ) ;
212221 members . sort ( ( left , right ) => ( order . get ( left . number ) ?? 0 ) - ( order . get ( right . number ) ?? 0 ) ) ;
213222 for ( const member of members ) placed . add ( threadPullRequestKeyOf ( member ) ) ;
214- chains . push ( { kind : "native" , layers : members } ) ;
223+ chains . push ( { kind : "native" , layers : members , depths : members . map ( ( _ , index ) => index ) } ) ;
215224 }
216225
217226 const remaining = visible . filter ( ( link ) => ! placed . has ( threadPullRequestKeyOf ( link ) ) ) ;
@@ -225,29 +234,44 @@ export function resolveThreadPullRequestChains(
225234 const key = branchKey ( link , link . snapshot . headBranch ) ;
226235 byHead . set ( key , byHead . has ( key ) ? null : link ) ;
227236 }
228- const hasChild = new Set < string > ( ) ;
229- for ( const link of remaining ) {
230- if ( link . snapshot === null ) continue ;
237+ const parentOf = ( link : ThreadPullRequestLink ) => {
238+ if ( link . snapshot === null ) return undefined ;
231239 const parent = byHead . get ( branchKey ( link , link . snapshot . baseBranch ) ) ;
232- if ( parent != null && parent !== link ) hasChild . add ( threadPullRequestKeyOf ( parent ) ) ;
240+ return parent == null || parent === link ? undefined : parent ;
241+ } ;
242+ const children = new Map < string , Array < ThreadPullRequestLink > > ( ) ;
243+ for ( const link of remaining ) {
244+ const parent = parentOf ( link ) ;
245+ if ( parent === undefined ) continue ;
246+ const key = threadPullRequestKeyOf ( parent ) ;
247+ const siblings = children . get ( key ) ?? [ ] ;
248+ siblings . push ( link ) ;
249+ children . set ( key , siblings ) ;
233250 }
234- for ( const top of remaining ) {
235- if ( hasChild . has ( threadPullRequestKeyOf ( top ) ) ) continue ;
251+ for ( const base of remaining ) {
252+ if ( parentOf ( base ) !== undefined ) continue ;
236253 const layers : Array < ThreadPullRequestLink > = [ ] ;
237- let cursor : ThreadPullRequestLink | undefined = top ;
238- while ( cursor !== undefined && ! placed . has ( threadPullRequestKeyOf ( cursor ) ) ) {
239- placed . add ( threadPullRequestKeyOf ( cursor ) ) ;
240- layers . unshift ( cursor ) ;
241- cursor =
242- cursor . snapshot === null
243- ? undefined
244- : ( byHead . get ( branchKey ( cursor , cursor . snapshot . baseBranch ) ) ?? undefined ) ;
254+ const depths : Array < number > = [ ] ;
255+ const pending : Array < { readonly link : ThreadPullRequestLink ; readonly depth : number } > = [
256+ { link : base , depth : 0 } ,
257+ ] ;
258+ for ( let next = pending . pop ( ) ; next !== undefined ; next = pending . pop ( ) ) {
259+ const key = threadPullRequestKeyOf ( next . link ) ;
260+ // Two links with one identity would otherwise share a child list and expand forever.
261+ if ( placed . has ( key ) ) continue ;
262+ placed . add ( key ) ;
263+ layers . push ( next . link ) ;
264+ depths . push ( next . depth ) ;
265+ const below = children . get ( key ) ?? [ ] ;
266+ for ( let index = below . length - 1 ; index >= 0 ; index -= 1 ) {
267+ pending . push ( { link : below [ index ] ! , depth : next . depth + 1 } ) ;
268+ }
245269 }
246- if ( layers . length > 0 ) chains . push ( { kind : "derived" , layers } ) ;
270+ if ( layers . length > 0 ) chains . push ( { kind : "derived" , layers, depths } ) ;
247271 }
248272 for ( const link of remaining ) {
249273 if ( ! placed . has ( threadPullRequestKeyOf ( link ) ) ) {
250- chains . push ( { kind : "derived" , layers : [ link ] } ) ;
274+ chains . push ( { kind : "derived" , layers : [ link ] , depths : [ 0 ] } ) ;
251275 }
252276 }
253277 return chains ;
0 commit comments