index.js 714 B

123456789101112131415161718192021222324252627282930313233343536
  1. const FixedFIFO = require('./fixed-size')
  2. module.exports = class FastFIFO {
  3. constructor (hwm) {
  4. this.hwm = hwm || 16
  5. this.head = new FixedFIFO(this.hwm)
  6. this.tail = this.head
  7. }
  8. push (val) {
  9. if (!this.head.push(val)) {
  10. const prev = this.head
  11. this.head = prev.next = new FixedFIFO(2 * this.head.buffer.length)
  12. this.head.push(val)
  13. }
  14. }
  15. shift () {
  16. const val = this.tail.shift()
  17. if (val === undefined && this.tail.next) {
  18. const next = this.tail.next
  19. this.tail.next = null
  20. this.tail = next
  21. return this.tail.shift()
  22. }
  23. return val
  24. }
  25. peek () {
  26. return this.tail.peek()
  27. }
  28. isEmpty () {
  29. return this.head.isEmpty()
  30. }
  31. }