All files / lib/internal/process task_queues.js

100% Statements 174/174
100% Branches 53/53
100% Functions 8/8
100% Lines 174/174

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 17522x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 63118x 63118x 63118x 22x 495157x 495157x 495157x 22x 22x 22x 22x 31559x 31559x 31559x 31559x 31559x 5568x 5568x 31559x 22x 189877x 189877x 189877x 192339x 624377x 624377x 624377x 624377x 624377x 624377x 74444x 624275x 549932x 549932x 549932x 549932x 549932x 549932x 549932x 549932x 549932x 624377x 624277x 624277x 624277x 623136x 623136x 623136x 191098x 189877x 189877x 189877x 189877x 22x 22x 22x 624726x 624726x 624726x 624726x 624726x 624619x 624619x 624619x 624726x 624726x 624726x 624726x 624726x 24158x 24158x 24158x 24158x 624619x 624619x 624708x 624619x 624619x 624619x 624619x 624619x 624619x 624619x 624619x 624619x 624716x 624619x 624726x 22x 215x 215x 215x 215x 215x 215x 215x 215x 215x 215x 22x 22x 22x 225x 225x 225x 225x 225x 225x 225x 225x 225x 225x 225x 22x 22x 22x 565x 565x 565x 565x 565x 565x 565x 565x 22x 22x 22x  
'use strict';
 
const {
  Array,
  FunctionPrototypeBind,
} = primordials;
 
const {
  // For easy access to the nextTick state in the C++ land,
  // and to avoid unnecessary calls into JS land.
  tickInfo,
  // Used to run V8's micro task queue.
  runMicrotasks,
  setTickCallback,
  enqueueMicrotask
} = internalBinding('task_queue');
 
const {
  setHasRejectionToWarn,
  hasRejectionToWarn,
  listenForRejections,
  processPromiseRejections
} = require('internal/process/promises');
 
const {
  getDefaultTriggerAsyncId,
  newAsyncId,
  initHooksExist,
  destroyHooksExist,
  emitInit,
  emitBefore,
  emitAfter,
  emitDestroy,
  symbols: { async_id_symbol, trigger_async_id_symbol }
} = require('internal/async_hooks');
const FixedQueue = require('internal/fixed_queue');
 
const {
  validateCallback,
  validateFunction,
} = require('internal/validators');
 
const { AsyncResource } = require('async_hooks');
 
// *Must* match Environment::TickInfo::Fields in src/env.h.
const kHasTickScheduled = 0;
 
function hasTickScheduled() {
  return tickInfo[kHasTickScheduled] === 1;
}
 
function setHasTickScheduled(value) {
  tickInfo[kHasTickScheduled] = value ? 1 : 0;
}
 
const queue = new FixedQueue();
 
// Should be in sync with RunNextTicksNative in node_task_queue.cc
function runNextTicks() {
  if (!hasTickScheduled() && !hasRejectionToWarn())
    runMicrotasks();
  if (!hasTickScheduled() && !hasRejectionToWarn())
    return;
 
  processTicksAndRejections();
}
 
function processTicksAndRejections() {
  let tock;
  do {
    while (tock = queue.shift()) {
      const asyncId = tock[async_id_symbol];
      emitBefore(asyncId, tock[trigger_async_id_symbol], tock);
 
      try {
        const callback = tock.callback;
        if (tock.args === undefined) {
          callback();
        } else {
          const args = tock.args;
          switch (args.length) {
            case 1: callback(args[0]); break;
            case 2: callback(args[0], args[1]); break;
            case 3: callback(args[0], args[1], args[2]); break;
            case 4: callback(args[0], args[1], args[2], args[3]); break;
            default: callback(...args);
          }
        }
      } finally {
        if (destroyHooksExist())
          emitDestroy(asyncId);
      }
 
      emitAfter(asyncId);
    }
    runMicrotasks();
  } while (!queue.isEmpty() || processPromiseRejections());
  setHasTickScheduled(false);
  setHasRejectionToWarn(false);
}
 
// `nextTick()` will not enqueue any callback when the process is about to
// exit since the callback would not have a chance to be executed.
function nextTick(callback) {
  validateCallback(callback);
 
  if (process._exiting)
    return;
 
  let args;
  switch (arguments.length) {
    case 1: break;
    case 2: args = [arguments[1]]; break;
    case 3: args = [arguments[1], arguments[2]]; break;
    case 4: args = [arguments[1], arguments[2], arguments[3]]; break;
    default:
      args = new Array(arguments.length - 1);
      for (let i = 1; i < arguments.length; i++)
        args[i - 1] = arguments[i];
  }
 
  if (queue.isEmpty())
    setHasTickScheduled(true);
  const asyncId = newAsyncId();
  const triggerAsyncId = getDefaultTriggerAsyncId();
  const tickObject = {
    [async_id_symbol]: asyncId,
    [trigger_async_id_symbol]: triggerAsyncId,
    callback,
    args
  };
  if (initHooksExist())
    emitInit(asyncId, 'TickObject', triggerAsyncId, tickObject);
  queue.push(tickObject);
}
 
function runMicrotask() {
  this.runInAsyncScope(() => {
    const callback = this.callback;
    try {
      callback();
    } finally {
      this.emitDestroy();
    }
  });
}
 
const defaultMicrotaskResourceOpts = { requireManualDestroy: true };
 
function queueMicrotask(callback) {
  validateFunction(callback, 'callback');
 
  const asyncResource = new AsyncResource(
    'Microtask',
    defaultMicrotaskResourceOpts
  );
  asyncResource.callback = callback;
 
  enqueueMicrotask(FunctionPrototypeBind(runMicrotask, asyncResource));
}
 
module.exports = {
  setupTaskQueue() {
    // Sets the per-isolate promise rejection callback
    listenForRejections();
    // Sets the callback to be run in every tick.
    setTickCallback(processTicksAndRejections);
    return {
      nextTick,
      runNextTicks
    };
  },
  queueMicrotask
};