All files / lib timers.js

100% Statements 327/327
100% Branches 73/73
100% Functions 15/15
100% Lines 327/327

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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 3498x 3498x 3498x 3491x 3491x 3491x 3491x 3498x 3498x 3491x 3491x 3491x 3491x 3491x 3491x 3491x 3491x 3492x 921x 921x 921x 921x 276x 276x 276x 276x 921x 921x 921x 3491x 3491x 3491x 3498x 137x 137x 137x 137x 16x 16x 16x 16x 16x 16x 8x 8x 8x 16x 137x 137x 137x 137x 137x 137x 137x 5761x 5761x 6x 6x 5755x 5755x 5755x 5755x 5755x 5761x 5435x 5761x 40x 40x 5761x 23x 23x 5761x 257x 257x 16003x 16003x 16003x 257x 5761x 5755x 5755x 5755x 5755x 5755x 5761x 137x 137x 14x 14x 14x 14x 14x 137x 137x 63718x 63718x 3491x 3491x 3491x 63718x 137x 122x 122x 6x 6x 116x 116x 116x 116x 116x 122x 110x 122x 2x 2x 122x 1x 1x 122x 3x 3x 1x 1x 1x 3x 122x 116x 116x 116x 116x 116x 122x 137x 224x 224x 224x 224x 224x 224x 137x 137x 1x 1x 137x 137x 137x 137x 55911x 55911x 55911x 55911x 55911x 55911x 55911x 55911x 55911x 55911x 55911x 55911x 55911x 55911x 137x 137x 55915x 55912x 55912x 55912x 55912x 55915x 55915x 137x 137x 11x 10x 10x 10x 10x 11x 11x 137x 137x 2x 2x 137x 137x 55913x 55913x 6x 6x 55907x 55907x 55907x 55907x 55907x 42300x 55913x 13568x 13568x 55913x 34x 34x 55913x 5x 5x 5x 5x 5x 5x 55913x 55907x 55907x 55913x 137x 137x 4x 137x 137x 17x 17x 17x 9x 9x 9x 9x 17x 17x 9x 9x 10x 1x 1x 9x 9x 9x 9x 17x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x  
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
 
'use strict';
 
const {
  MathTrunc,
  Promise,
} = primordials;
 
const {
  immediateInfo,
  toggleImmediateRef
} = internalBinding('timers');
const L = require('internal/linkedlist');
const {
  async_id_symbol,
  Timeout,
  decRefCount,
  immediateInfoFields: {
    kCount,
    kRefCount
  },
  kRefed,
  initAsyncResource,
  getTimerDuration,
  timerListMap,
  timerListQueue,
  immediateQueue,
  active,
  unrefActive,
  insert
} = require('internal/timers');
const {
  promisify: { custom: customPromisify },
  deprecate
} = require('internal/util');
const { ERR_INVALID_CALLBACK } = require('internal/errors').codes;
const debug = require('internal/util/debuglog').debuglog('timer');
 
const {
  destroyHooksExist,
  // The needed emit*() functions.
  emitDestroy
} = require('internal/async_hooks');
 
// Remove a timer. Cancels the timeout and resets the relevant timer properties.
function unenroll(item) {
  if (item._destroyed)
    return;
 
  item._destroyed = true;
 
  // Fewer checks may be possible, but these cover everything.
  if (destroyHooksExist() && item[async_id_symbol] !== undefined)
    emitDestroy(item[async_id_symbol]);
 
  L.remove(item);
 
  // We only delete refed lists because unrefed ones are incredibly likely
  // to come from http and be recreated shortly after.
  // TODO: Long-term this could instead be handled by creating an internal
  // clearTimeout that makes it clear that the list should not be deleted.
  // That function could then be used by http and other similar modules.
  if (item[kRefed]) {
    // Compliment truncation during insert().
    const msecs = MathTrunc(item._idleTimeout);
    const list = timerListMap[msecs];
    if (list !== undefined && L.isEmpty(list)) {
      debug('unenroll: list empty');
      timerListQueue.removeAt(list.priorityQueuePosition);
      delete timerListMap[list.msecs];
    }
 
    decRefCount();
  }
 
  // If active is called later, then we want to make sure not to insert again
  item._idleTimeout = -1;
}
 
// Make a regular object able to act as a timer by setting some properties.
// This function does not start the timer, see `active()`.
// Using existing objects as timers slightly reduces object overhead.
function enroll(item, msecs) {
  msecs = getTimerDuration(msecs, 'msecs');
 
  // If this item was already in a list somewhere
  // then we should unenroll it from that
  if (item._idleNext) unenroll(item);
 
  L.init(item);
  item._idleTimeout = msecs;
}
 
 
/*
 * DOM-style timers
 */
 
 
