We need our array to be filled with zeros, not undefineds.

git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@5429 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Michael Bayne
2008-10-09 22:08:47 +00:00
parent 9f2682a5e4
commit b7214df1c2
+13 -5
View File
@@ -62,7 +62,7 @@ public class Throttle
*/ */
public function Throttle (operations :int, period :int) public function Throttle (operations :int, period :int)
{ {
_ops = new Array(operations); _ops = makeArray(operations);
_period = period; _period = period;
} }
@@ -72,11 +72,11 @@ public class Throttle
* if the limit is decreased. * if the limit is decreased.
* *
* @param operations the new maximum number of operations. * @param operations the new maximum number of operations.
* @param period the new period. * @param period the new period (in milliseconds).
*/ */
public function reinit (operations :int, period :int) :void public function reinit (operations :int, period :int) :void
{ {
var ops :Array = new Array(operations); var ops :Array = makeArray(operations);
// copy up to 'operations' of our most recent operations into our new array // copy up to 'operations' of our most recent operations into our new array
for (var ii :int = 0; ii < operations; ii++) { for (var ii :int = 0; ii < operations; ii++) {
ops[operations-ii-1] = _ops[(_oldestOp + _ops.length - ii - 1) % _ops.length]; ops[operations-ii-1] = _ops[(_oldestOp + _ops.length - ii - 1) % _ops.length];
@@ -125,8 +125,7 @@ public class Throttle
{ {
// if the oldest operation was performed less than _period ago, we need to throttle // if the oldest operation was performed less than _period ago, we need to throttle
var elapsed :int = timeStamp - _ops[_oldestOp]; var elapsed :int = timeStamp - _ops[_oldestOp];
// if negative time elapsed, we must be running on windows; let's just cope by not // cope with negative time elapsed (shouldn't happen) by not throttling
// throttling
return (elapsed >= 0 && elapsed < _period); return (elapsed >= 0 && elapsed < _period);
} }
@@ -161,6 +160,15 @@ public class Throttle
return _ops.length + " ops per " + _period + "ms (oldest " + oldest + ")"; return _ops.length + " ops per " + _period + "ms (oldest " + oldest + ")";
} }
protected function makeArray (operations :int) :Array
{
var ops :Array = new Array(operations);
for (var ii :int = 0; ii < operations; ii++) {
ops[ii] = 0;
}
return ops;
}
protected var _ops :Array; protected var _ops :Array;
protected var _oldestOp :int; protected var _oldestOp :int;
protected var _period :int; protected var _period :int;