@ Override public void changed ( Database . ChangeEvent event ) { synchronized ( changesLock ) { if ( isTimeout ) return ; lastChangesTimestamp = System . currentTimeMillis ( ) ; // Stop timeout timer: stopTimeout ( ) ; // In race condition, new doc or update doc is fired before starting to observe the // DatabaseChangeEvent, it allows to skip few document changes with /_changes REST API. // Make sure all document changes are tread by /_changes REST API. if ( ! filled ) { filled = true ; RevisionList changes = db . changesSince ( changesSince , changesOptions , changesFilter , changesFilterParams ) ; if ( changes . size ( ) > 0 ) { sendLongpollChanges ( changes , changesSince ) ; return ; } } List < RevisionInternal > revs = new ArrayList < RevisionInternal > ( ) ; List < DocumentChange > changes = event . getChanges ( ) ; for ( DocumentChange change : changes ) { RevisionInternal rev = change . getAddedRevision ( ) ; if ( rev == null ) continue ; String winningRevID = change . getWinningRevisionID ( ) ; if ( ! this . changesIncludesConflicts ) { if ( winningRevID == null ) continue ; // // this change doesn't affect the winning rev ID, no need to send it else if ( ! winningRevID . equals ( rev . getRevID ( ) ) ) { // This rev made a _different_ rev current, so substitute that one. // We need to emit the current sequence # in the feed, so put it in the rev. // This isn't correct internally (this is an old rev so it has an older sequence) // but consumers of the _changes feed don't care about the internal state. RevisionInternal mRev = db . getDocument ( rev . getDocID ( ) , winningRevID , changesIncludesDocs ) ; mRev . setSequence ( rev . getSequence ( ) ) ; rev = mRev ; } } if ( ! event . getSource ( ) . runFilter ( changesFilter , changesFilterParams , rev ) ) continue ; if ( longpoll ) { revs . add ( rev ) ; } else { Log . d ( TAG , "Router: Sending continuous change chunk" ) ; sendContinuousChange ( rev ) ; } timeoutLastSeqence = rev . getSequence ( ) ; } if ( longpoll && revs . size ( ) > 0 ) sendLongpollChanges ( revs , changesSince ) ; else // Restart timeout timer for continuous feed request: startTimeout ( ) ; } }
Implementation of ChangeListener