@@ -43,6 +43,11 @@ export interface SubscribeState<T> {
4343 snapshotComplete : boolean ;
4444 /** True while a re-subscribe holds the previous snapshot as `data`. */
4545 resubscribing ?: boolean ;
46+ /**
47+ * The statement ended, as a SUBSCRIBE does at its `UP TO`. Set only with
48+ * `closeSocketOnComplete`, which then closes the socket.
49+ */
50+ isComplete ?: boolean ;
4651 error : SubscribeError | undefined ;
4752}
4853
@@ -99,6 +104,7 @@ export class SubscribeManager<T extends object, R> implements Connectable {
99104 /** True when the socket signalled ready before any request was set. */
100105 private readyAwaitingRequest : boolean = false ;
101106 private currentTimestamp : number | undefined ;
107+ private connectionId : number | undefined ;
102108 /** Holds completed timestamp messages, waiting for the flush interval */
103109 private closedTimestampBuffer : SubscribeRow < T > [ ] = [ ] ;
104110 /** Holds messages from the socket until the timestamp is closed */
@@ -234,6 +240,7 @@ export class SubscribeManager<T extends object, R> implements Connectable {
234240 // than flashing the loading state on every reconnect. `resubscribing`
235241 // marks the held data. Any following setState clears it.
236242 this . resetForNewConnection ( ) ;
243+ this . connectionId = undefined ;
237244 if ( this . snapshotState . snapshotComplete || this . snapshotState . error ) {
238245 this . snapshotState = {
239246 ...this . snapshotState ,
@@ -261,6 +268,21 @@ export class SubscribeManager<T extends object, R> implements Connectable {
261268 return this . snapshotState ;
262269 } ;
263270
271+ /**
272+ * The subscribe's upper: `getSnapshot().data` holds every update before it.
273+ * Undefined until the snapshot completes. It applies the updates waiting for
274+ * the flush interval, so read the snapshot after calling it.
275+ */
276+ getUpper = ( ) => {
277+ this . flushSocketBuffer ( ) ;
278+ return this . currentState . snapshotComplete
279+ ? this . currentTimestamp
280+ : undefined ;
281+ } ;
282+
283+ /** The session's `pg_backend_pid()`, once the connection reports it. */
284+ getConnectionId = ( ) => this . connectionId ;
285+
264286 private setState ( update : Partial < SubscribeState < SubscribeRow < T > > > ) {
265287 this . currentState = {
266288 ...this . currentState ,
@@ -282,6 +304,7 @@ export class SubscribeManager<T extends object, R> implements Connectable {
282304 private onReadyForQuery = ( ) => {
283305 if ( this . querySent && this . closeSocketOnComplete ) {
284306 this . socket . disconnect ( ) ;
307+ this . setState ( { isComplete : true } ) ;
285308 return ;
286309 }
287310 if ( this . sqlRequest ) {
@@ -313,6 +336,9 @@ export class SubscribeManager<T extends object, R> implements Connectable {
313336 } ,
314337 } ) ;
315338 }
339+ if ( message . type === "BackendKeyData" ) {
340+ this . connectionId = message . payload . conn_id ;
341+ }
316342 if ( message . type === "Rows" ) {
317343 this . columns = message . payload . columns . map ( mapColumnToColumnMetadata ) ;
318344 }
@@ -366,10 +392,10 @@ export class SubscribeManager<T extends object, R> implements Connectable {
366392
367393 private flushSocketBuffer = ( ) => {
368394 if ( this . closedTimestampBuffer . length === 0 ) return ;
369-
370- const clearBuffer = ( ) => {
371- this . closedTimestampBuffer = [ ] ;
372- } ;
395+ // NOTE: taken before `setState`. Its listeners may call `getUpper`, which
396+ // flushes again, and a buffer still set then recurses without end.
397+ const closed = this . closedTimestampBuffer ;
398+ this . closedTimestampBuffer = [ ] ;
373399
374400 const newState = {
375401 ...this . currentState ,
@@ -384,16 +410,13 @@ export class SubscribeManager<T extends object, R> implements Connectable {
384410 ) ;
385411
386412 // We want to filter out progress messages which have mzState === null
387- const filteredChanges = this . closedTimestampBuffer . filter (
413+ const filteredChanges = closed . filter (
388414 ( change ) => change . mzState !== null ,
389415 ) ;
390416
391- // If there are no real changes, we can clear the buffer and return early to avoid
392- // unnecessary notifications to listeners.
393- if ( filteredChanges . length === 0 ) {
394- clearBuffer ( ) ;
395- return ;
396- }
417+ // If there are no real changes, return early to avoid unnecessary
418+ // notifications to listeners.
419+ if ( filteredChanges . length === 0 ) return ;
397420
398421 for ( const change of filteredChanges ) {
399422 switch ( change . mzState ) {
@@ -425,14 +448,10 @@ export class SubscribeManager<T extends object, R> implements Connectable {
425448 // Convert the upsert map back into an array once upsert operation is complete.
426449 newState . data = Array . from ( upsertMap . values ( ) ) ;
427450 } else {
428- newState . data = [
429- ...newState . data ,
430- ...this . closedTimestampBuffer . values ( ) ,
431- ] ;
451+ newState . data = [ ...newState . data , ...closed ] ;
432452 }
433453
434454 this . setState ( newState ) ;
435- clearBuffer ( ) ;
436455 } ;
437456}
438457
0 commit comments