{"version":3,"file":"fixed-queue.mjs","sources":["../../../lib/dispatcher/fixed-queue.js"],"sourcesContent":["'use strict'\n\n// Extracted from node/lib/internal/fixed_queue.js\n\n// Currently optimal queue size, tested on V8 6.0 - 6.6. Must be power of two.\nconst kSize = 2048\nconst kMask = kSize - 1\n\n// The FixedQueue is implemented as a singly-linked list of fixed-size\n// circular buffers. It looks something like this:\n//\n//  head                                                       tail\n//    |                                                          |\n//    v                                                          v\n// +-----------+ <-----\\       +-----------+ <------\\         +-----------+\n// |  [null]   |        \\----- |   next    |         \\------- |   next    |\n// +-----------+               +-----------+                  +-----------+\n// |   item    | <-- bottom    |   item    | <-- bottom       | undefined |\n// |   item    |               |   item    |                  | undefined |\n// |   item    |               |   item    |                  | undefined |\n// |   item    |               |   item    |                  | undefined |\n// |   item    |               |   item    |       bottom --> |   item    |\n// |   item    |               |   item    |                  |   item    |\n// |    ...    |               |    ...    |                  |    ...    |\n// |   item    |               |   item    |                  |   item    |\n// |   item    |               |   item    |                  |   item    |\n// | undefined | <-- top       |   item    |                  |   item    |\n// | undefined |               |   item    |                  |   item    |\n// | undefined |               | undefined | <-- top  top --> | undefined |\n// +-----------+               +-----------+                  +-----------+\n//\n// Or, if there is only one circular buffer, it looks something\n// like either of these:\n//\n//  head   tail                                 head   tail\n//    |     |                                     |     |\n//    v     v                                     v     v\n// +-----------+                               +-----------+\n// |  [null]   |                               |  [null]   |\n// +-----------+                               +-----------+\n// | undefined |                               |   item    |\n// | undefined |                               |   item    |\n// |   item    | <-- bottom            top --> | undefined |\n// |   item    |                               | undefined |\n// | undefined | <-- top            bottom --> |   item    |\n// | undefined |                               |   item    |\n// +-----------+                               +-----------+\n//\n// Adding a value means moving `top` forward by one, removing means\n// moving `bottom` forward by one. After reaching the end, the queue\n// wraps around.\n//\n// When `top === bottom` the current queue is empty and when\n// `top + 1 === bottom` it's full. This wastes a single space of storage\n// but allows much quicker checks.\n\n/**\n * @type {FixedCircularBuffer}\n * @template T\n */\nclass FixedCircularBuffer {\n  /** @type {number} */\n  bottom = 0\n  /** @type {number} */\n  top = 0\n  /** @type {Array<T|undefined>} */\n  list = new Array(kSize).fill(undefined)\n  /** @type {T|null} */\n  next = null\n\n  /** @returns {boolean} */\n  isEmpty () {\n    return this.top === this.bottom\n  }\n\n  /** @returns {boolean} */\n  isFull () {\n    return ((this.top + 1) & kMask) === this.bottom\n  }\n\n  /**\n   * @param {T} data\n   * @returns {void}\n   */\n  push (data) {\n    this.list[this.top] = data\n    this.top = (this.top + 1) & kMask\n  }\n\n  /** @returns {T|null} */\n  shift () {\n    const nextItem = this.list[this.bottom]\n    if (nextItem === undefined) { return null }\n    this.list[this.bottom] = undefined\n    this.bottom = (this.bottom + 1) & kMask\n    return nextItem\n  }\n}\n\n/**\n * @template T\n */\nmodule.exports = class FixedQueue {\n  constructor () {\n    /** @type {FixedCircularBuffer<T>} */\n    this.head = this.tail = new FixedCircularBuffer()\n  }\n\n  /** @returns {boolean} */\n  isEmpty () {\n    return this.head.isEmpty()\n  }\n\n  /** @param {T} data */\n  push (data) {\n    if (this.head.isFull()) {\n      // Head is full: Creates a new queue, sets the old queue's `.next` to it,\n      // and sets it as the new main queue.\n      this.head = this.head.next = new FixedCircularBuffer()\n    }\n    this.head.push(data)\n  }\n\n  /** @returns {T|null} */\n  shift () {\n    const tail = this.tail\n    const next = tail.shift()\n    if (tail.isEmpty() && tail.next !== null) {\n      // If there is another queue, it forms the new tail.\n      this.tail = tail.next\n      tail.next = null\n    }\n    return next\n  }\n}\n"],"names":["FixedCircularBuffer","bottom","top","list","Array","fill","undefined","next","isEmpty","this","isFull","kSize","push","data","shift","nextItem","fixedQueue","constructor","head","tail"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4DA,MAAMA;;AAEJC,OAAS;sBAETC,IAAM;kCAENC,KAAO,IAAIC,MA7DC,MA6DYC,UAAKC;sBAE7BC,KAAO;0BAGP,OAAAC,GACE,OAAOC,KAAKP,MAAQO,KAAKR,MAC7B;yBAGE,MAAAS,GACE,OAASD,KAAKP,IAAM,EAvEVS,QAuE0BF,KAAKR,MAC7C;;;;KAME,IAAAW,CAAMC,MACJJ,KAAKN,KAAKM,KAAKP,KAAOW,KACtBJ,KAAKP,IAAOO,KAAKP,IAAM,EAhFbS,IAiFd;wBAGE,KAAAG,GACE,MAAMC,SAAWN,KAAKN,KAAKM,KAAKR,QAChC,YAAiBK,IAAbS,SAAiC,MACrCN,KAAKN,KAAKM,KAAKR,aAAUK,EACzBG,KAAKR,OAAUQ,KAAKR,OAAS,EAxFnBU,KAyFHI,SACX;;;OAMAC,WAAiB,MACf,WAAAC;;AAEER,KAAKS,KAAOT,KAAKU,KAAO,IAAInB,mBAChC;yBAGE,OAAAQ,GACE,OAAOC,KAAKS,KAAKV,SACrB;sBAGE,IAAAI,CAAMC,MACAJ,KAAKS,KAAKR;;;AAGZD,KAAKS,KAAOT,KAAKS,KAAKX,KAAO,IAAIP,qBAEnCS,KAAKS,KAAKN,KAAKC,KACnB;wBAGE,KAAAC,GACE,MAAMK,KAAOV,KAAKU,KACZZ,KAAOY,KAAKL,QAMlB,OALIK,KAAKX,WAA2B,OAAdW,KAAKZ;;AAEzBE,KAAKU,KAAOA,KAAKZ,KACjBY,KAAKZ,KAAO,MAEPA,IACX"}