flush.js 750 B

123456789101112131415161718192021222324
  1. 'use strict'
  2. /**
  3. * Invokes all requests in the queue by passing them to the supplied callback
  4. * function and then clears all items from the queue.
  5. *
  6. * @param {function} cb A function used to handle the requests.
  7. */
  8. module.exports = function flush (cb) {
  9. if (this._timer) {
  10. clearTimeout(this._timer)
  11. this._timer = null
  12. }
  13. // We must get a local copy of the queue and clear it before iterating it.
  14. // The client will invoke this flush function _many_ times. If we try to
  15. // iterate it without a local copy and clearing first then we will overflow
  16. // the stack.
  17. const requests = Array.from(this._queue.values())
  18. this._queue.clear()
  19. for (const req of requests) {
  20. cb(req.message, req.expect, req.emitter, req.cb)
  21. }
  22. }