All files prom.js

97.38% Statements 149/153
92.59% Branches 25/27
71.42% Functions 10/14
97.38% Lines 149/153

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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 1541x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 3x 3x 9x 9x 4x 4x 9x 9x 9x 9x 1x 1x 1x 1x 1x 1x 9x 9x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 1x 1x 4x 4x 4x 4x 4x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 2x 2x 2x 2x 2x 2x 3x 3x 3x 1x 1x 1x 1x 3x         3x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 7x 4x 4x 4x 3x 3x 3x 7x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x  
// @ts-check
// SPDX-License-Identifier: Zlib
// SPDX-FileCopyrightText: 2024 CERN (home.cern)
// SPDX-Created: 2024-02-19T10:01:22
// SPDX-FileContributor: Author: Sylvain Fargier <sylvain.fargier@cern.ch>
 
/**
 * @template T
 * @brief Deferred object class, regrouping resolve/reject and promise
 */
export class Deferred {
  /** @type {Promise<T>} */
  promise;
 
  isPending = false;
 
  /** @type {(() => any)|null} */
  abort = null;
 
  /** @type {(value: T) => void} */
  resolve = (_val) => undefined;
 
  /** @type {(err: Error|any) => void} */
  reject = (_err) => undefined;
 
  constructor() {
    this.promise = new Promise((resolve, reject) => {
      this.isPending = true;
      this.resolve = (val) => {
        this.isPending = false;
        resolve(val);
      };
      this.reject = (err) => {
        this.isPending = false;
        reject(err);
      };
    });
  }
}
 
/**
 * @template T=any
 * @return {Deferred<T>}
 */
export function makeDeferred() {
  return new Deferred();
}
 
/**
 * @brief adds a timeout on deferred
 * @template T=any
 * @param {Deferred<T>} deferred
 * @param {number} ms
 * @param {(Error|any)=} error
 * @return {Promise<T>}
 */
export function deferredTimeout(deferred, ms, error) {
  /** @type {NodeJS.Timeout|null} */
  var timeoutId = setTimeout(function() {
    if (!error || typeof error === "string") {
      error = new Error(error || "Timed out after " + ms + " ms");
      error.code = "ETIMEDOUT";
    }
    timeoutId = null;
    const rej = deferred.reject;
    deferred.resolve = function() {};
    deferred.reject = function() {};
    rej(error);
    if (deferred.abort) {
      deferred.abort();
    }
  }, ms);
 
  return deferred.promise.finally(() => {
    if (timeoutId) { clearTimeout(timeoutId); }
  });
}
 
/**
 * @brief adds a timeout on promise
 * @template T=any
 * @param {Promise<T>} prom
 * @param {number} ms
 * @param {(Error|any)=} error
 * @return {Promise<T>}
 */
export function promiseTimeout(prom, ms, error) {
  return new Promise(function(resolve, reject) {
    let resolved = false;
    const timeoutId = setTimeout(function() {
      if (!error || typeof error === "string") {
        error = new Error(error || "Timed out after " + ms + " ms");
        error.code = "ETIMEDOUT";
      }
      resolved = true;
      reject(error);
    }, ms);
 
    prom.then(function(value) {
      if (!resolved) {
        clearTimeout(timeoutId);
        resolve(value);
      }
    }, function(error) {
      if (!resolved) {
        clearTimeout(timeoutId);
        reject(error);
      }
    });
  });
}
 
/**
 * @brief adds a timeout on promise
 * @template T=any
 * @param {Deferred<T>|Promise<T>} promise
 * @param {number} ms
 * @param {(Error|any)=} error
 * @return {Promise<T>}
 */
export function timeout(promise, ms, error) {
  if (promise && "resolve" in promise) {
    return deferredTimeout(/** @type {Deferred<T>} */ (promise),
      ms, error);
  }
  else {
    return promiseTimeout(/** @type {Promise<T>} */ (promise), ms, error);
  }
}
 
/**
 * @template T=undefined
 * @param {number} ms
 * @param {T} [ret]
 * @return {Promise<T>}
 */
export function delay(ms, ret) {
  return new Promise(function(resolve) {
    setTimeout(function() { resolve(/** @type {T} */ (ret)); }, ms);
  });
}
 
/**
 * @template T=any
 * @param  {(ret: T) => any} fun
 * @return {(ret: T) => Promise<T>}
 */
export function tap(fun) {
  return function(/** @type {T} */ ret) {
    return Promise.resolve(fun(ret))
    .then(() => ret);
  };
}