import { assert , Timer } from "@fluidframework/common-utils" ; import { ISequencedDocumentMessage , MessageType } from "@fluidframework/protocol-definitions" ; import { isSystemMessage } from "@fluidframework/protocol-base" ; export class CollabWindowTracker { private opsCountSinceNoop = 0 ; private readonly timer : Timer ; constructor ( private readonly submit , private readonly activeConnection , NoopTimeFrequency = 2000 , private readonly NoopCountFrequency = 50 ) { this . timer = new Timer ( NoopTimeFrequency , ( ) => { if ( this . opsCountSinceNoop !== 0 ) { assert ( this . activeConnection ( ) , 0x241 ) ; this . submitNoop ( false ) ; } } ) ; } /**
     * Schedules as ack to the server to update the reference sequence number
     */ public scheduleSequenceNumberUpdate ( message , immediateNoOp ) { if ( ! this . activeConnection ( ) ) { return ; } if ( immediateNoOp ) { this . submitNoop ( true ) ; return ; } if ( isSystemMessage ( message ) ) { return ; } assert ( message . type !== MessageType . NoOp , 0x0ce ) ; this . opsCountSinceNoop ++ ; if ( this . opsCountSinceNoop >= this . NoopCountFrequency ) { this . submitNoop ( false ) ; return ; } if ( this . opsCountSinceNoop === 1 ) { this . timer . restart ( ) ; } assert ( this . timer . hasTimer , 0x242 ) ; } private submitNoop ( immediate ) { this . submit ( MessageType . NoOp , immediate ? "" : null ) ; assert ( this . opsCountSinceNoop === 0 , 0x243 ) ; } public stopSequenceNumberUpdate ( ) { this . opsCountSinceNoop = 0 ; } }