scheduling
Advanced tools
Comparing version 1.4.0 to 1.4.1
@@ -1,1 +0,1 @@ | ||
let e=window.requestAnimationFrame,n=60,t=1,r=0,o=0,i=0;const f=new Map,a=[];let u=[],c=[],l=0;function s(t){n=t,e=t=>{requestAnimationFrame((r=>{const o=1e3/n,f=r-i;f>=o?t(r):setTimeout((()=>e(t)),o-f)}))}}r=performance.now(),function l(s){!function(){let e,n=0;for(let[e,n]of f)null!=n&&n.func(n.args);for(;u.length>0;)e=u.pop(),e.func(e.args);for(n=0;n<a.length;n++)e=a[n],r-e.time>e.delay/t&&(e.func(e.args),e.repeat?e.time=r:a.splice(n,1))}(),i=r,void 0===s?r+=1e3/n*t:r=s,o=r-i,u=u.concat(c),c=[],e(l)}(r);var d={addEF:function(e,n){if("function"!=typeof e)throw new Error("Invalid function provided for enterframe task.");const t=++l;return f.set(t,{func:e,args:n}),t},removeEF:function(e){return f.delete(e),-1},delay:function(e,n,t,o=!1){if("function"!=typeof e)throw new Error("Invalid function provided for delayed task.");a.push({func:e,args:t,delay:n,time:r,repeat:o})},next:function(e,n){if("function"!=typeof e)throw new Error("Invalid function provided for next frame task.");c.push({func:e,args:n})},getTime:function(){return r},getDeltaTime:function(){return o},setFrameRate:s,setTimeScale:function(e){t=e,s(n*t)},getTimeScale:function(){return t}};export default d; | ||
let e=window.requestAnimationFrame,n=60,t=1,r=0,o=0,f=0;const i=new Map,u=[],a=[];let c=[],s=[],d=0;function l(t){n=t,e=t=>{requestAnimationFrame((r=>{const o=1e3/n,i=r-f;i>=o?t(r):setTimeout((()=>e(t)),o-i)}))}}r=performance.now(),function d(l){!function(){let e,o=0;for(let[e,n]of i)null!=n&&n.func(n.args);for(;c.length>0;)e=c.pop(),e.func(e.args);for(o=0;o<u.length;o++)e=u[o],r-e.time>e.delay/t&&(e.func(e.args),e.repeat?e.time=r:u.splice(o,1));let f=performance.now();for(;a.length>0;){if(e=a.shift(),!(performance.now()-f<1e3/n*t)){a.unshift(e);break}e.func(e.args)}}(),f=r,void 0===l?r+=1e3/n*t:r=l,o=r-f,c=c.concat(s),s=[],e(d)}(r);var p={addEF:function(e,n){if("function"!=typeof e)throw new Error("Invalid function provided for enterframe task.");const t=++d;return i.set(t,{func:e,args:n}),t},removeEF:function(e){return i.delete(e),-1},delay:function(e,n,t,o=!1){if("function"!=typeof e)throw new Error("Invalid function provided for delayed task.");u.push({func:e,args:t,delay:n,time:r,repeat:o})},next:function(e,n){if("function"!=typeof e)throw new Error("Invalid function provided for next frame task.");s.push({func:e,args:n})},defer:function(e,n){if("function"!=typeof e)throw new Error("Invalid function provided for deferred task.");a.push({func:e,args:n})},getTime:function(){return r},getDeltaTime:function(){return o},setFrameRate:l,setTimeScale:function(e){t=e,l(n*t)},getTimeScale:function(){return t},setEnterframeFunc:function(n){e=n}};export default p; |
// main.js | ||
// import Scheduler from "../src/scheduler"; | ||
import Scheduler from "../build/scheduler"; | ||
import Scheduler from "../src/scheduler"; | ||
// import Scheduler from "../build/scheduler"; | ||
@@ -5,0 +5,0 @@ const maxFrame = 60; |
{ | ||
"name": "scheduling", | ||
"version": "1.4.0", | ||
"version": "1.4.1", | ||
"description": "A enterframe tool", | ||
@@ -5,0 +5,0 @@ "main": "build/scheduler.js", |
@@ -11,2 +11,3 @@ let enterframeFunc = window.requestAnimationFrame; | ||
const delayTasks = []; | ||
const deferTasks = []; | ||
let highTasks = []; | ||
@@ -86,2 +87,17 @@ let nextTasks = []; | ||
/** | ||
* Add a deffered task that only execute when there's enough time left in the frame. | ||
* Otherwise try again in the next frame ( Green threading ) | ||
* | ||
* @param {function} mFunc the function to be called | ||
* @param {object} mArgs the arguments for the function | ||
* @throws {Error} Throws an error if the provided mFunc is not a function. | ||
*/ | ||
function defer(mFunc, mArgs) { | ||
if (typeof mFunc !== "function") { | ||
throw new Error("Invalid function provided for deferred task."); | ||
} | ||
deferTasks.push({ func: mFunc, args: mArgs }); | ||
} | ||
/** | ||
* Sets the frame rate and defines the enterframe function. | ||
@@ -170,2 +186,14 @@ * | ||
} | ||
// defer tasks | ||
let time = performance.now(); | ||
while (deferTasks.length > 0) { | ||
task = deferTasks.shift(); | ||
if (performance.now() - time < (1000 / frameRate) * timeScale) { | ||
task.func(task.args); | ||
} else { | ||
deferTasks.unshift(task); | ||
break; | ||
} | ||
} | ||
} | ||
@@ -223,2 +251,3 @@ | ||
next, | ||
defer, | ||
getTime, | ||
@@ -229,2 +258,3 @@ getDeltaTime, | ||
getTimeScale, | ||
setEnterframeFunc, | ||
}; |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
18615
588
0