@@ -8,7 +8,6 @@ import type {
88 PullRequestFilesViewedResult ,
99 PullRequestRef ,
1010} from "@t3tools/contracts" ;
11- import * as Option from "effect/Option" ;
1211import { AsyncResult } from "effect/reactivity" ;
1312
1413import { appAtomRegistry } from "~/rpc/atomRegistry" ;
@@ -37,7 +36,7 @@ type FilesViewedBatch = ReadonlyArray<{
3736
3837export interface FilesViewedStoreDeps {
3938 readonly flushDelayMs : number ;
40- /** Calls `onAnswer` with the host's current answer at once , then with every later one . */
39+ /** Calls `onAnswer` with the current answer, then after each completed read . */
4140 readonly subscribeAnswers : (
4241 target : FilesViewedTarget ,
4342 onAnswer : ( answer : PullRequestFilesViewedResult | null ) => void ,
@@ -68,13 +67,8 @@ interface FilesViewedScope {
6867 readonly sentBy : Map < string , number > ;
6968 requests : number ;
7069 timer : ReturnType < typeof setTimeout > | null ;
71- answer : PullRequestFilesViewedResult | null ;
72- /**
73- * The host's answer as it stood when a completed write was acknowledged, per path. The first
74- * answer that differs from it is the first read that could have seen the write, which is what
75- * retires the press rather than the host happening to agree with it.
76- */
77- readonly answeredFrom : Map < string , PullRequestFilesViewedResult | null > ;
70+ // Completed reads retire acknowledged presses even when equal data is structurally shared.
71+ readonly answeredFrom : Set < string > ;
7872}
7973
8074/**
@@ -95,12 +89,11 @@ export function createFilesViewedStore(deps: FilesViewedStoreDeps) {
9589 const isLive = ( key : string , scope : FilesViewedScope ) => scopes . get ( key ) === scope ;
9690
9791 const settle = ( scope : FilesViewedScope , answer : PullRequestFilesViewedResult | null ) => {
98- if ( answer === scope . answer ) return ;
99- scope . answer = answer ;
92+ if ( answer === null ) return ;
10093 const pending = new Set ( [ ...scope . queued . keys ( ) , ...scope . sentBy . keys ( ) ] ) ;
10194 const answered = new Set < string > ( ) ;
102- for ( const [ path , from ] of scope . answeredFrom ) {
103- if ( pending . has ( path ) || from === answer ) continue ;
95+ for ( const path of scope . answeredFrom ) {
96+ if ( pending . has ( path ) ) continue ;
10497 answered . add ( path ) ;
10598 scope . answeredFrom . delete ( path ) ;
10699 }
@@ -141,7 +134,7 @@ export function createFilesViewedStore(deps: FilesViewedStoreDeps) {
141134 }
142135 // Answered for from the next read on, whatever it says. A push landing between the write
143136 // and that read comes back as `dismissed`, and the press must not stand over it.
144- for ( const path of mine ) scope . answeredFrom . set ( path , scope . answer ) ;
137+ for ( const path of mine ) scope . answeredFrom . add ( path ) ;
145138 deps . refresh ( scope . target ) ;
146139 } ) ;
147140 } ;
@@ -160,10 +153,8 @@ export function createFilesViewedStore(deps: FilesViewedStoreDeps) {
160153 if ( scope === undefined ) {
161154 const listeners = new Set < ( ) => void > ( ) ;
162155 let created : FilesViewedScope | undefined ;
163- let initial : PullRequestFilesViewedResult | null = null ;
164156 const unsubscribeAnswers = deps . subscribeAnswers ( target , ( answer ) => {
165- if ( created === undefined ) initial = answer ;
166- else settle ( created , answer ) ;
157+ if ( created !== undefined ) settle ( created , answer ) ;
167158 } ) ;
168159 created = {
169160 target,
@@ -174,8 +165,7 @@ export function createFilesViewedStore(deps: FilesViewedStoreDeps) {
174165 sentBy : new Map ( ) ,
175166 requests : 0 ,
176167 timer : null ,
177- answer : initial ,
178- answeredFrom : new Map ( ) ,
168+ answeredFrom : new Set ( ) ,
179169 } ;
180170 scope = created ;
181171 scopes . set ( key , scope ) ;
@@ -243,7 +233,9 @@ const filesViewedStore = createFilesViewedStore({
243233 subscribeAnswers : ( target , onAnswer ) =>
244234 appAtomRegistry . subscribe (
245235 filesViewedAtom ( target ) ,
246- ( result ) => onAnswer ( Option . getOrNull ( AsyncResult . value ( result ) ) ) ,
236+ ( result ) => {
237+ if ( AsyncResult . isSuccess ( result ) && ! result . waiting ) onAnswer ( result . value ) ;
238+ } ,
247239 { immediate : true } ,
248240 ) ,
249241 send : ( { environmentId, reference } , files ) =>
0 commit comments