All files / lib/timers promises.js

100% Statements 173/173
100% Branches 58/58
100% Functions 5/5
100% Lines 173/173

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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 17411x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 2x 2x 2x 2x 2x 2x 11x 47x 47x 47x 4x 4x 4x 4x 4x 4x 43x 43x 43x 47x 6x 6x 47x 5x 5x 5x 5x 5x 5x 47x 1x 1x 31x 31x 31x 31x 31x 31x 3x 3x 3x 3x 3x 31x 47x 3x 3x 47x 47x 11x 22x 22x 4x 4x 4x 4x 4x 4x 18x 18x 18x 22x 6x 6x 22x 5x 5x 5x 5x 5x 5x 22x 1x 1x 6x 6x 6x 6x 6x 3x 3x 3x 3x 3x 6x 22x 3x 3x 22x 22x 11x 28x 28x 28x 28x 28x 28x 28x 28x 11x 11x 11x 11x 11x 11x 11x 18x 18x 17x 17x 17x 11x 28x 11x 28x 6x 4x 4x 4x 1x 1x 1x 6x 6x 6x 11x 28x 19x 19x 17x 18x 18x 12x 19x 3x 27x 10x 10x 10x 10x 28x 11x 11x 11x 11x 11x 11x  
'use strict';
 
const {
  FunctionPrototypeBind,
  Promise,
  PromisePrototypeFinally,
  PromiseReject,
} = primordials;
 
const {
  Timeout,
  Immediate,
  insert
} = require('internal/timers');
 
const {
  AbortError,
  codes: { ERR_INVALID_ARG_TYPE }
} = require('internal/errors');
 
const {
  validateAbortSignal,
  validateBoolean,
  validateObject,
} = require('internal/validators');
 
function cancelListenerHandler(clear, reject) {
  if (!this._destroyed) {
    clear(this);
    reject(new AbortError());
  }
}
 
function setTimeout(after, value, options = {}) {
  const args = value !== undefined ? [value] : value;
  if (options == null || typeof options !== 'object') {
    return PromiseReject(
      new ERR_INVALID_ARG_TYPE(
        'options',
        'Object',
        options));
  }
  const { signal, ref = true } = options;
  try {
    validateAbortSignal(signal, 'options.signal');
  } catch (err) {
    return PromiseReject(err);
  }
  if (typeof ref !== 'boolean') {
    return PromiseReject(
      new ERR_INVALID_ARG_TYPE(
        'options.ref',
        'boolean',
        ref));
  }
  if (signal?.aborted) {
    return PromiseReject(new AbortError());
  }
  let oncancel;
  const ret = new Promise((resolve, reject) => {
    const timeout = new Timeout(resolve, after, args, false, true);
    if (!ref) timeout.unref();
    insert(timeout, timeout._idleTimeout);
    if (signal) {
      oncancel = FunctionPrototypeBind(cancelListenerHandler,
                                       // eslint-disable-next-line no-undef
                                       timeout, clearTimeout, reject);
      signal.addEventListener('abort', oncancel);
    }
  });
  return oncancel !== undefined ?
    PromisePrototypeFinally(
      ret,
      () => signal.removeEventListener('abort', oncancel)) : ret;
}
 
function setImmediate(value, options = {}) {
  if (options == null || typeof options !== 'object') {
    return PromiseReject(
      new ERR_INVALID_ARG_TYPE(
        'options',
        'Object',
        options));
  }
  const { signal, ref = true } = options;
  try {
    validateAbortSignal(signal, 'options.signal');
  } catch (err) {
    return PromiseReject(err);
  }
  if (typeof ref !== 'boolean') {
    return PromiseReject(
      new ERR_INVALID_ARG_TYPE(
        'options.ref',
        'boolean',
        ref));
  }
  if (signal?.aborted) {
    return PromiseReject(new AbortError());
  }
  let oncancel;
  const ret = new Promise((resolve, reject) => {
    const immediate = new Immediate(resolve, [value]);
    if (!ref) immediate.unref();
    if (signal) {
      oncancel = FunctionPrototypeBind(cancelListenerHandler,
                                       // eslint-disable-next-line no-undef
                                       immediate, clearImmediate, reject);
      signal.addEventListener('abort', oncancel);
    }
  });
  return oncancel !== undefined ?
    PromisePrototypeFinally(
      ret,
      () => signal.removeEventListener('abort', oncancel)) : ret;
}
 
async function* setInterval(after, value, options = {}) {
  validateObject(options, 'options');
  const { signal, ref = true } = options;
  validateAbortSignal(signal, 'options.signal');
  validateBoolean(ref, 'options.ref');
 
  if (signal?.aborted)
    throw new AbortError();
 
  let onCancel;
  let interval;
  try {
    let notYielded = 0;
    let callback;
    interval = new Timeout(() => {
      notYielded++;
      if (callback) {
        callback();
        callback = undefined;
      }
    }, after, undefined, true, true);
    if (!ref) interval.unref();
    insert(interval, interval._idleTimeout);
    if (signal) {
      onCancel = () => {
        // eslint-disable-next-line no-undef
        clearInterval(interval);
        if (callback) {
          callback(PromiseReject(new AbortError()));
          callback = undefined;
        }
      };
      signal.addEventListener('abort', onCancel, { once: true });
    }
 
    while (!signal?.aborted) {
      if (notYielded === 0) {
        await new Promise((resolve) => callback = resolve);
      }
      for (; notYielded > 0; notYielded--) {
        yield value;
      }
    }
    throw new AbortError();
  } finally {
    // eslint-disable-next-line no-undef
    clearInterval(interval);
    signal?.removeEventListener('abort', onCancel);
  }
}
 
module.exports = {
  setTimeout,
  setImmediate,
  setInterval,
};