enqueue.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. 'use strict'
  2. /**
  3. * Adds requests to the queue. If a timeout has been added to the queue then
  4. * this will freeze the queue with the newly added item, flush it, and then
  5. * unfreeze it when the queue has been cleared.
  6. *
  7. * @param {object} message An LDAP message object.
  8. * @param {object} expect An expectation object.
  9. * @param {object} emitter An event emitter or `null`.
  10. * @param {function} cb A callback to invoke when the request is finished.
  11. *
  12. * @returns {boolean} `true` if the requested was queued. `false` if the queue
  13. * is not accepting any requests.
  14. */
  15. module.exports = function enqueue (message, expect, emitter, cb) {
  16. if (this._queue.size >= this.size || this._frozen) {
  17. return false
  18. }
  19. this._queue.add({ message, expect, emitter, cb })
  20. if (this.timeout === 0) return true
  21. if (this._timer === null) return true
  22. // A queue can have a specified time allotted for it to be cleared. If that
  23. // time has been reached, reject new entries until the queue has been cleared.
  24. this._timer = setTimeout(queueTimeout.bind(this), this.timeout)
  25. return true
  26. function queueTimeout () {
  27. this.freeze()
  28. this.purge()
  29. }
  30. }