All files oven.js

100% Statements 25/25
87.5% Branches 7/8
100% Functions 4/4
100% Lines 24/24

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 541x                       10x 10x 10x 10x 10x   10x 8x         16x 16x 16x 16x         10x 10x 8x 8x   8x 8x   2x 2x       10x 10x 8x         1x  
const debug = require('debug')('SimpleZSTDOven');
 
class Oven {
  #poolOptions;
 
  #queue;
 
  #factory;
 
  #destroy;
 
  constructor(poolOptions, factory, destroy) {
    debug('constructor', poolOptions);
    this.#poolOptions = poolOptions || {};
    this.#queue = [];
    this.#factory = factory;
    this.#destroy = destroy;
 
    for (let i = 0; i < this.#poolOptions.targetSize || 0; i += 1) {
      this.#createResource();
    }
  }
 
  async #createResource() {
    debug('createResource?', this.#queue.length);
    Eif (this.#queue.length < this.#poolOptions.targetSize) {
      debug('createResource call factory');
      this.#queue.push(this.#factory());
    }
  }
 
  async acquire() {
    debug('acquire');
    if (this.#queue.length > 0) {
      setTimeout(() => {
        this.#createResource();
      }, 1);
      debug('acquire from queue');
      return this.#queue.pop();
    }
    debug('acquire create on demand');
    return this.#factory();
  }
 
  async destroy() {
    debug('destroy', this.#queue.length);
    while (this.#queue.length > 0) {
      this.#destroy(this.#queue.pop());
    }
  }
}
 
module.exports = Oven;