Comparing version 0.1.7 to 0.1.8
{ | ||
"name": "ric", | ||
"version": "0.1.7", | ||
"version": "0.1.8", | ||
"description": "requestIdleCallback polyfill", | ||
@@ -5,0 +5,0 @@ "main": "./src/ric-polyfill.js", |
@@ -14,3 +14,3 @@ /** | ||
var IDLE_ENOUGH_DELAY = 300; | ||
var timeout = null; | ||
var timeoutId = null; | ||
var callbacks = []; | ||
@@ -23,26 +23,18 @@ var lastInteractionTime = Date.now(); | ||
var isFree = function () { | ||
return timeout === null; | ||
return timeoutId === null; | ||
} | ||
var onContinousInteraction = function (interactionName) { | ||
var onContinousInteractionStarts = function (interactionName) { | ||
deadline.timeRemaining = 0; | ||
lastInteractionTime = Date.now(); | ||
if (!timeout) { | ||
timeout = setTimeout(timeoutCompleted, IDLE_ENOUGH_DELAY); | ||
if (!timeoutId) { | ||
timeoutId = setTimeout(timeoutCompleted, IDLE_ENOUGH_DELAY); | ||
} | ||
} | ||
//Consider categorizing last interaction timestamp in order to add cancelling events like touchend, touchleave, touchcancel, mouseup, mouseout, mouseleave | ||
document.addEventListener('keydown', onContinousInteraction.bind(this, 'keydown')); | ||
document.addEventListener('mousedown', onContinousInteraction.bind(this, 'mousedown')); | ||
document.addEventListener('touchstart', onContinousInteraction.bind(this, 'touchstart')); | ||
document.addEventListener('touchmove', onContinousInteraction.bind(this, 'touchmove')); | ||
document.addEventListener('mousemove', onContinousInteraction.bind(this, 'mousemove')); | ||
document.addEventListener('scroll', onContinousInteraction.bind(this, 'scroll')); | ||
var onContinousInteractionEnds = function (interactionName) { | ||
clearTimeout(timeoutId); | ||
timeoutId = null; | ||
var onContinousInteractionEnd = function (interactionName) { | ||
clearTimeout(timeout); | ||
timeout = null; | ||
for (var i = 0; i < callbacks.length; i++) { | ||
@@ -53,2 +45,11 @@ executeCallback(callbacks[i]) | ||
//Consider categorizing last interaction timestamp in order to add cancelling events like touchend, touchleave, touchcancel, mouseup, mouseout, mouseleave | ||
document.addEventListener('keydown', onContinousInteractionStarts.bind(this, 'keydown')); | ||
document.addEventListener('mousedown', onContinousInteractionStarts.bind(this, 'mousedown')); | ||
document.addEventListener('touchstart', onContinousInteractionStarts.bind(this, 'touchstart')); | ||
document.addEventListener('touchmove', onContinousInteractionStarts.bind(this, 'touchmove')); | ||
document.addEventListener('mousemove', onContinousInteractionStarts.bind(this, 'mousemove')); | ||
document.addEventListener('scroll', onContinousInteractionStarts.bind(this, 'scroll'), true); | ||
var timeoutCompleted = function () { | ||
@@ -59,15 +60,23 @@ var expectedEndTime = lastInteractionTime + IDLE_ENOUGH_DELAY; | ||
if (delta > 0) { | ||
timeout = setTimeout(timeoutCompleted, delta); | ||
timeoutId = setTimeout(timeoutCompleted, delta); | ||
} else { | ||
onContinousInteractionEnd(); | ||
onContinousInteractionEnds(); | ||
} | ||
} | ||
var addCallback = function (callback, timeout) { | ||
callbacks.push({ | ||
var createCallbackObject = function (callback, timeout) { | ||
var callbackObject = { | ||
callback: callback, | ||
timeout: timeout !== undefined ? setTimeout(executeCallback.bind(this, {callback: callback}), timeout) : undefined | ||
}); | ||
timeoutId: null | ||
}; | ||
callbackObject.timeoutId = timeout !== undefined ? setTimeout(executeCallback.bind(this, callbackObject), timeout) : null; | ||
return callbackObject; | ||
} | ||
var addCallback = function (callbackObject, timeout) { | ||
callbacks.push(callbackObject); | ||
} | ||
var executeCallback = function (callbackObject) { | ||
@@ -82,5 +91,5 @@ var callbackIndex = callbacks.indexOf(callbackObject); | ||
if (callbackObject.timeout) { | ||
clearTimeout(callbackObject.timeout); | ||
callbackObject.timeout = null; | ||
if (callbackObject.timeoutId) { | ||
clearTimeout(callbackObject.timeoutId); | ||
callbackObject.timeoutId = null; | ||
} | ||
@@ -90,9 +99,8 @@ } | ||
return function (callback, timeout) { | ||
var callbackObject = createCallbackObject(callback, timeout); | ||
if (isFree()) { | ||
executeCallback({ | ||
callback: callback, | ||
timeout: timeout | ||
}); | ||
executeCallback(callbackObject); | ||
} else { | ||
addCallback(callback, timeout); | ||
addCallback(callbackObject); | ||
} | ||
@@ -102,3 +110,6 @@ }; | ||
window.requestIdleUICallback = applyPolyfill(); | ||
window.requestIdleCallback = window.requestIdleCallback || applyPolyfill(); | ||
window.activateUIIdle = function () { | ||
window.requestUserIdle = applyPolyfill(); | ||
}; | ||
window.requestIdleCallback = window.requestIdleCallback || window.requestIdleUICallback; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
43371
11
85