queue.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // Uses Node, AMD or browser globals to create a module.
  2. (function (root, factory) {
  3. if (typeof define === 'function' && define.amd) {
  4. // AMD. Register as an anonymous module.
  5. define([], factory);
  6. } else if (typeof exports === 'object') {
  7. // Node. Does not work with strict CommonJS, but
  8. // only CommonJS-like environments that support module.exports,
  9. // like Node.
  10. module.exports = factory();
  11. } else {
  12. // Browser globals (root is window)
  13. root.returnExports = factory();
  14. }
  15. }(this, function () {
  16. function Queue() {
  17. this.tail = [];
  18. this.head = [].slice.call(arguments);
  19. this.offset = 0;
  20. }
  21. Queue.prototype.shift = function () {
  22. if (this.offset === this.head.length) {
  23. var tmp = this.head;
  24. tmp.length = 0;
  25. this.head = this.tail;
  26. this.tail = tmp;
  27. this.offset = 0;
  28. if (this.head.length === 0) return;
  29. }
  30. return this.head[this.offset++];
  31. };
  32. Queue.prototype.push = function () {
  33. return [].push.apply(this.tail, arguments);
  34. };
  35. Object.defineProperty(Queue.prototype, "length", {
  36. get: function () {
  37. return this.head.length - this.offset + this.tail.length;
  38. },
  39. enumerable: false,
  40. configurable: true
  41. });
  42. return Queue;
  43. }));