ge-window.js 680 B

12345678910111213141516171819202122232425
  1. 'use strict'
  2. const { MAX_MSGID } = require('../constants')
  3. /**
  4. * Compare a reference id with another id to determine "greater than or equal"
  5. * between the two values according to a sliding window.
  6. *
  7. * @param {integer} ref
  8. * @param {integer} comp
  9. *
  10. * @returns {boolean} `true` if the `comp` value is >= to the `ref` value
  11. * within the computed window, otherwise `false`.
  12. */
  13. module.exports = function geWindow (ref, comp) {
  14. let max = ref + Math.floor(MAX_MSGID / 2)
  15. const min = ref
  16. if (max >= MAX_MSGID) {
  17. // Handle roll-over
  18. max = max - MAX_MSGID - 1
  19. return ((comp <= max) || (comp >= min))
  20. } else {
  21. return ((comp <= max) && (comp >= min))
  22. }
  23. }