function __ensureEqual(rxQuery) {
  rxQuery._lastEnsureEqual = (0, _util.now)();
  if (rxQuery.collection.database.destroyed) return false; // db is closed

  if (_isResultsInSync(rxQuery)) return false; // nothing happend

  var ret = false;
  var mustReExec = false; // if this becomes true, a whole execution over the database is made

  if (rxQuery._latestChangeEvent === -1) mustReExec = true; // have not executed yet -> must run

  /**
   * try to use the queryChangeDetector to calculate the new results
   */

  if (!mustReExec) {
    var missedChangeEvents = rxQuery.collection._changeEventBuffer.getFrom(rxQuery._latestChangeEvent + 1);

    if (missedChangeEvents === null) {
      // changeEventBuffer is of bounds -> we must re-execute over the database
      mustReExec = true;
    } else {
      rxQuery._latestChangeEvent = rxQuery.collection._changeEventBuffer.counter;
      /**
       * because pouchdb prefers writes over reads,
       * we have to filter out the events that happend before the read has started
       * so that we do not fill event-reduce with the wrong data
       */

      missedChangeEvents = missedChangeEvents.filter(function (cE) {
        return !cE.startTime || cE.startTime > rxQuery._lastExecStart;
      });

      var runChangeEvents = rxQuery.collection._changeEventBuffer.reduceByLastOfDoc(missedChangeEvents);
      /*
      console.log('calculateNewResults() ' + new Date().getTime());
      console.log(rxQuery._lastExecStart + ' - ' + rxQuery._lastExecEnd);
      console.dir(rxQuery._resultsData.slice());
      console.dir(runChangeEvents);
      */


      var eventReduceResult = (0, _eventReduce.calculateNewResults)(rxQuery, runChangeEvents);

      if (eventReduceResult.runFullQueryAgain) {
        // could not calculate the new results, execute must be done
        mustReExec = true;
      } else if (eventReduceResult.changed) {
        // we got the new results, we do not have to re-execute, mustReExec stays false
        ret = true; // true because results changed

        rxQuery._setResultData(eventReduceResult.newResults);
      }
    }
  } // oh no we have to re-execute the whole query over the database


  if (mustReExec) {
    // counter can change while _execOverDatabase() is running so we save it here
    var latestAfter = rxQuery.collection._changeEventBuffer.counter;
    return rxQuery._execOverDatabase().then(function (newResultData) {
      rxQuery._latestChangeEvent = latestAfter;

      if (!(0, _deepEqual["default"])(newResultData, rxQuery._resultsData)) {
        ret = true; // true because results changed

        rxQuery._setResultData(newResultData);
      }

      return ret;
    });
  }

  return ret; // true if results have changed
}
/**
 * ensures that the results of this query is equal to the results which a query over the database would give
 * @return true if results have changed
 */
ensures that the results of this query is equal to the results which a query over the database would give
@return true if results have changed
ensures that the results of this query is equal to the results which a query over the database would give @return true if results have changed
function __ensureEqual(rxQuery) {
  rxQuery._lastEnsureEqual = (0, _util.now)();
  if (rxQuery.collection.database.destroyed) return false; 
  if (_isResultsInSync(rxQuery)) return false; 
  var ret = false;
  var mustReExec = false; 
  if (rxQuery._latestChangeEvent === -1) mustReExec = true; 
  if (!mustReExec) {
    var missedChangeEvents = rxQuery.collection._changeEventBuffer.getFrom(rxQuery._latestChangeEvent + 1);
    if (missedChangeEvents === null) {
      mustReExec = true;
    } else {
      rxQuery._latestChangeEvent = rxQuery.collection._changeEventBuffer.counter;
      missedChangeEvents = missedChangeEvents.filter(function (cE) {
        return !cE.startTime || cE.startTime > rxQuery._lastExecStart;
      });
      var runChangeEvents = rxQuery.collection._changeEventBuffer.reduceByLastOfDoc(missedChangeEvents);
      var eventReduceResult = (0, _eventReduce.calculateNewResults)(rxQuery, runChangeEvents);
      if (eventReduceResult.runFullQueryAgain) {
        mustReExec = true;
      } else if (eventReduceResult.changed) {
        ret = true; 
        rxQuery._setResultData(eventReduceResult.newResults);
      }
    }
  } 
  if (mustReExec) {
    var latestAfter = rxQuery.collection._changeEventBuffer.counter;
    return rxQuery._execOverDatabase().then(function (newResultData) {
      rxQuery._latestChangeEvent = latestAfter;
      if (!(0, _deepEqual["default"])(newResultData, rxQuery._resultsData)) {
        ret = true; 
        rxQuery._setResultData(newResultData);
      }
      return ret;
    });
  }
  return ret; 
}
function __ensureEqual ( rxQuery ) { rxQuery . _lastEnsureEqual = ( 0 , _util . now ) ( ) ; if ( rxQuery . collection . database . destroyed ) return false ; if ( _isResultsInSync ( rxQuery ) ) return false ; var ret = false ; var mustReExec = false ; if ( rxQuery . _latestChangeEvent === - 1 ) mustReExec = true ; if ( ! mustReExec ) { var missedChangeEvents = rxQuery . collection . _changeEventBuffer . getFrom ( rxQuery . _latestChangeEvent + 1 ) ; if ( missedChangeEvents === null ) { mustReExec = true ; } else { rxQuery . _latestChangeEvent = rxQuery . collection . _changeEventBuffer . counter ; missedChangeEvents = missedChangeEvents . filter ( function ( cE ) { return ! cE . startTime || cE . startTime > rxQuery . _lastExecStart ; } ) ; var runChangeEvents = rxQuery . collection . _changeEventBuffer . reduceByLastOfDoc ( missedChangeEvents ) ; var eventReduceResult = ( 0 , _eventReduce . calculateNewResults ) ( rxQuery , runChangeEvents ) ; if ( eventReduceResult . runFullQueryAgain ) { mustReExec = true ; } else if ( eventReduceResult . changed ) { ret = true ; rxQuery . _setResultData ( eventReduceResult . newResults ) ; } } } if ( mustReExec ) { var latestAfter = rxQuery . collection . _changeEventBuffer . counter ; return rxQuery . _execOverDatabase ( ) . then ( function ( newResultData ) { rxQuery . _latestChangeEvent = latestAfter ; if ( ! ( 0 , _deepEqual [ "default" ] ) ( newResultData , rxQuery . _resultsData ) ) { ret = true ; rxQuery . _setResultData ( newResultData ) ; } return ret ; } ) ; } return ret ; }
ensures that the results of this query is equal to the results which a query over the database would give
 @return true if results have changed
ensures that the results of this query is equal to the results which a query over the database would give @return true if results have changed
// db is closed // nothing happend // if this becomes true, a whole execution over the database is made // have not executed yet -> must run /**
   * try to use the queryChangeDetector to calculate the new results
   */ // changeEventBuffer is of bounds -> we must re-execute over the database /**
       * because pouchdb prefers writes over reads,
       * we have to filter out the events that happend before the read has started
       * so that we do not fill event-reduce with the wrong data
       */ /*
      console.log('calculateNewResults() ' + new Date().getTime());
      console.log(rxQuery._lastExecStart + ' - ' + rxQuery._lastExecEnd);
      console.dir(rxQuery._resultsData.slice());
      console.dir(runChangeEvents);
      */ // could not calculate the new results, execute must be done // we got the new results, we do not have to re-execute, mustReExec stays false // true because results changed // oh no we have to re-execute the whole query over the database // counter can change while _execOverDatabase() is running so we save it here // true because results changed // true if results have changed