Comparing version 1.0.0 to 1.1.0
135
lolex.js
@@ -130,32 +130,20 @@ !function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var f;"undefined"!=typeof window?f=window:"undefined"!=typeof global?f=global:"undefined"!=typeof self&&(f=self),f.lolex=e()}}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ | ||
function addTimer(clock, args, opt) { | ||
if (args.length === 0) { | ||
throw new Error("Function requires at least 1 parameter"); | ||
} | ||
if (typeof args[0] === "undefined") { | ||
function addTimer(clock, timer) { | ||
if (typeof timer.func === "undefined") { | ||
throw new Error("Callback must be provided to timer calls"); | ||
} | ||
var toId = id++; | ||
var delay = args[1] || 0; | ||
if (!clock.timeouts) { | ||
clock.timeouts = {}; | ||
if (!clock.timers) { | ||
clock.timers = {}; | ||
} | ||
clock.timeouts[toId] = { | ||
id: toId, | ||
func: args[0], | ||
callAt: clock.now + delay, | ||
invokeArgs: Array.prototype.slice.call(args, 2) | ||
}; | ||
timer.id = id++; | ||
timer.createdAt = clock.now; | ||
timer.callAt = clock.now + (timer.delay || 0); | ||
if (opt && opt.recurring) { | ||
clock.timeouts[toId].interval = delay; | ||
} | ||
clock.timers[timer.id] = timer; | ||
if (addTimerReturnsObject) { | ||
return { | ||
id: toId, | ||
id: timer.id, | ||
ref: function() {}, | ||
@@ -166,3 +154,3 @@ unref: function() {} | ||
else { | ||
return toId; | ||
return timer.id; | ||
} | ||
@@ -172,31 +160,58 @@ } | ||
function firstTimerInRange(clock, from, to) { | ||
var timer, smallest = null, originalTimer; | ||
var timers = clock.timers, timer = null; | ||
for (var id in clock.timeouts) { | ||
if (!inRange(from, to, clock.timeouts[id])) { | ||
for (var id in timers) { | ||
if (!inRange(from, to, timers[id])) { | ||
continue; | ||
} | ||
if (smallest === null || clock.timeouts[id].callAt < smallest) { | ||
originalTimer = clock.timeouts[id]; | ||
smallest = clock.timeouts[id].callAt; | ||
timer = { | ||
func: clock.timeouts[id].func, | ||
callAt: clock.timeouts[id].callAt, | ||
interval: clock.timeouts[id].interval, | ||
id: clock.timeouts[id].id, | ||
invokeArgs: clock.timeouts[id].invokeArgs | ||
}; | ||
if (!timer || ~compareTimers(timer, timers[id])) { | ||
timer = timers[id]; | ||
} | ||
} | ||
return timer || null; | ||
return timer; | ||
} | ||
function compareTimers(a, b) { | ||
// Sort first by absolute timing | ||
if (a.callAt < b.callAt) { | ||
return -1; | ||
} | ||
if (a.callAt > b.callAt) { | ||
return 1; | ||
} | ||
// Sort next by immediate, immediate timers take precedence | ||
if (a.immediate && !b.immediate) { | ||
return -1; | ||
} | ||
if (!a.immediate && b.immediate) { | ||
return 1; | ||
} | ||
// Sort next by creation time, earlier-created timers take precedence | ||
if (a.createdAt < b.createdAt) { | ||
return -1; | ||
} | ||
if (a.createdAt > b.createdAt) { | ||
return 1; | ||
} | ||
// Sort next by id, lower-id timers take precedence | ||
if (a.id < b.id) { | ||
return -1; | ||
} | ||
if (a.id > b.id) { | ||
return 1; | ||
} | ||
// As timer ids are unique, no fallback `0` is necessary | ||
} | ||
function callTimer(clock, timer) { | ||
if (typeof timer.interval == "number") { | ||
clock.timeouts[timer.id].callAt += timer.interval; | ||
clock.timers[timer.id].callAt += timer.interval; | ||
} else { | ||
delete clock.timeouts[timer.id]; | ||
delete clock.timers[timer.id]; | ||
} | ||
@@ -206,3 +221,3 @@ | ||
if (typeof timer.func == "function") { | ||
timer.func.apply(null, timer.invokeArgs); | ||
timer.func.apply(null, timer.args); | ||
} else { | ||
@@ -215,3 +230,3 @@ eval(timer.func); | ||
if (!clock.timeouts[timer.id]) { | ||
if (!clock.timers[timer.id]) { | ||
if (exception) { | ||
@@ -298,4 +313,8 @@ throw exception; | ||
clock.setTimeout = function setTimeout(callback, timeout) { | ||
return addTimer(clock, arguments); | ||
clock.setTimeout = function setTimeout(func, timeout) { | ||
return addTimer(clock, { | ||
func: func, | ||
args: Array.prototype.slice.call(arguments, 2), | ||
delay: timeout | ||
}); | ||
}; | ||
@@ -309,4 +328,4 @@ | ||
} | ||
if (!clock.timeouts) { | ||
clock.timeouts = []; | ||
if (!clock.timers) { | ||
clock.timers = []; | ||
} | ||
@@ -318,9 +337,14 @@ // in Node, timerId is an object with .ref()/.unref(), and | ||
} | ||
if (timerId in clock.timeouts) { | ||
delete clock.timeouts[timerId]; | ||
if (timerId in clock.timers) { | ||
delete clock.timers[timerId]; | ||
} | ||
}; | ||
clock.setInterval = function setInterval(callback, timeout) { | ||
return addTimer(clock, arguments, { recurring: true }); | ||
clock.setInterval = function setInterval(func, timeout) { | ||
return addTimer(clock, { | ||
func: func, | ||
args: Array.prototype.slice.call(arguments, 2), | ||
delay: timeout, | ||
interval: timeout | ||
}); | ||
}; | ||
@@ -332,5 +356,8 @@ | ||
clock.setImmediate = function setImmediate(callback) { | ||
var passThruArgs = Array.prototype.slice.call(arguments, 1); | ||
return addTimer(clock, [callback, 0].concat(passThruArgs)); | ||
clock.setImmediate = function setImmediate(func) { | ||
return addTimer(clock, { | ||
func: func, | ||
args: Array.prototype.slice.call(arguments, 1), | ||
immediate: true | ||
}); | ||
}; | ||
@@ -349,3 +376,3 @@ | ||
while (timer && tickFrom <= tickTo) { | ||
if (clock.timeouts[timer.id]) { | ||
if (clock.timers[timer.id]) { | ||
tickFrom = clock.now = timer.callAt; | ||
@@ -373,3 +400,3 @@ try { | ||
clock.reset = function reset() { | ||
clock.timeouts = {}; | ||
clock.timers = {}; | ||
}; | ||
@@ -376,0 +403,0 @@ |
{ | ||
"name": "lolex", | ||
"description": "Fake JavaScript timers", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"homepage": "http://github.com/sinonjs/lolex", | ||
@@ -6,0 +6,0 @@ "author": "Christian Johansen", |
135
src/lolex.js
@@ -128,32 +128,20 @@ /*jslint eqeqeq: false, plusplus: false, evil: true, onevar: false, browser: true, forin: false*/ | ||
function addTimer(clock, args, opt) { | ||
if (args.length === 0) { | ||
throw new Error("Function requires at least 1 parameter"); | ||
} | ||
if (typeof args[0] === "undefined") { | ||
function addTimer(clock, timer) { | ||
if (typeof timer.func === "undefined") { | ||
throw new Error("Callback must be provided to timer calls"); | ||
} | ||
var toId = id++; | ||
var delay = args[1] || 0; | ||
if (!clock.timeouts) { | ||
clock.timeouts = {}; | ||
if (!clock.timers) { | ||
clock.timers = {}; | ||
} | ||
clock.timeouts[toId] = { | ||
id: toId, | ||
func: args[0], | ||
callAt: clock.now + delay, | ||
invokeArgs: Array.prototype.slice.call(args, 2) | ||
}; | ||
timer.id = id++; | ||
timer.createdAt = clock.now; | ||
timer.callAt = clock.now + (timer.delay || 0); | ||
if (opt && opt.recurring) { | ||
clock.timeouts[toId].interval = delay; | ||
} | ||
clock.timers[timer.id] = timer; | ||
if (addTimerReturnsObject) { | ||
return { | ||
id: toId, | ||
id: timer.id, | ||
ref: function() {}, | ||
@@ -164,3 +152,3 @@ unref: function() {} | ||
else { | ||
return toId; | ||
return timer.id; | ||
} | ||
@@ -170,31 +158,58 @@ } | ||
function firstTimerInRange(clock, from, to) { | ||
var timer, smallest = null, originalTimer; | ||
var timers = clock.timers, timer = null; | ||
for (var id in clock.timeouts) { | ||
if (!inRange(from, to, clock.timeouts[id])) { | ||
for (var id in timers) { | ||
if (!inRange(from, to, timers[id])) { | ||
continue; | ||
} | ||
if (smallest === null || clock.timeouts[id].callAt < smallest) { | ||
originalTimer = clock.timeouts[id]; | ||
smallest = clock.timeouts[id].callAt; | ||
timer = { | ||
func: clock.timeouts[id].func, | ||
callAt: clock.timeouts[id].callAt, | ||
interval: clock.timeouts[id].interval, | ||
id: clock.timeouts[id].id, | ||
invokeArgs: clock.timeouts[id].invokeArgs | ||
}; | ||
if (!timer || ~compareTimers(timer, timers[id])) { | ||
timer = timers[id]; | ||
} | ||
} | ||
return timer || null; | ||
return timer; | ||
} | ||
function compareTimers(a, b) { | ||
// Sort first by absolute timing | ||
if (a.callAt < b.callAt) { | ||
return -1; | ||
} | ||
if (a.callAt > b.callAt) { | ||
return 1; | ||
} | ||
// Sort next by immediate, immediate timers take precedence | ||
if (a.immediate && !b.immediate) { | ||
return -1; | ||
} | ||
if (!a.immediate && b.immediate) { | ||
return 1; | ||
} | ||
// Sort next by creation time, earlier-created timers take precedence | ||
if (a.createdAt < b.createdAt) { | ||
return -1; | ||
} | ||
if (a.createdAt > b.createdAt) { | ||
return 1; | ||
} | ||
// Sort next by id, lower-id timers take precedence | ||
if (a.id < b.id) { | ||
return -1; | ||
} | ||
if (a.id > b.id) { | ||
return 1; | ||
} | ||
// As timer ids are unique, no fallback `0` is necessary | ||
} | ||
function callTimer(clock, timer) { | ||
if (typeof timer.interval == "number") { | ||
clock.timeouts[timer.id].callAt += timer.interval; | ||
clock.timers[timer.id].callAt += timer.interval; | ||
} else { | ||
delete clock.timeouts[timer.id]; | ||
delete clock.timers[timer.id]; | ||
} | ||
@@ -204,3 +219,3 @@ | ||
if (typeof timer.func == "function") { | ||
timer.func.apply(null, timer.invokeArgs); | ||
timer.func.apply(null, timer.args); | ||
} else { | ||
@@ -213,3 +228,3 @@ eval(timer.func); | ||
if (!clock.timeouts[timer.id]) { | ||
if (!clock.timers[timer.id]) { | ||
if (exception) { | ||
@@ -296,4 +311,8 @@ throw exception; | ||
clock.setTimeout = function setTimeout(callback, timeout) { | ||
return addTimer(clock, arguments); | ||
clock.setTimeout = function setTimeout(func, timeout) { | ||
return addTimer(clock, { | ||
func: func, | ||
args: Array.prototype.slice.call(arguments, 2), | ||
delay: timeout | ||
}); | ||
}; | ||
@@ -307,4 +326,4 @@ | ||
} | ||
if (!clock.timeouts) { | ||
clock.timeouts = []; | ||
if (!clock.timers) { | ||
clock.timers = []; | ||
} | ||
@@ -316,9 +335,14 @@ // in Node, timerId is an object with .ref()/.unref(), and | ||
} | ||
if (timerId in clock.timeouts) { | ||
delete clock.timeouts[timerId]; | ||
if (timerId in clock.timers) { | ||
delete clock.timers[timerId]; | ||
} | ||
}; | ||
clock.setInterval = function setInterval(callback, timeout) { | ||
return addTimer(clock, arguments, { recurring: true }); | ||
clock.setInterval = function setInterval(func, timeout) { | ||
return addTimer(clock, { | ||
func: func, | ||
args: Array.prototype.slice.call(arguments, 2), | ||
delay: timeout, | ||
interval: timeout | ||
}); | ||
}; | ||
@@ -330,5 +354,8 @@ | ||
clock.setImmediate = function setImmediate(callback) { | ||
var passThruArgs = Array.prototype.slice.call(arguments, 1); | ||
return addTimer(clock, [callback, 0].concat(passThruArgs)); | ||
clock.setImmediate = function setImmediate(func) { | ||
return addTimer(clock, { | ||
func: func, | ||
args: Array.prototype.slice.call(arguments, 1), | ||
immediate: true | ||
}); | ||
}; | ||
@@ -347,3 +374,3 @@ | ||
while (timer && tickFrom <= tickTo) { | ||
if (clock.timeouts[timer.id]) { | ||
if (clock.timers[timer.id]) { | ||
tickFrom = clock.now = timer.callAt; | ||
@@ -371,3 +398,3 @@ try { | ||
clock.reset = function reset() { | ||
clock.timeouts = {}; | ||
clock.timers = {}; | ||
}; | ||
@@ -374,0 +401,0 @@ |
@@ -165,2 +165,15 @@ /*jslint onevar: false, eqeqeq: false, plusplus: false*/ | ||
}); | ||
it("calls the given callback before setTimeout", function () { | ||
var stub1 = sinon.stub.create(); | ||
var stub2 = sinon.stub.create(); | ||
this.clock.setTimeout(stub1, 0); | ||
this.clock.setImmediate(stub2); | ||
this.clock.tick(0); | ||
assert(stub1.calledOnce); | ||
assert(stub2.calledOnce); | ||
assert(stub2.calledBefore(stub1)); | ||
}); | ||
}); | ||
@@ -167,0 +180,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
63216
1519