Procházet zdrojové kódy

Refs #4915: fix bad and unnecessary manipulation in Cursor

Super-User před 12 roky
rodič
revize
1c02eb4938
1 změnil soubory, kde provedl 6 přidání a 4 odebrání
  1. 6 4
      lib/Cursor.js

+ 6 - 4
lib/Cursor.js

@@ -6,19 +6,21 @@
  **/
 var klass = module.exports = function Cursor(items){
 	if (!(items instanceof Array)) throw new Error("arg `items` must be an Array");
-	this.cachedData = items.slice(0);	// keep a copy
+	this.cachedData = items.slice(0);	// keep a copy so array changes when using async doc srcs do not cause side effects
+	this.length = items.length;
+	this.offset = 0;
 }, base = Object, proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
 
 proto.ok = function ok(){
-	return this.cachedData.length > 0 || this.hasOwnProperty("curr");
+	return (this.offset < this.length) || this.hasOwnProperty("curr");
 };
 
 proto.advance = function advance(){
-	if (this.cachedData.length === 0){
+	if (this.offset >= this.length){
 		delete this.curr;
 		return false;
 	}
-	this.curr = this.cachedData.shift();
+	this.curr = this.cachedData[this.offset++];
 	return this.curr;
 };