function setTimeout(callback, after, arg1, arg2, arg3) {
  if (typeof callback !== 'function') {
    throw new ERR_INVALID_CALLBACK(callback);
  }
 
  let i, args;
  switch (arguments.length) {
    // fast cases
    case 1:
    case 2:
      break;
    case 3:
      args = [arg1];
      break;
    case 4:
      args = [arg1, arg2];
      break;
    default:
      args = [arg1, arg2, arg3];
      for (i = 5; i < arguments.length; i++) {
        // Extend array dynamically, makes .apply run much faster in v6.0.0
        args[i - 2] = arguments[i];
      }
      break;
  }
 
  const timeout = new Timeout(callback, after, args, false, true);
  insert(timeout, timeout._idleTimeout);
 
  return timeout;
}
 
setTimeout[customPromisify] = function(after, value) {
  const args = value !== undefined ? [value] : value;
  return new Promise((resolve) => {
    const timeout = new Timeout(resolve, after, args, false, true);
    insert(timeout, timeout._idleTimeout);
  });
};
 
function clearTimeout(timer) {
  if (timer && timer._onTimeout) {
    timer._onTimeout = null;
    unenroll(timer);
  }
}
 
function setInterval(callback, repeat, arg1, arg2, arg3) {
  if (typeof callback !== 'function') {
    throw new ERR_INVALID_CALLBACK(callback);
  }
 
  let i, args;
  switch (arguments.length) {
    // fast cases
    case 1:
    case 2:
      break;
    case 3:
      args = [arg1];
      break;
    case 4:
      args = [arg1, arg2];
      break;
    default:
      args = [arg1, arg2, arg3];
      for (i = 5; i < arguments.length; i++) {
        // Extend array dynamically, makes .apply run much faster in v6.0.0
        args[i - 2] = arguments[i];
      }
      break;
  }
 
  const timeout = new Timeout(callback, repeat, args, true, true);
  insert(timeout, timeout._idleTimeout);
 
  return timeout;
}
 
function clearInterval(timer) {
  // clearTimeout and clearInterval can be used to clear timers created from
  // both setTimeout and setInterval, as specified by HTML Living Standard:
  // https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#dom-setinterval
  clearTimeout(timer);
}
 
Timeout.prototype.close = function() {
  clearTimeout(this);
  return this;
};
 
const Immediate = class Immediate {
  constructor(callback, args) {
    this._idleNext = null;
    this._idlePrev = null;
    this._onImmediate = callback;
    this._argv = args;
    this._destroyed = false;
    this[kRefed] = false;
 
    initAsyncResource(this, 'Immediate');
 
    this.ref();
    immediateInfo[kCount]++;
 
    immediateQueue.append(this);
  }
 
  ref() {
    if (this[kRefed] === false) {
      this[kRefed] = true;
      if (immediateInfo[kRefCount]++ === 0)
        toggleImmediateRef(true);
    }
    return this;
  }
 
  unref() {
    if (this[kRefed] === true) {
      this[kRefed] = false;
      if (--immediateInfo[kRefCount] === 0)
        toggleImmediateRef(false);
    }
    return this;
  }
 
  hasRef() {
    return !!this[kRefed];
  }
};
 
function setImmediate(callback, arg1, arg2, arg3) {
  if (typeof callback !== 'function') {
    throw new ERR_INVALID_CALLBACK(callback);
  }
 
  let i, args;
  switch (arguments.length) {
    // fast cases
    case 1:
      break;
    case 2:
      args = [arg1];
      break;
    case 3:
      args = [arg1, arg2];
      break;
    default:
      args = [arg1, arg2, arg3];
      for (i = 4; i < arguments.length; i++) {
        // Extend array dynamically, makes .apply run much faster in v6.0.0
        args[i - 1] = arguments[i];
      }
      break;
  }
 
  return new Immediate(callback, args);
}
 
setImmediate[customPromisify] = function(value) {
  return new Promise((resolve) => new Immediate(resolve, [value]));
};
 
function clearImmediate(immediate) {
  if (!immediate || immediate._destroyed)
    return;
 
  immediateInfo[kCount]--;
  immediate._destroyed = true;
 
  if (immediate[kRefed] && --immediateInfo[kRefCount] === 0)
    toggleImmediateRef(false);
  immediate[kRefed] = null;
 
  if (destroyHooksExist()) {
    emitDestroy(immediate[async_id_symbol]);
  }
 
  immediate._onImmediate = null;
 
  immediateQueue.remove(immediate);
}
 
module.exports = {
  setTimeout,
  clearTimeout,
  setImmediate,
  clearImmediate,
  setInterval,
  clearInterval,
  _unrefActive: deprecate(
    unrefActive,
    'timers._unrefActive() is deprecated.' +
    ' Please use timeout.refresh() instead.',
    'DEP0127'),
  active: deprecate(
    active,
    'timers.active() is deprecated. Please use timeout.refresh() instead.',
    'DEP0126'),
  unenroll: deprecate(
    unenroll,
    'timers.unenroll() is deprecated. Please use clearTimeout instead.',
    'DEP0096'),
  enroll: deprecate(
    enroll,
    'timers.enroll() is deprecated. Please use setTimeout instead.',
    'DEP0095')
};