@@ -72,6 +72,7 @@ import {
7272 type PullRequestUpdateInput ,
7373 type SourceControlProviderInfo ,
7474 type SourceControlProviderKind ,
75+ type ThreadPullRequestKey ,
7576} from "@t3tools/contracts" ;
7677import { resolveProjectSettings } from "@t3tools/shared/projectSettings" ;
7778import { detectSourceControlProviderFromRemoteUrl } from "@t3tools/shared/sourceControl" ;
@@ -98,6 +99,16 @@ export interface PullRequestMergeEvent extends PullRequestRef {
9899 readonly mergedAt : string ;
99100}
100101
102+ type PullRequestReading = Pick < PullRequestSummary , "state" | "updatedAt" | "observedAt" > ;
103+
104+ /** Whether `next` is a newer reading of a pull request than `current`. Merged is final. */
105+ function supersedesReading ( current : PullRequestReading | undefined , next : PullRequestReading ) {
106+ if ( current === undefined ) return true ;
107+ if ( current . state === "merged" && next . state !== "merged" ) return false ;
108+ if ( next . updatedAt !== current . updatedAt ) return next . updatedAt > current . updatedAt ;
109+ return ( next . observedAt ?? - Infinity ) >= ( current . observedAt ?? - Infinity ) ;
110+ }
111+
101112/**
102113 * Rows per repository when the client does not ask for a page size, and rows per slice when a
103114 * listing is carried on from a cursor.
@@ -225,6 +236,15 @@ export class PullRequestService extends Context.Service<
225236 never ,
226237 Scope . Scope
227238 > ;
239+ /**
240+ * Pull requests a detail read saw for the first time or in a new state (opened, closed,
241+ * merged), so thread links can catch up before the next sync sweep. Best effort.
242+ */
243+ readonly subscribeStateChanges : Effect . Effect <
244+ Stream . Stream < ThreadPullRequestKey > ,
245+ never ,
246+ Scope . Scope
247+ > ;
228248 readonly subscribeRefreshes : Stream . Stream < number > ;
229249 readonly refreshAfterTurn : ( projectId : ProjectId ) => Effect . Effect < void > ;
230250 readonly detail : ( input : PullRequestRef ) => Effect . Effect < PullRequestDetail , PullRequestError > ;
@@ -666,6 +686,7 @@ const observeRead = Effect.fnUntraced(function* <A, E, R>(read: Effect.Effect<A,
666686
667687export const make = Effect . gen ( function * ( ) {
668688 const mergedPullRequests = yield * PubSub . sliding < PullRequestMergeEvent > ( 64 ) ;
689+ const stateChanges = yield * PubSub . sliding < ThreadPullRequestKey > ( 64 ) ;
669690 const pullRequestRefreshes = yield * SubscriptionRef . make ( 0 ) ;
670691 const registry = yield * PullRequestProviderRegistry . PullRequestProviderRegistry ;
671692 const serverSettings = yield * ServerSettings . ServerSettingsService ;
@@ -3057,13 +3078,39 @@ export const make = Effect.gen(function* () {
30573078 updatedAt : detail . updatedAt ,
30583079 observedAt : detail . observedAt ,
30593080 } ) ;
3060- const shouldReplaceHeldSummary = ( key : string , next : PullRequestSummary ) => {
3061- const current = lastGoodSummary . peek ( key ) ;
3062- if ( current === undefined ) return true ;
3063- if ( current . state === "merged" && next . state !== "merged" ) return false ;
3064- if ( next . updatedAt !== current . updatedAt ) return next . updatedAt > current . updatedAt ;
3065- return ( next . observedAt ?? - Infinity ) >= ( current . observedAt ?? - Infinity ) ;
3066- } ;
3081+ const shouldReplaceHeldSummary = ( key : string , next : PullRequestSummary ) =>
3082+ supersedesReading ( lastGoodSummary . peek ( key ) , next ) ;
3083+ // The newest state a detail read found per pull request, whatever cache epoch or credential
3084+ // it read under. Summary reads stay out: they would hide a change from the detail read after.
3085+ const detailStates = new Map < string , PullRequestReading > ( ) ;
3086+ /**
3087+ * Announces a pull request a detail read sees first or in a new state, so a client showing a
3088+ * fresh merge brings the thread's link and its settlement along, rather than leaving them to
3089+ * the next sync sweep.
3090+ */
3091+ const noteDetailReading = ( ref : PullRequestRef , next : PullRequestSummary ) =>
3092+ Effect . suspend ( ( ) => {
3093+ const scope = refScope ( ref ) ;
3094+ const current = detailStates . get ( scope ) ;
3095+ if ( ! supersedesReading ( current , next ) ) return Effect . void ;
3096+ detailStates . delete ( scope ) ;
3097+ if ( detailStates . size >= REF_EPOCH_CAPACITY ) {
3098+ const oldest = detailStates . keys ( ) . next ( ) . value ;
3099+ if ( oldest !== undefined ) detailStates . delete ( oldest ) ;
3100+ }
3101+ detailStates . set ( scope , {
3102+ state : next . state ,
3103+ updatedAt : next . updatedAt ,
3104+ observedAt : next . observedAt ,
3105+ } ) ;
3106+ return current ?. state !== next . state && ref . host !== undefined
3107+ ? PubSub . publish ( stateChanges , {
3108+ host : ref . host ,
3109+ repository : ref . repository ,
3110+ number : ref . number ,
3111+ } )
3112+ : Effect . void ;
3113+ } ) ;
30673114 const detail : PullRequestService [ "Service" ] [ "detail" ] = ( input ) => {
30683115 const key = refCacheKey ( input ) ;
30693116 // Record the summary from a host or cache read, not the stale value
@@ -3073,9 +3120,13 @@ export const make = Effect.gen(function* () {
30733120 const read = Cache . get ( detailCache , key ) . pipe (
30743121 Effect . tap ( ( value ) => {
30753122 const summary = summaryFromDetail ( value , lastGoodSummary . peek ( key ) ) ;
3076- return shouldReplaceHeldSummary ( key , summary )
3077- ? lastGoodSummary . record ( key , summary )
3078- : Effect . void ;
3123+ return noteDetailReading ( input , summary ) . pipe (
3124+ Effect . andThen (
3125+ shouldReplaceHeldSummary ( key , summary )
3126+ ? lastGoodSummary . record ( key , summary )
3127+ : Effect . void ,
3128+ ) ,
3129+ ) ;
30793130 } ) ,
30803131 ) ;
30813132 return input . allowStale === false
@@ -3374,6 +3425,9 @@ export const make = Effect.gen(function* () {
33743425 subscribeMerges : PubSub . subscribe ( mergedPullRequests ) . pipe (
33753426 Effect . map ( ( subscription ) => Stream . fromSubscription ( subscription ) ) ,
33763427 ) ,
3428+ subscribeStateChanges : PubSub . subscribe ( stateChanges ) . pipe (
3429+ Effect . map ( ( subscription ) => Stream . fromSubscription ( subscription ) ) ,
3430+ ) ,
33773431 subscribeRefreshes : SubscriptionRef . changes ( pullRequestRefreshes ) . pipe (
33783432 Stream . filter ( ( revision ) => revision > 0 ) ,
33793433 ) ,
0 commit comments