requestanimationframe-timer
Advanced tools
Comparing version 1.0.5 to 2.0.0
@@ -5,2 +5,11 @@ # Changelog | ||
## [2.0.0](https://github.com/kambing86/requestanimationframe-timer/compare/v1.0.5...v2.0.0) (2019-12-30) | ||
### ⚠ BREAKING CHANGES | ||
* use babel 7 and only node >=10.18 | ||
* use babel 7 and only node >=10.18 ([c5122f0](https://github.com/kambing86/requestanimationframe-timer/commit/c5122f0974e68e60cbf310ef3679a087453da702)) | ||
### [1.0.5](https://github.com/kambing86/requestanimationframe-timer/compare/v1.0.4...v1.0.5) (2019-09-19) | ||
@@ -7,0 +16,0 @@ |
@@ -1,3 +0,3 @@ | ||
'use strict'; | ||
"use strict"; | ||
module.exports = require('./index').default; |
@@ -1,2 +0,2 @@ | ||
'use strict'; | ||
"use strict"; | ||
@@ -6,18 +6,15 @@ Object.defineProperty(exports, "__esModule", { | ||
}); | ||
exports.default = void 0; | ||
var _raf = require('raf'); | ||
var _raf = _interopRequireDefault(require("raf")); | ||
var _raf2 = _interopRequireDefault(_raf); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } | ||
const MODE_TIMEOUT = 0; | ||
const MODE_INTERVAL = 1; | ||
const fnStacks = new Map(); | ||
const runArray = new Set(); | ||
let rafStarted = false; | ||
let startId = 0; | ||
var MODE_TIMEOUT = 0; | ||
var MODE_INTERVAL = 1; | ||
var fnStacks = new Map(); | ||
var runArray = new Set(); | ||
var rafStarted = false; | ||
var startId = 0; | ||
function getTimeStamp() { | ||
@@ -28,6 +25,7 @@ return new Date().getTime(); | ||
function executeFn(value) { | ||
var fn = value.fn, | ||
args = value.args; | ||
fn.apply(undefined, _toConsumableArray(args)); | ||
const { | ||
fn, | ||
args | ||
} = value; | ||
fn(...args); | ||
} | ||
@@ -41,25 +39,27 @@ | ||
var checkTick = function checkTick(currentTimeTick) { | ||
return function (value, id) { | ||
var nextTick = value.nextTick, | ||
ms = value.ms, | ||
mode = value.mode; | ||
const checkTick = currentTimeTick => (value, id) => { | ||
const { | ||
nextTick, | ||
ms, | ||
mode | ||
} = value; | ||
if (currentTimeTick - nextTick >= 0) { | ||
runArray.add(value); | ||
if (mode === MODE_TIMEOUT) { | ||
fnStacks.delete(id); | ||
} else { | ||
fnStacks.set(id, Object.assign({}, value, { | ||
nextTick: nextTick + ms | ||
})); | ||
} | ||
if (currentTimeTick - nextTick >= 0) { | ||
runArray.add(value); | ||
if (mode === MODE_TIMEOUT) { | ||
fnStacks.delete(id); | ||
} else { | ||
fnStacks.set(id, { ...value, | ||
nextTick: nextTick + ms | ||
}); | ||
} | ||
}; | ||
} | ||
}; | ||
function loop() { | ||
var currentTimeTick = getTimeStamp(); | ||
const currentTimeTick = getTimeStamp(); | ||
fnStacks.forEach(checkTick(currentTimeTick)); | ||
runFunction(); | ||
if (fnStacks.size === 0) { | ||
@@ -69,25 +69,27 @@ rafStarted = false; | ||
} | ||
(0, _raf2.default)(loop); | ||
(0, _raf.default)(loop); | ||
} | ||
function addId(_ref) { | ||
var fn = _ref.fn, | ||
_ref$ms = _ref.ms, | ||
ms = _ref$ms === undefined ? 0 : _ref$ms, | ||
args = _ref.args, | ||
mode = _ref.mode; | ||
function addId({ | ||
fn, | ||
ms = 0, | ||
args, | ||
mode | ||
}) { | ||
if (!fn) return null; | ||
var currentId = startId; | ||
const currentId = startId; | ||
fnStacks.set(currentId, { | ||
fn: fn, | ||
ms: ms, | ||
fn, | ||
ms, | ||
nextTick: getTimeStamp() + ms, | ||
args: args, | ||
mode: mode | ||
args, | ||
mode | ||
}); | ||
if (!rafStarted) { | ||
rafStarted = true; | ||
(0, _raf2.default)(loop); | ||
(0, _raf.default)(loop); | ||
} | ||
startId += 1; | ||
@@ -101,2 +103,3 @@ return currentId; | ||
} | ||
if (fnStacks.size === 0) { | ||
@@ -107,21 +110,18 @@ rafStarted = false; | ||
exports.default = { | ||
setTimeout: function setTimeout(fn) { | ||
for (var _len = arguments.length, args = Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) { | ||
args[_key - 2] = arguments[_key]; | ||
} | ||
var ms = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0; | ||
return addId({ fn: fn, ms: ms, args: args, mode: MODE_TIMEOUT }); | ||
}, | ||
var _default = { | ||
setTimeout: (fn, ms = 0, ...args) => addId({ | ||
fn, | ||
ms, | ||
args, | ||
mode: MODE_TIMEOUT | ||
}), | ||
clearTimeout: removeId, | ||
setInterval: function setInterval(fn) { | ||
for (var _len2 = arguments.length, args = Array(_len2 > 2 ? _len2 - 2 : 0), _key2 = 2; _key2 < _len2; _key2++) { | ||
args[_key2 - 2] = arguments[_key2]; | ||
} | ||
var ms = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0; | ||
return addId({ fn: fn, ms: ms, args: args, mode: MODE_INTERVAL }); | ||
}, | ||
setInterval: (fn, ms = 0, ...args) => addId({ | ||
fn, | ||
ms, | ||
args, | ||
mode: MODE_INTERVAL | ||
}), | ||
clearInterval: removeId | ||
}; | ||
}; | ||
exports.default = _default; |
{ | ||
"name": "requestanimationframe-timer", | ||
"version": "1.0.5", | ||
"version": "2.0.0", | ||
"description": "setTimeout and setInterval by using requestAnimationFrame", | ||
@@ -29,3 +29,3 @@ "keywords": [ | ||
"clean": "del-cli dist", | ||
"lint": "eslint --cache src test", | ||
"lint": "eslint src test", | ||
"lint-staged": "lint-staged", | ||
@@ -42,17 +42,16 @@ "prepublish": "npm run build", | ||
"devDependencies": { | ||
"babel-cli": "^6.26.0", | ||
"babel-plugin-transform-object-rest-spread": "^6.26.0", | ||
"babel-polyfill": "^6.26.0", | ||
"babel-preset-env": "^1.7.0", | ||
"cross-env": "^5.2.1", | ||
"del-cli": "^1.1.0", | ||
"@babel/cli": "^7.7.7", | ||
"@babel/core": "^7.7.7", | ||
"@babel/preset-env": "^7.7.7", | ||
"cross-env": "^6.0.3", | ||
"del-cli": "^3.0.0", | ||
"eslint": "^4.19.1", | ||
"eslint-config-webpack": "^1.2.5", | ||
"eslint-plugin-import": "^2.18.2", | ||
"lint-staged": "^6.1.1", | ||
"eslint-plugin-import": "^2.19.1", | ||
"lint-staged": "^9.5.0", | ||
"pre-commit": "^1.2.2", | ||
"standard-version": "^7.0.0" | ||
"standard-version": "^7.0.1" | ||
}, | ||
"engines": { | ||
"node": ">= 4.3 < 5.0.0 || >= 5.10" | ||
"node": ">= 10.18" | ||
}, | ||
@@ -59,0 +58,0 @@ "email": "kambing860210@gmail.com", |
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
11
8908