All files / confluent-kafka-javascript/lib/tools ref-counter.js

18.75% Statements 3/16
0% Branches 0/8
0% Functions 0/3
18.75% Lines 3/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54                  1x                                             1x                         1x                
/*
 * confluent-kafka-javascript - Node.js wrapper  for RdKafka C/C++ library
 *
 * Copyright (c) 2016-2023 Blizzard Entertainment
 *
 * This software may be modified and distributed under the terms
 * of the MIT license.  See the LICENSE.txt file for details.
 */
 
module.exports = RefCounter;
 
/**
 * Ref counter class.
 *
 * Is used to basically determine active/inactive and allow callbacks that
 * hook into each.
 *
 * For the producer, it is used to begin rapid polling after a produce until
 * the delivery report is dispatched.
 * @private
 */
function RefCounter(onActive, onPassive) {
  this.context = {};
  this.onActive = onActive;
  this.onPassive = onPassive;
  this.currentValue = 0;
  this.isRunning = false;
}
 
/**
 * Increment the ref counter
 */
RefCounter.prototype.increment = function() {
  this.currentValue += 1;
 
  // If current value exceeds 0, activate the start
  if (this.currentValue > 0 && !this.isRunning) {
    this.isRunning = true;
    this.onActive(this.context);
  }
};
 
/**
 * Decrement the ref counter
 */
RefCounter.prototype.decrement = function() {
  this.currentValue -= 1;
 
  if (this.currentValue <= 0 && this.isRunning) {
    this.isRunning = false;
    this.onPassive(this.context);
  }
};