Comparing version 1.3.1 to 1.3.2
109
lolex.js
@@ -21,4 +21,2 @@ !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){ | ||
global.clearTimeout = glbl.clearTimeout; | ||
global.setImmediate = glbl.setImmediate; | ||
global.clearImmediate = glbl.clearImmediate; | ||
global.setInterval = glbl.setInterval; | ||
@@ -28,2 +26,9 @@ global.clearInterval = glbl.clearInterval; | ||
// setImmediate is not a standard function | ||
// avoid adding the prop to the window object if not present | ||
if('setImmediate' in global) { | ||
global.setImmediate = glbl.setImmediate; | ||
global.clearImmediate = glbl.clearImmediate; | ||
} | ||
// node expects setTimeout/setInterval to return a fn object w/ .ref()/.unref() | ||
@@ -265,2 +270,40 @@ // browsers, a number. | ||
function timerType(timer) { | ||
if (timer.immediate) { | ||
return "Immediate"; | ||
} else if (typeof timer.interval !== "undefined") { | ||
return "Interval"; | ||
} else { | ||
return "Timeout"; | ||
} | ||
} | ||
function clearTimer(clock, timerId, ttype) { | ||
if (!timerId) { | ||
// null appears to be allowed in most browsers, and appears to be | ||
// relied upon by some libraries, like Bootstrap carousel | ||
return; | ||
} | ||
if (!clock.timers) { | ||
clock.timers = []; | ||
} | ||
// in Node, timerId is an object with .ref()/.unref(), and | ||
// its .id field is the actual timer id. | ||
if (typeof timerId === "object") { | ||
timerId = timerId.id; | ||
} | ||
if (clock.timers.hasOwnProperty(timerId)) { | ||
// check that the ID matches a timer of the correct type | ||
var timer = clock.timers[timerId]; | ||
if (timerType(timer) === ttype) { | ||
delete clock.timers[timerId]; | ||
} else { | ||
throw new Error("Cannot clear timer: timer created with set" + ttype + "() but cleared with clear" + timerType(timer) + "()"); | ||
} | ||
} | ||
} | ||
function uninstall(clock, target) { | ||
@@ -354,21 +397,3 @@ var method, | ||
clock.clearTimeout = function clearTimeout(timerId) { | ||
if (!timerId) { | ||
// null appears to be allowed in most browsers, and appears to be | ||
// relied upon by some libraries, like Bootstrap carousel | ||
return; | ||
} | ||
if (!clock.timers) { | ||
clock.timers = []; | ||
} | ||
// in Node, timerId is an object with .ref()/.unref(), and | ||
// its .id field is the actual timer id. | ||
if (typeof timerId === "object") { | ||
timerId = timerId.id; | ||
} | ||
if (clock.timers.hasOwnProperty(timerId)) { | ||
delete clock.timers[timerId]; | ||
} | ||
return clearTimer(clock, timerId, "Timeout"); | ||
}; | ||
@@ -386,3 +411,3 @@ | ||
clock.clearInterval = function clearInterval(timerId) { | ||
clock.clearTimeout(timerId); | ||
return clearTimer(clock, timerId, "Interval"); | ||
}; | ||
@@ -399,3 +424,3 @@ | ||
clock.clearImmediate = function clearImmediate(timerId) { | ||
clock.clearTimeout(timerId); | ||
return clearTimer(clock, timerId, "Immediate"); | ||
}; | ||
@@ -407,2 +432,3 @@ | ||
var timer = firstTimerInRange(clock, tickFrom, tickTo); | ||
var oldNow; | ||
@@ -416,3 +442,10 @@ clock.duringTick = true; | ||
try { | ||
oldNow = clock.now; | ||
callTimer(clock, timer); | ||
// compensate for any setSystemTime() call during timer callback | ||
if (oldNow !== clock.now) { | ||
tickFrom += clock.now - oldNow; | ||
tickTo += clock.now - oldNow; | ||
previous += clock.now - oldNow; | ||
} | ||
} catch (e) { | ||
@@ -441,17 +474,23 @@ firstException = firstException || e; | ||
return clock; | ||
} | ||
exports.createClock = createClock; | ||
clock.setSystemTime = function setSystemTime(now) { | ||
// determine time difference | ||
var newNow = getEpoch(now); | ||
var difference = newNow - clock.now; | ||
function detectKnownFailSituation(methods) { | ||
if (methods.indexOf("Date") < 0) { return; } | ||
// update 'system clock' | ||
clock.now = newNow; | ||
if (methods.indexOf("setTimeout") < 0) { | ||
throw new Error("Native setTimeout will not work when Date is faked"); | ||
} | ||
// update timers and intervals to keep them stable | ||
for (var id in clock.timers) { | ||
if (clock.timers.hasOwnProperty(id)) { | ||
var timer = clock.timers[id]; | ||
timer.createdAt += difference; | ||
timer.callAt += difference; | ||
} | ||
} | ||
}; | ||
if (methods.indexOf("setImmediate") < 0) { | ||
throw new Error("Native setImmediate will not work when Date is faked"); | ||
} | ||
return clock; | ||
} | ||
exports.createClock = createClock; | ||
@@ -484,4 +523,2 @@ exports.install = function install(target, now, toFake) { | ||
detectKnownFailSituation(clock.methods); | ||
for (i = 0, l = clock.methods.length; i < l; i++) { | ||
@@ -488,0 +525,0 @@ hijackMethod(target, clock.methods[i], clock); |
{ | ||
"name": "lolex", | ||
"description": "Fake JavaScript timers", | ||
"version": "1.3.1", | ||
"version": "1.3.2", | ||
"homepage": "http://github.com/sinonjs/lolex", | ||
@@ -6,0 +6,0 @@ "author": "Christian Johansen", |
@@ -19,4 +19,2 @@ /*global global, window*/ | ||
global.clearTimeout = glbl.clearTimeout; | ||
global.setImmediate = glbl.setImmediate; | ||
global.clearImmediate = glbl.clearImmediate; | ||
global.setInterval = glbl.setInterval; | ||
@@ -26,2 +24,9 @@ global.clearInterval = glbl.clearInterval; | ||
// setImmediate is not a standard function | ||
// avoid adding the prop to the window object if not present | ||
if('setImmediate' in global) { | ||
global.setImmediate = glbl.setImmediate; | ||
global.clearImmediate = glbl.clearImmediate; | ||
} | ||
// node expects setTimeout/setInterval to return a fn object w/ .ref()/.unref() | ||
@@ -28,0 +33,0 @@ // browsers, a number. |
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
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
76185
1809
2