Comparing version 2.1.3 to 2.2.0
v2.2.0 / 2017-11-07 | ||
================== | ||
* Add support for requestAnimationFrame | ||
* fix negative timeout bug | ||
v2.1.3 / 2017-10-03 | ||
@@ -3,0 +9,0 @@ ================== |
59
lolex.js
@@ -196,10 +196,19 @@ (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.lolex = f()}})(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){ | ||
timer.type = timer.immediate ? "Immediate" : "Timeout"; | ||
if (timer.hasOwnProperty("delay")) { | ||
timer.delay = timer.delay > maxTimeout ? 1 : timer.delay; | ||
timer.delay = Math.max(0, timer.delay); | ||
} | ||
if (timer.hasOwnProperty("interval")) { | ||
timer.type = "Interval"; | ||
timer.interval = timer.interval > maxTimeout ? 1 : timer.interval; | ||
} | ||
if (timer.hasOwnProperty("animation")) { | ||
timer.type = "AnimationFrame"; | ||
timer.animation = true; | ||
} | ||
if (!clock.timers) { | ||
@@ -329,12 +338,2 @@ clock.timers = {}; | ||
function timerType(timer) { | ||
if (timer.immediate) { | ||
return "Immediate"; | ||
} | ||
if (timer.interval !== undefined) { | ||
return "Interval"; | ||
} | ||
return "Timeout"; | ||
} | ||
function clearTimer(clock, timerId, ttype) { | ||
@@ -360,7 +359,9 @@ if (!timerId) { | ||
var timer = clock.timers[timerId]; | ||
if (timerType(timer) === ttype) { | ||
if (timer.type === ttype) { | ||
delete clock.timers[timerId]; | ||
} else { | ||
throw new Error("Cannot clear timer: timer created with set" + timerType(timer) | ||
+ "() but cleared with clear" + ttype + "()"); | ||
var clear = ttype === "AnimationFrame" ? "cancelAnimationFrame" : "clear" + ttype; | ||
var schedule = timer.type === "AnimationFrame" ? "requestAnimationFrame" : "set" + timer.type; | ||
throw new Error("Cannot clear timer: timer created with " + schedule | ||
+ "() but cleared with " + clear + "()"); | ||
} | ||
@@ -435,2 +436,4 @@ } | ||
clearInterval: clearInterval, | ||
requestAnimationFrame: global.requestAnimationFrame, | ||
cancelAnimationFrame: global.cancelAnimationFrame, | ||
Date: Date | ||
@@ -467,10 +470,11 @@ }; | ||
/** | ||
* @param now {Date|number} the system time | ||
* @param start {Date|number} the system time | ||
* @param loopLimit {number} maximum number of timers that will be run when calling runAll() | ||
*/ | ||
function createClock(now, loopLimit) { | ||
function createClock(start, loopLimit) { | ||
start = start || 0; | ||
loopLimit = loopLimit || 1000; | ||
var clock = { | ||
now: getEpoch(now), | ||
now: getEpoch(start), | ||
hrNow: 0, | ||
@@ -484,2 +488,6 @@ timeouts: {}, | ||
function getTimeToNextFrame() { | ||
return 16 - ((clock.now - start) % 16); | ||
} | ||
clock.setTimeout = function setTimeout(func, timeout) { | ||
@@ -527,2 +535,17 @@ return addTimer(clock, { | ||
clock.requestAnimationFrame = function requestAnimationFrame(func) { | ||
var result = addTimer(clock, { | ||
func: func, | ||
delay: getTimeToNextFrame(), | ||
args: [clock.now + getTimeToNextFrame()], | ||
animation: true | ||
}); | ||
return result.id || result; | ||
}; | ||
clock.cancelAnimationFrame = function cancelAnimationFrame(timerId) { | ||
return clearTimer(clock, timerId, "AnimationFrame"); | ||
}; | ||
function updateHrTime(newNow) { | ||
@@ -618,2 +641,6 @@ clock.hrNow += (newNow - clock.now); | ||
clock.runToFrame = function runToFrame() { | ||
return clock.tick(getTimeToNextFrame()); | ||
}; | ||
clock.runToLast = function runToLast() { | ||
@@ -620,0 +647,0 @@ var timer = lastTimer(clock); |
{ | ||
"name": "lolex", | ||
"description": "Fake JavaScript timers", | ||
"version": "2.1.3", | ||
"version": "2.2.0", | ||
"homepage": "http://github.com/sinonjs/lolex", | ||
@@ -6,0 +6,0 @@ "author": "Christian Johansen", |
# Lolex [![Build Status](https://secure.travis-ci.org/sinonjs/lolex.png)](http://travis-ci.org/sinonjs/lolex) [![bitHound Overall Score](https://www.bithound.io/github/sinonjs/lolex/badges/score.svg)](https://www.bithound.io/github/sinonjs/lolex) | ||
JavaScript implementation of the timer APIs; `setTimeout`, `clearTimeout`, | ||
`setImmediate`, `clearImmediate`, `setInterval` and `clearInterval`, along with | ||
a clock instance that controls the flow of time. Lolex also provides a `Date` | ||
implementation that gets its time from the clock. | ||
JavaScript implementation of the timer APIs; `setTimeout`, `clearTimeout`, `setImmediate`, `clearImmediate`, `setInterval`, `clearInterval`, `requestAnimationFrame`, and `clearAnimationFrame`, along with a clock instance that controls the flow of time. Lolex also provides a `Date` implementation that gets its time from the clock. | ||
In addition in browser environment lolex provides a `performance` implementation that gets its time from the clock. In Node environments lolex provides a `nextTick` implementation that is synchronized with the clock - and a `process.hrtime` shim that works with the clock. | ||
In addition in browser environment lolex provides a `performance` implementation that gets its time from the clock. In Node environments lolex provides a `nextTick` implementation that is synchronized with the clock - and a `process.hrtime` shim that works with the clock. | ||
Lolex can be used to simulate passing time in automated tests and other | ||
situations where you want the scheduling semantics, but don't want to actually | ||
wait (however, from version 2.0 lolex supports those of you who would like to wait too). | ||
wait (however, from version 2.0 lolex supports those of you who would like to wait too). | ||
@@ -54,3 +51,3 @@ Lolex is extracted from [Sinon.JS](https://github.com/sinonjs/sinon.js). | ||
The `next`, `runAll`, and `runToLast` methods are available to advance the clock. See the | ||
The `next`, `runAll`, `runToFrame`, and `runToLast` methods are available to advance the clock. See the | ||
API Reference for more details. | ||
@@ -106,3 +103,3 @@ | ||
configurable interval `config.advanceTimeDelta` (default: 20ms). Meaning time would | ||
be incremented every 20ms, not in real time. | ||
be incremented every 20ms, not in real time. | ||
@@ -145,4 +142,4 @@ An example would be: | ||
--------- | ---- | ------- | ------------ | ||
`config.target`| Object | global | installs lolex onto the specified target context | ||
`config.now` | Number/Date | 0 | installs lolex with the specified unix epoch | ||
`config.target`| Object | global | installs lolex onto the specified target context | ||
`config.now` | Number/Date | 0 | installs lolex with the specified unix epoch | ||
`config.toFake` | String[] | ["setTimeout", "clearTimeout", "setImmediate", "clearImmediate","setInterval", "clearInterval", "Date"] | an array with explicit function names to hijack. *When not set, lolex will automatically fake all methods **except** `nextTick`* e.g., `lolex.install({ toFake: ["setTimeout","nextTick"]})` will fake only `setTimeout` and `nextTick` | ||
@@ -199,2 +196,12 @@ `config.loopLimit` | Number | 1000 | the maximum number of timers that will be run when calling runAll() | ||
### `clock.requestAnimationFrame(callback)` | ||
Schedules the callback to be fired on the next animation frame, which runs every | ||
16 ticks. Returns an `id` which can be used to cancel the callback. This is | ||
available in both browser & node environments. | ||
### `clock.clearAnimationFrame(id)` | ||
Cancels the callback scheduled by the provided id. | ||
### `clock.hrtime(prevTime?)` | ||
@@ -233,2 +240,7 @@ Only available in Node.js, mimicks process.hrtime(). | ||
### `lock.runToFrame()` | ||
Advances the clock to the next frame, firing all scheduled animation frame callbacks, | ||
if any, for that frame as well as any other timers scheduled along the way. | ||
### `clock.runToLast()` | ||
@@ -235,0 +247,0 @@ |
@@ -194,10 +194,19 @@ "use strict"; | ||
timer.type = timer.immediate ? "Immediate" : "Timeout"; | ||
if (timer.hasOwnProperty("delay")) { | ||
timer.delay = timer.delay > maxTimeout ? 1 : timer.delay; | ||
timer.delay = Math.max(0, timer.delay); | ||
} | ||
if (timer.hasOwnProperty("interval")) { | ||
timer.type = "Interval"; | ||
timer.interval = timer.interval > maxTimeout ? 1 : timer.interval; | ||
} | ||
if (timer.hasOwnProperty("animation")) { | ||
timer.type = "AnimationFrame"; | ||
timer.animation = true; | ||
} | ||
if (!clock.timers) { | ||
@@ -327,12 +336,2 @@ clock.timers = {}; | ||
function timerType(timer) { | ||
if (timer.immediate) { | ||
return "Immediate"; | ||
} | ||
if (timer.interval !== undefined) { | ||
return "Interval"; | ||
} | ||
return "Timeout"; | ||
} | ||
function clearTimer(clock, timerId, ttype) { | ||
@@ -358,7 +357,9 @@ if (!timerId) { | ||
var timer = clock.timers[timerId]; | ||
if (timerType(timer) === ttype) { | ||
if (timer.type === ttype) { | ||
delete clock.timers[timerId]; | ||
} else { | ||
throw new Error("Cannot clear timer: timer created with set" + timerType(timer) | ||
+ "() but cleared with clear" + ttype + "()"); | ||
var clear = ttype === "AnimationFrame" ? "cancelAnimationFrame" : "clear" + ttype; | ||
var schedule = timer.type === "AnimationFrame" ? "requestAnimationFrame" : "set" + timer.type; | ||
throw new Error("Cannot clear timer: timer created with " + schedule | ||
+ "() but cleared with " + clear + "()"); | ||
} | ||
@@ -433,2 +434,4 @@ } | ||
clearInterval: clearInterval, | ||
requestAnimationFrame: global.requestAnimationFrame, | ||
cancelAnimationFrame: global.cancelAnimationFrame, | ||
Date: Date | ||
@@ -465,10 +468,11 @@ }; | ||
/** | ||
* @param now {Date|number} the system time | ||
* @param start {Date|number} the system time | ||
* @param loopLimit {number} maximum number of timers that will be run when calling runAll() | ||
*/ | ||
function createClock(now, loopLimit) { | ||
function createClock(start, loopLimit) { | ||
start = start || 0; | ||
loopLimit = loopLimit || 1000; | ||
var clock = { | ||
now: getEpoch(now), | ||
now: getEpoch(start), | ||
hrNow: 0, | ||
@@ -482,2 +486,6 @@ timeouts: {}, | ||
function getTimeToNextFrame() { | ||
return 16 - ((clock.now - start) % 16); | ||
} | ||
clock.setTimeout = function setTimeout(func, timeout) { | ||
@@ -525,2 +533,17 @@ return addTimer(clock, { | ||
clock.requestAnimationFrame = function requestAnimationFrame(func) { | ||
var result = addTimer(clock, { | ||
func: func, | ||
delay: getTimeToNextFrame(), | ||
args: [clock.now + getTimeToNextFrame()], | ||
animation: true | ||
}); | ||
return result.id || result; | ||
}; | ||
clock.cancelAnimationFrame = function cancelAnimationFrame(timerId) { | ||
return clearTimer(clock, timerId, "AnimationFrame"); | ||
}; | ||
function updateHrTime(newNow) { | ||
@@ -616,2 +639,6 @@ clock.hrNow += (newNow - clock.now); | ||
clock.runToFrame = function runToFrame() { | ||
return clock.tick(getTimeToNextFrame()); | ||
}; | ||
clock.runToLast = function runToLast() { | ||
@@ -618,0 +645,0 @@ var timer = lastTimer(clock); |
Sorry, the diff of this file is not supported yet
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
67771
1275
302