Use arrow fns rather than self = this
This commit is contained in:
@@ -1,20 +1,19 @@
|
||||
getJasmineRequireObj().DelayedFunctionScheduler = function(j$) {
|
||||
function DelayedFunctionScheduler() {
|
||||
const self = this;
|
||||
const scheduledLookup = [];
|
||||
const scheduledFunctions = {};
|
||||
let currentTime = 0;
|
||||
let delayedFnCount = 0;
|
||||
let deletedKeys = [];
|
||||
this.scheduledLookup_ = [];
|
||||
this.scheduledFunctions_ = {};
|
||||
this.currentTime_ = 0;
|
||||
this.delayedFnCount_ = 0;
|
||||
this.deletedKeys_ = [];
|
||||
|
||||
self.tick = function(millis, tickDate) {
|
||||
this.tick = function(millis, tickDate) {
|
||||
millis = millis || 0;
|
||||
const endTime = currentTime + millis;
|
||||
const endTime = this.currentTime_ + millis;
|
||||
|
||||
runScheduledFunctions(endTime, tickDate);
|
||||
this.runScheduledFunctions_(endTime, tickDate);
|
||||
};
|
||||
|
||||
self.scheduleFunction = function(
|
||||
this.scheduleFunction = function(
|
||||
funcToCall,
|
||||
millis,
|
||||
params,
|
||||
@@ -33,8 +32,8 @@ getJasmineRequireObj().DelayedFunctionScheduler = function(j$) {
|
||||
}
|
||||
|
||||
millis = millis || 0;
|
||||
timeoutKey = timeoutKey || ++delayedFnCount;
|
||||
runAtMillis = runAtMillis || currentTime + millis;
|
||||
timeoutKey = timeoutKey || ++this.delayedFnCount_;
|
||||
runAtMillis = runAtMillis || this.currentTime_ + millis;
|
||||
|
||||
const funcToSchedule = {
|
||||
runAtMillis: runAtMillis,
|
||||
@@ -45,12 +44,12 @@ getJasmineRequireObj().DelayedFunctionScheduler = function(j$) {
|
||||
millis: millis
|
||||
};
|
||||
|
||||
if (runAtMillis in scheduledFunctions) {
|
||||
scheduledFunctions[runAtMillis].push(funcToSchedule);
|
||||
if (runAtMillis in this.scheduledFunctions_) {
|
||||
this.scheduledFunctions_[runAtMillis].push(funcToSchedule);
|
||||
} else {
|
||||
scheduledFunctions[runAtMillis] = [funcToSchedule];
|
||||
scheduledLookup.push(runAtMillis);
|
||||
scheduledLookup.sort(function(a, b) {
|
||||
this.scheduledFunctions_[runAtMillis] = [funcToSchedule];
|
||||
this.scheduledLookup_.push(runAtMillis);
|
||||
this.scheduledLookup_.sort(function(a, b) {
|
||||
return a - b;
|
||||
});
|
||||
}
|
||||
@@ -58,19 +57,19 @@ getJasmineRequireObj().DelayedFunctionScheduler = function(j$) {
|
||||
return timeoutKey;
|
||||
};
|
||||
|
||||
self.removeFunctionWithId = function(timeoutKey) {
|
||||
deletedKeys.push(timeoutKey);
|
||||
this.removeFunctionWithId = function(timeoutKey) {
|
||||
this.deletedKeys_.push(timeoutKey);
|
||||
|
||||
for (const runAtMillis in scheduledFunctions) {
|
||||
const funcs = scheduledFunctions[runAtMillis];
|
||||
for (const runAtMillis in this.scheduledFunctions_) {
|
||||
const funcs = this.scheduledFunctions_[runAtMillis];
|
||||
const i = indexOfFirstToPass(funcs, function(func) {
|
||||
return func.timeoutKey === timeoutKey;
|
||||
});
|
||||
|
||||
if (i > -1) {
|
||||
if (funcs.length === 1) {
|
||||
delete scheduledFunctions[runAtMillis];
|
||||
deleteFromLookup(runAtMillis);
|
||||
delete this.scheduledFunctions_[runAtMillis];
|
||||
this.deleteFromLookup_(runAtMillis);
|
||||
} else {
|
||||
funcs.splice(i, 1);
|
||||
}
|
||||
@@ -82,93 +81,99 @@ getJasmineRequireObj().DelayedFunctionScheduler = function(j$) {
|
||||
}
|
||||
};
|
||||
|
||||
return self;
|
||||
return this;
|
||||
}
|
||||
|
||||
function indexOfFirstToPass(array, testFn) {
|
||||
let index = -1;
|
||||
DelayedFunctionScheduler.prototype.runScheduledFunctions_ = function(
|
||||
endTime,
|
||||
tickDate
|
||||
) {
|
||||
tickDate = tickDate || function() {};
|
||||
if (
|
||||
this.scheduledLookup_.length === 0 ||
|
||||
this.scheduledLookup_[0] > endTime
|
||||
) {
|
||||
if (endTime >= this.currentTime_) {
|
||||
tickDate(endTime - this.currentTime_);
|
||||
this.currentTime_ = endTime;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
for (let i = 0; i < array.length; ++i) {
|
||||
if (testFn(array[i])) {
|
||||
index = i;
|
||||
break;
|
||||
do {
|
||||
this.deletedKeys_ = [];
|
||||
const newCurrentTime = this.scheduledLookup_.shift();
|
||||
if (newCurrentTime >= this.currentTime_) {
|
||||
tickDate(newCurrentTime - this.currentTime_);
|
||||
this.currentTime_ = newCurrentTime;
|
||||
}
|
||||
|
||||
const funcsToRun = this.scheduledFunctions_[this.currentTime_];
|
||||
|
||||
delete this.scheduledFunctions_[this.currentTime_];
|
||||
|
||||
for (const fn of funcsToRun) {
|
||||
if (fn.recurring) {
|
||||
this.reschedule_(fn);
|
||||
}
|
||||
}
|
||||
|
||||
return index;
|
||||
for (const fn of funcsToRun) {
|
||||
if (this.deletedKeys_.includes(fn.timeoutKey)) {
|
||||
// skip a timeoutKey deleted whilst we were running
|
||||
return;
|
||||
}
|
||||
fn.funcToCall.apply(null, fn.params || []);
|
||||
}
|
||||
this.deletedKeys_ = [];
|
||||
} while (
|
||||
this.scheduledLookup_.length > 0 &&
|
||||
// checking first if we're out of time prevents setTimeout(0)
|
||||
// scheduled in a funcToRun from forcing an extra iteration
|
||||
this.currentTime_ !== endTime &&
|
||||
this.scheduledLookup_[0] <= endTime
|
||||
);
|
||||
|
||||
// ran out of functions to call, but still time left on the clock
|
||||
if (endTime >= this.currentTime_) {
|
||||
tickDate(endTime - this.currentTime_);
|
||||
this.currentTime_ = endTime;
|
||||
}
|
||||
};
|
||||
|
||||
function deleteFromLookup(key) {
|
||||
const value = Number(key);
|
||||
const i = indexOfFirstToPass(scheduledLookup, function(millis) {
|
||||
return millis === value;
|
||||
});
|
||||
DelayedFunctionScheduler.prototype.reschedule_ = function(scheduledFn) {
|
||||
this.scheduleFunction(
|
||||
scheduledFn.funcToCall,
|
||||
scheduledFn.millis,
|
||||
scheduledFn.params,
|
||||
true,
|
||||
scheduledFn.timeoutKey,
|
||||
scheduledFn.runAtMillis + scheduledFn.millis
|
||||
);
|
||||
};
|
||||
|
||||
if (i > -1) {
|
||||
scheduledLookup.splice(i, 1);
|
||||
DelayedFunctionScheduler.prototype.deleteFromLookup_ = function(key) {
|
||||
const value = Number(key);
|
||||
const i = indexOfFirstToPass(this.scheduledLookup_, function(millis) {
|
||||
return millis === value;
|
||||
});
|
||||
|
||||
if (i > -1) {
|
||||
this.scheduledLookup_.splice(i, 1);
|
||||
}
|
||||
};
|
||||
|
||||
function indexOfFirstToPass(array, testFn) {
|
||||
let index = -1;
|
||||
|
||||
for (let i = 0; i < array.length; ++i) {
|
||||
if (testFn(array[i])) {
|
||||
index = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
function reschedule(scheduledFn) {
|
||||
self.scheduleFunction(
|
||||
scheduledFn.funcToCall,
|
||||
scheduledFn.millis,
|
||||
scheduledFn.params,
|
||||
true,
|
||||
scheduledFn.timeoutKey,
|
||||
scheduledFn.runAtMillis + scheduledFn.millis
|
||||
);
|
||||
}
|
||||
|
||||
function runScheduledFunctions(endTime, tickDate) {
|
||||
tickDate = tickDate || function() {};
|
||||
if (scheduledLookup.length === 0 || scheduledLookup[0] > endTime) {
|
||||
if (endTime >= currentTime) {
|
||||
tickDate(endTime - currentTime);
|
||||
currentTime = endTime;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
do {
|
||||
deletedKeys = [];
|
||||
const newCurrentTime = scheduledLookup.shift();
|
||||
if (newCurrentTime >= currentTime) {
|
||||
tickDate(newCurrentTime - currentTime);
|
||||
currentTime = newCurrentTime;
|
||||
}
|
||||
|
||||
const funcsToRun = scheduledFunctions[currentTime];
|
||||
|
||||
delete scheduledFunctions[currentTime];
|
||||
|
||||
for (const fn of funcsToRun) {
|
||||
if (fn.recurring) {
|
||||
reschedule(fn);
|
||||
}
|
||||
}
|
||||
|
||||
for (const fn of funcsToRun) {
|
||||
if (deletedKeys.includes(fn.timeoutKey)) {
|
||||
// skip a timeoutKey deleted whilst we were running
|
||||
return;
|
||||
}
|
||||
fn.funcToCall.apply(null, fn.params || []);
|
||||
}
|
||||
deletedKeys = [];
|
||||
} while (
|
||||
scheduledLookup.length > 0 &&
|
||||
// checking first if we're out of time prevents setTimeout(0)
|
||||
// scheduled in a funcToRun from forcing an extra iteration
|
||||
currentTime !== endTime &&
|
||||
scheduledLookup[0] <= endTime
|
||||
);
|
||||
|
||||
// ran out of functions to call, but still time left on the clock
|
||||
if (endTime >= currentTime) {
|
||||
tickDate(endTime - currentTime);
|
||||
currentTime = endTime;
|
||||
}
|
||||
}
|
||||
return index;
|
||||
}
|
||||
|
||||
return DelayedFunctionScheduler;
|
||||
|
||||
Reference in New Issue
Block a user