diff --git a/dist/tempus.js b/dist/tempus.js index 02ced49..2684e74 100644 --- a/dist/tempus.js +++ b/dist/tempus.js @@ -1,2 +1,2 @@ -var t=0,i=window.requestAnimationFrame,e=window.cancelAnimationFrame,r=/*#__PURE__*/function(){function i(t){void 0===t&&(t=Infinity),this.callbacks=[],this.fps=t,this.time=0,this.lastTickDate=performance.now()}var e,r,n=i.prototype;return n.dispatch=function(t,i){for(var e=0;e=this.executionTime){this.time=this.time%this.executionTime;var e=t-this.lastTickDate;this.lastTickDate=t,this.dispatch(t,e)}},n.add=function(i){var e=this,r=i.callback,n=i.priority;"function"!=typeof r&&console.error("Tempus.add: callback is not a function");var a=t++;return this.callbacks.push({callback:r,priority:n,uid:a}),this.callbacks.sort(function(t,i){return t.priority-i.priority}),function(){return e.remove(a)}},n.remove=function(t){this.callbacks=this.callbacks.filter(function(i){return t!==i.uid})},e=i,(r=[{key:"executionTime",get:function(){return 1e3/this.fps}}])&&function(t,i){for(var e=0;e=this.executionTime){this.time=this.time%this.executionTime;var e=t-this.lastTickDate;this.lastTickDate=t,this.dispatch(t,e)}},n.add=function(i){var e=this,r=i.callback,n=i.priority;"function"!=typeof r&&console.error("Tempus.add: callback is not a function");var a=t++;return this.callbacks.push({callback:r,priority:n,uid:a}),this.callbacks.sort(function(t,i){return t.priority-i.priority}),function(){return e.remove(a)}},n.remove=function(t){this.callbacks=this.callbacks.filter(function(i){return t!==i.uid})},e=i,(r=[{key:"executionTime",get:function(){return 1e3/this.fps}}])&&function(t,i){for(var e=0;e= this.executionTime) {\r\n this.time = this.time % this.executionTime\r\n const deltaTime = time - this.lastTickDate\r\n this.lastTickDate = time\r\n\r\n this.dispatch(time, deltaTime)\r\n }\r\n }\r\n\r\n add({ callback, priority }) {\r\n if (typeof callback !== 'function') console.error('Tempus.add: callback is not a function')\r\n\r\n const uid = getUID()\r\n this.callbacks.push({ callback, priority, uid })\r\n this.callbacks.sort((a, b) => a.priority - b.priority)\r\n\r\n return () => this.remove(uid)\r\n }\r\n\r\n remove(uid) {\r\n this.callbacks = this.callbacks.filter(({ uid: u }) => uid !== u)\r\n }\r\n}\r\n\r\nclass Tempus {\r\n constructor() {\r\n /**\r\n * @private\r\n */\r\n this.framerates = {}\r\n\r\n /**\r\n * @private\r\n */\r\n this.time = performance.now()\r\n requestAnimationFrame(this.raf)\r\n }\r\n\r\n /**\r\n * @param {Function} callback\r\n * @param {{ priority?: number, fps?: number }} [options]\r\n * @returns {Function}\r\n */\r\n add(callback, { priority = 0, fps = Infinity } = {}) {\r\n if (typeof fps === 'number') {\r\n if (!this.framerates[fps]) this.framerates[fps] = new Framerate(fps)\r\n\r\n return this.framerates[fps].add({ callback, priority })\r\n }\r\n }\r\n\r\n /**\r\n * @private\r\n */\r\n raf = (time) => {\r\n requestAnimationFrame(this.raf, true)\r\n\r\n const deltaTime = time - this.time\r\n this.time = time\r\n\r\n for (const framerate of Object.values(this.framerates)) {\r\n framerate.raf(time, deltaTime)\r\n }\r\n }\r\n\r\n patch() {\r\n window.requestAnimationFrame = (callback, { priority = 0, fps = Infinity } = {}) => {\r\n if (callback === this.raf || !callback.toString().includes('requestAnimationFrame(')) {\r\n return originalRAF(callback)\r\n }\r\n\r\n if (!callback.__tempusPatched) {\r\n callback.__tempusPatched = true\r\n callback.__tempusUnsubscribe = this.add(callback, { priority, fps })\r\n }\r\n\r\n return callback.__tempusUnsubscribe\r\n }\r\n\r\n window.cancelAnimationFrame = (callback) => {\r\n if (typeof callback === 'function') callback?.()\r\n\r\n return originalCancelRAF(callback)\r\n }\r\n }\r\n\r\n unpatch() {\r\n window.requestAnimationFrame = originalRAF\r\n window.cancelAnimationFrame = originalCancelRAF\r\n }\r\n}\r\n\r\nconst isClient = typeof window !== 'undefined'\r\n\r\nexport default isClient && new Tempus()\r\n"],"names":["index","originalRAF","window","requestAnimationFrame","originalCancelRAF","cancelAnimationFrame","Framerate","fps","Infinity","this","callbacks","time","lastTickDate","performance","now","_proto","prototype","dispatch","deltaTime","i","length","callback","raf","executionTime","add","_ref","_this","priority","console","error","uid","push","sort","a","b","remove","filter","_ref2","key","get","Tempus","_this2","_i","_Object$values","Object","values","framerates","_proto2","_temp","_ref3","_ref3$priority","_ref3$fps","patch","_this3","_temp2","_ref4","_ref4$priority","_ref4$fps","toString","includes","__tempusPatched","__tempusUnsubscribe","unpatch","isClient"],"mappings":"AAAA,IAAIA,EAAQ,ECINC,EAAcC,OAAOC,sBACrBC,EAAoBF,OAAOG,qBAE3BC,eACJ,WAAA,SAAAA,EAAYC,QAAG,IAAHA,IAAAA,EAAMC,UAChBC,KAAKC,UAAY,GACjBD,KAAKF,IAAMA,EACXE,KAAKE,KAAO,EACZF,KAAKG,aAAeC,YAAYC,KAClC,CAAC,QAAAC,EAAAT,EAAAU,UAIA,OAJAD,EAMDE,SAAA,SAASN,EAAMO,GACb,IAAK,IAAIC,EAAI,EAAGA,EAAIV,KAAKC,UAAUU,OAAQD,IACzCV,KAAKC,UAAUS,GAAGE,SAASV,EAAMO,EAErC,EAACH,EAEDO,IAAA,SAAIX,EAAMO,GAGR,GAFAT,KAAKE,MAAQO,EAEIV,WAAbC,KAAKF,IACPE,KAAKQ,SAASN,EAAMO,QACf,GAAIT,KAAKE,MAAQF,KAAKc,cAAe,CAC1Cd,KAAKE,KAAOF,KAAKE,KAAOF,KAAKc,cAC7B,IAAML,EAAYP,EAAOF,KAAKG,aAC9BH,KAAKG,aAAeD,EAEpBF,KAAKQ,SAASN,EAAMO,EACtB,CACF,EAACH,EAEDS,IAAA,SAAAC,OAA4BC,EAAAjB,KAAtBY,EAAQI,EAARJ,SAAUM,EAAQF,EAARE,SACU,mBAAbN,GAAyBO,QAAQC,MAAM,0CAElD,IAAMC,EDvCD9B,IC2CL,OAHAS,KAAKC,UAAUqB,KAAK,CAAEV,SAAAA,EAAUM,SAAAA,EAAUG,IAAAA,IAC1CrB,KAAKC,UAAUsB,KAAK,SAACC,EAAGC,GAAC,OAAKD,EAAEN,SAAWO,EAAEP,QAAQ,GAE9C,WA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AF,EAAEhD,IAAAA,OAAG,IAAAkD,EAAGjD,SAAQiD,EACtE,OAAIpC,IAAagC,EAAK/B,KAAQD,EAASqC,WAAWC,SAAS,2BAItDtC,EAASuC,kBACZvC,EAASuC,iBAAkB,EAC3BvC,EAASwC,oBAAsBR,EAAK7B,IAAIH,EAAU,CAAEM,SAAAA,EAAUpB,IAAAA,KAGzDc,EAASwC,qBARP5D,EAAYoB,EASvB,EAEAnB,OAAOG,qBAAuB,SAACgB,GAG7B,MAFwB,mBAAbA,UAAyBA,GAAAA,KAE7BjB,EAAkBiB,EAC3B,CACF,EAAC0B,EAEDe,QAAA,WACE5D,OAAOC,sBAAwBF,EAC/BC,OAAOG,qBAAuBD,CAChC,EAACoC,CAAA,CAhED,GAqEauB,EAFoB,oBAAX7D,QAEG,IAAIsC"} \ No newline at end of file +{"version":3,"file":"tempus.js","sources":["../src/uid.js","../src/index.js"],"sourcesContent":["let index = 0\r\n\r\nexport function getUID() {\r\n return index++\r\n}\r\n","// Infinity = max FPS (system default)\r\n\r\nimport { getUID } from './uid'\r\n\r\nconst originalRAF = window.requestAnimationFrame\r\nconst originalCancelRAF = window.cancelAnimationFrame\r\n\r\nclass Framerate {\r\n constructor(fps = Infinity) {\r\n this.callbacks = []\r\n this.fps = fps\r\n this.time = 0\r\n this.lastTickDate = performance.now()\r\n }\r\n\r\n get executionTime() {\r\n return 1000 / this.fps\r\n }\r\n\r\n dispatch(time, deltaTime) {\r\n for (let i = 0; i < this.callbacks.length; i++) {\r\n this.callbacks[i].callback(time, deltaTime)\r\n }\r\n }\r\n\r\n raf(time, deltaTime) {\r\n this.time += deltaTime\r\n\r\n if (this.fps === Infinity) {\r\n this.dispatch(time, deltaTime)\r\n } else if (this.time >= this.executionTime) {\r\n this.time = this.time % this.executionTime\r\n const deltaTime = time - this.lastTickDate\r\n this.lastTickDate = time\r\n\r\n this.dispatch(time, deltaTime)\r\n }\r\n }\r\n\r\n add({ callback, priority }) {\r\n if (typeof callback !== 'function') console.error('Tempus.add: callback is not a function')\r\n\r\n const uid = getUID()\r\n this.callbacks.push({ callback, priority, uid })\r\n this.callbacks.sort((a, b) => a.priority - b.priority)\r\n\r\n return () => this.remove(uid)\r\n }\r\n\r\n remove(uid) {\r\n this.callbacks = this.callbacks.filter(({ uid: u }) => uid !== u)\r\n }\r\n}\r\n\r\nclass Tempus {\r\n constructor() {\r\n /**\r\n * @private\r\n */\r\n this.framerates = {}\r\n\r\n /**\r\n * @private\r\n */\r\n this.time = performance.now()\r\n requestAnimationFrame(this.raf)\r\n }\r\n\r\n /**\r\n * @param {Function} callback\r\n * @param {{ priority?: number, fps?: number }} [options]\r\n * @returns {Function}\r\n */\r\n add(callback, { priority = 0, fps = Infinity } = {}) {\r\n if (typeof fps === 'number') {\r\n if (!this.framerates[fps]) this.framerates[fps] = new Framerate(fps)\r\n\r\n return this.framerates[fps].add({ callback, priority })\r\n }\r\n }\r\n\r\n /**\r\n * @private\r\n */\r\n raf = (time) => {\r\n requestAnimationFrame(this.raf, true)\r\n\r\n const deltaTime = time - this.time\r\n this.time = time\r\n\r\n for (const framerate of Object.values(this.framerates)) {\r\n framerate.raf(time, deltaTime)\r\n }\r\n }\r\n\r\n patch() {\r\n window.requestAnimationFrame = (callback, { priority = 0, fps = Infinity } = {}) => {\r\n if (callback === this.raf || !callback.toString().includes('requestAnimationFrame(')) {\r\n return originalRAF(callback)\r\n }\r\n\r\n if (!callback.__tempusPatched) {\r\n callback.__tempusPatched = true\r\n callback.__tempusUnsubscribe = this.add(callback, { priority, fps })\r\n }\r\n\r\n return callback.__tempusUnsubscribe\r\n }\r\n\r\n window.cancelAnimationFrame = (callback) => {\r\n if (typeof callback === 'function') {\r\n callback?.()\r\n return\r\n }\r\n\r\n return originalCancelRAF(callback)\r\n }\r\n }\r\n\r\n unpatch() {\r\n window.requestAnimationFrame = originalRAF\r\n window.cancelAnimationFrame = originalCancelRAF\r\n }\r\n}\r\n\r\nconst isClient = typeof window !== 'undefined'\r\n\r\nexport default isClient && new Tempus()\r\n"],"names":["index","originalRAF","window","requestAnimationFrame","originalCancelRAF","cancelAnimationFrame","Framerate","fps","Infinity","this","callbacks","time","lastTickDate","performance","now","_proto","prototype","dispatch","deltaTime","i","length","callback","raf","executionTime","add","_ref","_this","priority","console","error","uid","push","sort","a","b","remove","filter","_ref2","key","get","Tempus","_this2","_i","_Object$values","Object","values","framerates","_proto2","_temp","_ref3","_ref3$priority","_ref3$fps","patch","_this3","_temp2","_ref4","_ref4$priority","_ref4$fps","toString","includes","__tempusPatched","__tempusUnsubscribe","unpatch","isClient"],"mappings":"AAAA,IAAIA,EAAQ,ECINC,EAAcC,OAAOC,sBACrBC,EAAoBF,OAAOG,qBAE3BC,0BACJ,SAAAA,EAAYC,QAAAA,IAAAA,IAAAA,EAAMC,UAChBC,KAAKC,UAAY,GACjBD,KAAKF,IAAMA,EACXE,KAAKE,KAAO,EACZF,KAAKG,aAAeC,YAAYC,KAClC,CAAC,QAAAC,EAAAT,EAAAU,iBAAAD,EAMDE,SAAA,SAASN,EAAMO,GACb,IAAK,IAAIC,EAAI,EAAGA,EAAIV,KAAKC,UAAUU,OAAQD,IACzCV,KAAKC,UAAUS,GAAGE,SAASV,EAAMO,EAErC,EAACH,EAEDO,IAAA,SAAIX,EAAMO,GAGR,GAFAT,KAAKE,MAAQO,EAEIV,WAAbC,KAAKF,IACPE,KAAKQ,SAASN,EAAMO,QACX,GAAAT,KAAKE,MAAQF,KAAKc,cAAe,CAC1Cd,KAAKE,KAAOF,KAAKE,KAAOF,KAAKc,cAC7B,IAAML,EAAYP,EAAOF,KAAKG,aAC9BH,KAAKG,aAAeD,EAEpBF,KAAKQ,SAASN,EAAMO,EACtB,CACF,EAACH,EAEDS,IAAA,SAAAC,GAA4BC,IAAAA,OAAtBL,EAAQI,EAARJ,SAAUM,EAAQF,EAARE,SACU,mBAAbN,GAAyBO,QAAQC,MAAM,0CAElD,IAAMC,EDvCD9B,IC2CL,OAHAS,KAAKC,UAAUqB,KAAK,CAA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C,IAAAC,WAAAD,EAAP,CAAE,EAAAA,EAAAE,EAAAD,EAAnC5B,SAAAA,WAAQ6B,EAAG,EAACA,EAAAC,EAAAF,EAAEhD,IAAAA,OAAG,IAAAkD,EAAGjD,SAAQiD,EACtE,OAAIpC,IAAagC,EAAK/B,KAAQD,EAASqC,WAAWC,SAAS,2BAItDtC,EAASuC,kBACZvC,EAASuC,iBAAkB,EAC3BvC,EAASwC,oBAAsBR,EAAK7B,IAAIH,EAAU,CAAEM,SAAAA,EAAUpB,IAAAA,KAGzDc,EAASwC,qBARP5D,EAAYoB,EASvB,EAEAnB,OAAOG,qBAAuB,SAACgB,GAC7B,GAAwB,mBAAbA,EAKX,OAAOjB,EAAkBiB,GAJvBA,MAAAA,GAAAA,GAKJ,CACF,EAAC0B,EAEDe,QAAA,WACE5D,OAAOC,sBAAwBF,EAC/BC,OAAOG,qBAAuBD,CAChC,EAACoC,CAAA,CApES,GAyEGuB,EAFoB,oBAAX7D,QAEG,IAAIsC"} \ No newline at end of file diff --git a/dist/tempus.mjs b/dist/tempus.mjs index 085c0b1..e5609a7 100644 --- a/dist/tempus.mjs +++ b/dist/tempus.mjs @@ -1,2 +1,2 @@ -var t=0,i=window.requestAnimationFrame,e=window.cancelAnimationFrame,r=/*#__PURE__*/function(){function i(t){void 0===t&&(t=Infinity),this.callbacks=[],this.fps=t,this.time=0,this.lastTickDate=performance.now()}var e,r,n=i.prototype;return n.dispatch=function(t,i){for(var e=0;e=this.executionTime){this.time=this.time%this.executionTime;var e=t-this.lastTickDate;this.lastTickDate=t,this.dispatch(t,e)}},n.add=function(i){var e=this,r=i.callback,n=i.priority;"function"!=typeof r&&console.error("Tempus.add: callback is not a function");var a=t++;return this.callbacks.push({callback:r,priority:n,uid:a}),this.callbacks.sort(function(t,i){return t.priority-i.priority}),function(){return e.remove(a)}},n.remove=function(t){this.callbacks=this.callbacks.filter(function(i){return t!==i.uid})},e=i,(r=[{key:"executionTime",get:function(){return 1e3/this.fps}}])&&function(t,i){for(var e=0;e=this.executionTime){this.time=this.time%this.executionTime;var e=t-this.lastTickDate;this.lastTickDate=t,this.dispatch(t,e)}},n.add=function(i){var e=this,r=i.callback,n=i.priority;"function"!=typeof r&&console.error("Tempus.add: callback is not a function");var a=t++;return this.callbacks.push({callback:r,priority:n,uid:a}),this.callbacks.sort(function(t,i){return t.priority-i.priority}),function(){return e.remove(a)}},n.remove=function(t){this.callbacks=this.callbacks.filter(function(i){return t!==i.uid})},e=i,(r=[{key:"executionTime",get:function(){return 1e3/this.fps}}])&&function(t,i){for(var e=0;e= this.executionTime) {\r\n this.time = this.time % this.executionTime\r\n const deltaTime = time - this.lastTickDate\r\n this.lastTickDate = time\r\n\r\n this.dispatch(time, deltaTime)\r\n }\r\n }\r\n\r\n add({ callback, priority }) {\r\n if (typeof callback !== 'function') console.error('Tempus.add: callback is not a function')\r\n\r\n const uid = getUID()\r\n this.callbacks.push({ callback, priority, uid })\r\n this.callbacks.sort((a, b) => a.priority - b.priority)\r\n\r\n return () => this.remove(uid)\r\n }\r\n\r\n remove(uid) {\r\n this.callbacks = this.callbacks.filter(({ uid: u }) => uid !== u)\r\n }\r\n}\r\n\r\nclass Tempus {\r\n constructor() {\r\n /**\r\n * @private\r\n */\r\n this.framerates = {}\r\n\r\n /**\r\n * @private\r\n */\r\n this.time = performance.now()\r\n requestAnimationFrame(this.raf)\r\n }\r\n\r\n /**\r\n * @param {Function} callback\r\n * @param {{ priority?: number, fps?: number }} [options]\r\n * @returns {Function}\r\n */\r\n add(callback, { priority = 0, fps = Infinity } = {}) {\r\n if (typeof fps === 'number') {\r\n if (!this.framerates[fps]) this.framerates[fps] = new Framerate(fps)\r\n\r\n return this.framerates[fps].add({ callback, priority })\r\n }\r\n }\r\n\r\n /**\r\n * @private\r\n */\r\n raf = (time) => {\r\n requestAnimationFrame(this.raf, true)\r\n\r\n const deltaTime = time - this.time\r\n this.time = time\r\n\r\n for (const framerate of Object.values(this.framerates)) {\r\n framerate.raf(time, deltaTime)\r\n }\r\n }\r\n\r\n patch() {\r\n window.requestAnimationFrame = (callback, { priority = 0, fps = Infinity } = {}) => {\r\n if (callback === this.raf || !callback.toString().includes('requestAnimationFrame(')) {\r\n return originalRAF(callback)\r\n }\r\n\r\n if (!callback.__tempusPatched) {\r\n callback.__tempusPatched = true\r\n callback.__tempusUnsubscribe = this.add(callback, { priority, fps })\r\n }\r\n\r\n return callback.__tempusUnsubscribe\r\n }\r\n\r\n window.cancelAnimationFrame = (callback) => {\r\n if (typeof callback === 'function') callback?.()\r\n\r\n return originalCancelRAF(callback)\r\n }\r\n }\r\n\r\n unpatch() {\r\n window.requestAnimationFrame = originalRAF\r\n window.cancelAnimationFrame = originalCancelRAF\r\n }\r\n}\r\n\r\nconst isClient = typeof window !== 'undefined'\r\n\r\nexport default isClient && new Tempus()\r\n"],"names":["index","originalRAF","window","requestAnimationFrame","originalCancelRAF","cancelAnimationFrame","Framerate","fps","Infinity","this","callbacks","time","lastTickDate","performance","now","_proto","prototype","dispatch","deltaTime","i","length","callback","raf","executionTime","add","_ref","_this","priority","console","error","uid","push","sort","a","b","remove","filter","_ref2","key","get","Tempus","_this2","_i","_Object$values","Object","values","framerates","_proto2","_temp","_ref3","_ref3$priority","_ref3$fps","patch","_this3","_temp2","_ref4","_ref4$priority","_ref4$fps","toString","includes","__tempusPatched","__tempusUnsubscribe","unpatch","isClient"],"mappings":"AAAA,IAAIA,EAAQ,ECINC,EAAcC,OAAOC,sBACrBC,EAAoBF,OAAOG,qBAE3BC,eACJ,WAAA,SAAAA,EAAYC,QAAG,IAAHA,IAAAA,EAAMC,UAChBC,KAAKC,UAAY,GACjBD,KAAKF,IAAMA,EACXE,KAAKE,KAAO,EACZF,KAAKG,aAAeC,YAAYC,KAClC,CAAC,QAAAC,EAAAT,EAAAU,UAIA,OAJAD,EAMDE,SAAA,SAASN,EAAMO,GACb,IAAK,IAAIC,EAAI,EAAGA,EAAIV,KAAKC,UAAUU,OAAQD,IACzCV,KAAKC,UAAUS,GAA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AKpBrC,KAAKE,KAAOE,YAAYC,MACxBX,sBAAsBM,KAAKa,IAC7B,CAAC,IAAAyB,EAAAP,EAAAxB,iBAAA+B,EAODvB,IAAA,SAAIH,EAAQ2B,GAAyC,IAAAC,WAAAD,EAAJ,CAAE,EAAAA,EAAAE,EAAAD,EAAnCtB,SAAAA,WAAQuB,EAAG,EAACA,EAAAC,EAAAF,EAAE1C,IAAAA,OAAG,IAAA4C,EAAG3C,SAAQ2C,EAC1C,GAAmB,iBAAR5C,EAGT,OAFKE,KAAKqC,WAAWvC,KAAME,KAAKqC,WAAWvC,GAAO,IAAID,EAAUC,SAEpDuC,WAAWvC,GAAKiB,IAAI,CAAEH,SAAAA,EAAUM,SAAAA,GAEhD,EAACoB,EAgBDK,MAAA,WAAQ,IAAAC,EACNnD,KAAAA,OAAOC,sBAAwB,SAACkB,EAAQiC,GAA4C,IAAAC,WAAAD,EAAP,CAAE,EAAAA,EAAAE,EAAAD,EAAnC5B,SAAAA,WAAQ6B,EAAG,EAACA,EAAAC,EAAAF,EAAEhD,IAAAA,OAAG,IAAAkD,EAAGjD,SAAQiD,EACtE,OAAIpC,IAAagC,EAAK/B,KAAQD,EAASqC,WAAWC,SAAS,2BAItDtC,EAASuC,kBACZvC,EAASuC,iBAAkB,EAC3BvC,EAASwC,oBAAsBR,EAAK7B,IAAIH,EAAU,CAAEM,SAAAA,EAAUpB,IAAAA,KAGzDc,EAASwC,qBARP5D,EAAYoB,EASvB,EAEAnB,OAAOG,qBAAuB,SAACgB,GAG7B,MAFwB,mBAAbA,UAAyBA,GAAAA,KAE7BjB,EAAkBiB,EAC3B,CACF,EAAC0B,EAEDe,QAAA,WACE5D,OAAOC,sBAAwBF,EAC/BC,OAAOG,qBAAuBD,CAChC,EAACoC,CAAA,CAhED,GAqEauB,EAFoB,oBAAX7D,QAEG,IAAIsC"} \ No newline at end of file +{"version":3,"file":"tempus.mjs","sources":["../src/uid.js","../src/index.js"],"sourcesContent":["let index = 0\r\n\r\nexport function getUID() {\r\n return index++\r\n}\r\n","// Infinity = max FPS (system default)\r\n\r\nimport { getUID } from './uid'\r\n\r\nconst originalRAF = window.requestAnimationFrame\r\nconst originalCancelRAF = window.cancelAnimationFrame\r\n\r\nclass Framerate {\r\n constructor(fps = Infinity) {\r\n this.callbacks = []\r\n this.fps = fps\r\n this.time = 0\r\n this.lastTickDate = performance.now()\r\n }\r\n\r\n get executionTime() {\r\n return 1000 / this.fps\r\n }\r\n\r\n dispatch(time, deltaTime) {\r\n for (let i = 0; i < this.callbacks.length; i++) {\r\n this.callbacks[i].callback(time, deltaTime)\r\n }\r\n }\r\n\r\n raf(time, deltaTime) {\r\n this.time += deltaTime\r\n\r\n if (this.fps === Infinity) {\r\n this.dispatch(time, deltaTime)\r\n } else if (this.time >= this.executionTime) {\r\n this.time = this.time % this.executionTime\r\n const deltaTime = time - this.lastTickDate\r\n this.lastTickDate = time\r\n\r\n this.dispatch(time, deltaTime)\r\n }\r\n }\r\n\r\n add({ callback, priority }) {\r\n if (typeof callback !== 'function') console.error('Tempus.add: callback is not a function')\r\n\r\n const uid = getUID()\r\n this.callbacks.push({ callback, priority, uid })\r\n this.callbacks.sort((a, b) => a.priority - b.priority)\r\n\r\n return () => this.remove(uid)\r\n }\r\n\r\n remove(uid) {\r\n this.callbacks = this.callbacks.filter(({ uid: u }) => uid !== u)\r\n }\r\n}\r\n\r\nclass Tempus {\r\n constructor() {\r\n /**\r\n * @private\r\n */\r\n this.framerates = {}\r\n\r\n /**\r\n * @private\r\n */\r\n this.time = performance.now()\r\n requestAnimationFrame(this.raf)\r\n }\r\n\r\n /**\r\n * @param {Function} callback\r\n * @param {{ priority?: number, fps?: number }} [options]\r\n * @returns {Function}\r\n */\r\n add(callback, { priority = 0, fps = Infinity } = {}) {\r\n if (typeof fps === 'number') {\r\n if (!this.framerates[fps]) this.framerates[fps] = new Framerate(fps)\r\n\r\n return this.framerates[fps].add({ callback, priority })\r\n }\r\n }\r\n\r\n /**\r\n * @private\r\n */\r\n raf = (time) => {\r\n requestAnimationFrame(this.raf, true)\r\n\r\n const deltaTime = time - this.time\r\n this.time = time\r\n\r\n for (const framerate of Object.values(this.framerates)) {\r\n framerate.raf(time, deltaTime)\r\n }\r\n }\r\n\r\n patch() {\r\n window.requestAnimationFrame = (callback, { priority = 0, fps = Infinity } = {}) => {\r\n if (callback === this.raf || !callback.toString().includes('requestAnimationFrame(')) {\r\n return originalRAF(callback)\r\n }\r\n\r\n if (!callback.__tempusPatched) {\r\n callback.__tempusPatched = true\r\n callback.__tempusUnsubscribe = this.add(callback, { priority, fps })\r\n }\r\n\r\n return callback.__tempusUnsubscribe\r\n }\r\n\r\n window.cancelAnimationFrame = (callback) => {\r\n if (typeof callback === 'function') {\r\n callback?.()\r\n return\r\n }\r\n\r\n return originalCancelRAF(callback)\r\n }\r\n }\r\n\r\n unpatch() {\r\n window.requestAnimationFrame = originalRAF\r\n window.cancelAnimationFrame = originalCancelRAF\r\n }\r\n}\r\n\r\nconst isClient = typeof window !== 'undefined'\r\n\r\nexport default isClient && new Tempus()\r\n"],"names":["index","originalRAF","window","requestAnimationFrame","originalCancelRAF","cancelAnimationFrame","Framerate","fps","Infinity","this","callbacks","time","lastTickDate","performance","now","_proto","prototype","dispatch","deltaTime","i","length","callback","raf","executionTime","add","_ref","_this","priority","console","error","uid","push","sort","a","b","remove","filter","_ref2","key","get","Tempus","_this2","_i","_Object$values","Object","values","framerates","_proto2","_temp","_ref3","_ref3$priority","_ref3$fps","patch","_this3","_temp2","_ref4","_ref4$priority","_ref4$fps","toString","includes","__tempusPatched","__tempusUnsubscribe","unpatch","isClient"],"mappings":"AAAA,IAAIA,EAAQ,ECINC,EAAcC,OAAOC,sBACrBC,EAAoBF,OAAOG,qBAE3BC,0BACJ,SAAAA,EAAYC,QAAAA,IAAAA,IAAAA,EAAMC,UAChBC,KAAKC,UAAY,GACjBD,KAAKF,IAAMA,EACXE,KAAKE,KAAO,EACZF,KAAKG,aAAeC,YAAYC,KAClC,CAAC,QA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oBAAX7D,QAEG,IAAIsC"} \ No newline at end of file diff --git a/dist/tempus.modern.mjs b/dist/tempus.modern.mjs index 28a4047..5ce911b 100644 --- a/dist/tempus.modern.mjs +++ b/dist/tempus.modern.mjs @@ -1,2 +1,2 @@ -let t=0;const i=window.requestAnimationFrame,e=window.cancelAnimationFrame;class s{constructor(t=Infinity){this.callbacks=[],this.fps=t,this.time=0,this.lastTickDate=performance.now()}get executionTime(){return 1e3/this.fps}dispatch(t,i){for(let e=0;e=this.executionTime){this.time=this.time%this.executionTime;const i=t-this.lastTickDate;this.lastTickDate=t,this.dispatch(t,i)}}add({callback:i,priority:e}){"function"!=typeof i&&console.error("Tempus.add: callback is not a function");const s=t++;return this.callbacks.push({callback:i,priority:e,uid:s}),this.callbacks.sort((t,i)=>t.priority-i.priority),()=>this.remove(s)}remove(t){this.callbacks=this.callbacks.filter(({uid:i})=>t!==i)}}var a="undefined"!=typeof window&&new class{constructor(){this.raf=t=>{requestAnimationFrame(this.raf,!0);const i=t-this.time;this.time=t;for(const e of Object.values(this.framerates))e.raf(t,i)},this.framerates={},this.time=performance.now(),requestAnimationFrame(this.raf)}add(t,{priority:i=0,fps:e=Infinity}={}){if("number"==typeof e)return this.framerates[e]||(this.framerates[e]=new s(e)),this.framerates[e].add({callback:t,priority:i})}patch(){window.requestAnimationFrame=(t,{priority:e=0,fps:s=Infinity}={})=>t!==this.raf&&t.toString().includes("requestAnimationFrame(")?(t.__tempusPatched||(t.__tempusPatched=!0,t.__tempusUnsubscribe=this.add(t,{priority:e,fps:s})),t.__tempusUnsubscribe):i(t),window.cancelAnimationFrame=t=>("function"==typeof t&&(null==t||t()),e(t))}unpatch(){window.requestAnimationFrame=i,window.cancelAnimationFrame=e}};export{a as default}; +let t=0;const i=window.requestAnimationFrame,e=window.cancelAnimationFrame;class s{constructor(t=Infinity){this.callbacks=[],this.fps=t,this.time=0,this.lastTickDate=performance.now()}get executionTime(){return 1e3/this.fps}dispatch(t,i){for(let e=0;e=this.executionTime){this.time=this.time%this.executionTime;const i=t-this.lastTickDate;this.lastTickDate=t,this.dispatch(t,i)}}add({callback:i,priority:e}){"function"!=typeof i&&console.error("Tempus.add: callback is not a function");const s=t++;return this.callbacks.push({callback:i,priority:e,uid:s}),this.callbacks.sort((t,i)=>t.priority-i.priority),()=>this.remove(s)}remove(t){this.callbacks=this.callbacks.filter(({uid:i})=>t!==i)}}var a="undefined"!=typeof window&&new class{constructor(){this.raf=t=>{requestAnimationFrame(this.raf,!0);const i=t-this.time;this.time=t;for(const e of Object.values(this.framerates))e.raf(t,i)},this.framerates={},this.time=performance.now(),requestAnimationFrame(this.raf)}add(t,{priority:i=0,fps:e=Infinity}={}){if("number"==typeof e)return this.framerates[e]||(this.framerates[e]=new s(e)),this.framerates[e].add({callback:t,priority:i})}patch(){window.requestAnimationFrame=(t,{priority:e=0,fps:s=Infinity}={})=>t!==this.raf&&t.toString().includes("requestAnimationFrame(")?(t.__tempusPatched||(t.__tempusPatched=!0,t.__tempusUnsubscribe=this.add(t,{priority:e,fps:s})),t.__tempusUnsubscribe):i(t),window.cancelAnimationFrame=t=>{if("function"!=typeof t)return e(t);null==t||t()}}unpatch(){window.requestAnimationFrame=i,window.cancelAnimationFrame=e}};export{a as default}; //# sourceMappingURL=tempus.modern.mjs.map diff --git a/dist/tempus.modern.mjs.map b/dist/tempus.modern.mjs.map index f37f73d..97fb132 100644 --- a/dist/tempus.modern.mjs.map +++ b/dist/tempus.modern.mjs.map @@ -1 +1 @@ -{"version":3,"file":"tempus.modern.mjs","sources":["../src/uid.js","../src/index.js"],"sourcesContent":["let index = 0\r\n\r\nexport function getUID() {\r\n return index++\r\n}\r\n","// Infinity = max FPS (system default)\r\n\r\nimport { getUID } from './uid'\r\n\r\nconst originalRAF = window.requestAnimationFrame\r\nconst originalCancelRAF = window.cancelAnimationFrame\r\n\r\nclass Framerate {\r\n constructor(fps = Infinity) {\r\n this.callbacks = []\r\n this.fps = fps\r\n this.time = 0\r\n this.lastTickDate = performance.now()\r\n }\r\n\r\n get executionTime() {\r\n return 1000 / this.fps\r\n }\r\n\r\n dispatch(time, deltaTime) {\r\n for (let i = 0; i < this.callbacks.length; i++) {\r\n this.callbacks[i].callback(time, deltaTime)\r\n }\r\n }\r\n\r\n raf(time, deltaTime) {\r\n this.time += deltaTime\r\n\r\n if (this.fps === Infinity) {\r\n this.dispatch(time, deltaTime)\r\n } else if (this.time >= this.executionTime) {\r\n this.time = this.time % this.executionTime\r\n const deltaTime = time - this.lastTickDate\r\n this.lastTickDate = time\r\n\r\n this.dispatch(time, deltaTime)\r\n }\r\n }\r\n\r\n add({ callback, priority }) {\r\n if (typeof callback !== 'function') console.error('Tempus.add: callback is not a function')\r\n\r\n const uid = getUID()\r\n this.callbacks.push({ callback, priority, uid })\r\n this.callbacks.sort((a, b) => a.priority - b.priority)\r\n\r\n return () => this.remove(uid)\r\n }\r\n\r\n remove(uid) {\r\n this.callbacks = this.callbacks.filter(({ uid: u }) => uid !== u)\r\n }\r\n}\r\n\r\nclass Tempus {\r\n constructor() {\r\n /**\r\n * @private\r\n */\r\n this.framerates = {}\r\n\r\n /**\r\n * @private\r\n */\r\n this.time = performance.now()\r\n requestAnimationFrame(this.raf)\r\n }\r\n\r\n /**\r\n * @param {Function} callback\r\n * @param {{ priority?: number, fps?: number }} [options]\r\n * @returns {Function}\r\n */\r\n add(callback, { priority = 0, fps = Infinity } = {}) {\r\n if (typeof fps === 'number') {\r\n if (!this.framerates[fps]) this.framerates[fps] = new Framerate(fps)\r\n\r\n return this.framerates[fps].add({ callback, priority })\r\n }\r\n }\r\n\r\n /**\r\n * @private\r\n */\r\n raf = (time) => {\r\n requestAnimationFrame(this.raf, true)\r\n\r\n const deltaTime = time - this.time\r\n this.time = time\r\n\r\n for (const framerate of Object.values(this.framerates)) {\r\n framerate.raf(time, deltaTime)\r\n }\r\n }\r\n\r\n patch() {\r\n window.requestAnimationFrame = (callback, { priority = 0, fps = Infinity } = {}) => {\r\n if (callback === this.raf || !callback.toString().includes('requestAnimationFrame(')) {\r\n return originalRAF(callback)\r\n }\r\n\r\n if (!callback.__tempusPatched) {\r\n callback.__tempusPatched = true\r\n callback.__tempusUnsubscribe = this.add(callback, { priority, fps })\r\n }\r\n\r\n return callback.__tempusUnsubscribe\r\n }\r\n\r\n window.cancelAnimationFrame = (callback) => {\r\n if (typeof callback === 'function') callback?.()\r\n\r\n return originalCancelRAF(callback)\r\n }\r\n }\r\n\r\n unpatch() {\r\n window.requestAnimationFrame = originalRAF\r\n window.cancelAnimationFrame = originalCancelRAF\r\n }\r\n}\r\n\r\nconst isClient = typeof window !== 'undefined'\r\n\r\nexport default isClient && new Tempus()\r\n"],"names":["index","originalRAF","window","requestAnimationFrame","originalCancelRAF","cancelAnimationFrame","Framerate","constructor","fps","Infinity","this","callbacks","time","lastTickDate","performance","now","executionTime","dispatch","deltaTime","i","length","callback","raf","add","priority","console","error","uid","push","sort","a","b","remove","filter","u","framerate","Object","values","framerates","patch","toString","includes","__tempusPatched","__tempusUnsubscribe","unpatch"],"mappings":"AAAA,IAAIA,EAAQ,ECIZ,MAAMC,EAAcC,OAAOC,sBACrBC,EAAoBF,OAAOG,qBAEjC,MAAMC,EACJC,YAAYC,EAAMC,UAChBC,KAAKC,UAAY,GACjBD,KAAKF,IAAMA,EACXE,KAAKE,KAAO,EACZF,KAAKG,aAAeC,YAAYC,KAClC,CAEIC,oBACF,OAAO,IAAON,KAAKF,GACrB,CAEAS,SAASL,EAAMM,GACb,IAAK,IAAIC,EAAI,EAAGA,EAAIT,KAAKC,UAAUS,OAAQD,IACzCT,KAAKC,UAAUQ,GAAGE,SAAST,EAAMM,EAErC,CAEAI,IAAIV,EAAMM,GAGR,GAFAR,KAAKE,MAAQM,EAEIT,WAAbC,KAAKF,IACPE,KAAKO,SAASL,EAAMM,QACX,GAAAR,KAAKE,MAAQF,KAAKM,cAAe,CAC1CN,KAAKE,KAAOF,KAAKE,KAAOF,KAAKM,cAC7B,MAAME,EAAYN,EAAOF,KAAKG,aAC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UhB,IAAAA,KAGzDa,EAASsB,qBARP1C,EAAYoB,GAWvBnB,OAAOG,qBAAwBgB,IACL,mBAAbA,IAAiC,MAARA,GAAAA,KAE7BjB,EAAkBiB,GAE7B,CAEAuB,UACE1C,OAAOC,sBAAwBF,EAC/BC,OAAOG,qBAAuBD,CAChC"} \ No newline at end of file +{"version":3,"file":"tempus.modern.mjs","sources":["../src/uid.js","../src/index.js"],"sourcesContent":["let index = 0\r\n\r\nexport function getUID() {\r\n return index++\r\n}\r\n","// Infinity = max FPS (system default)\r\n\r\nimport { getUID } from './uid'\r\n\r\nconst originalRAF = window.requestAnimationFrame\r\nconst originalCancelRAF = window.cancelAnimationFrame\r\n\r\nclass Framerate {\r\n constructor(fps = Infinity) {\r\n this.callbacks = []\r\n this.fps = fps\r\n this.time = 0\r\n this.lastTickDate = performance.now()\r\n }\r\n\r\n get executionTime() {\r\n return 1000 / this.fps\r\n }\r\n\r\n dispatch(time, deltaTime) {\r\n for (let i = 0; i < this.callbacks.length; i++) {\r\n this.callbacks[i].callback(time, deltaTime)\r\n }\r\n }\r\n\r\n raf(time, deltaTime) {\r\n this.time += deltaTime\r\n\r\n if (this.fps === Infinity) {\r\n this.dispatch(time, deltaTime)\r\n } else if (this.time >= this.executionTime) {\r\n this.time = this.time % this.executionTime\r\n const deltaTime = time - this.lastTickDate\r\n this.lastTickDate = time\r\n\r\n this.dispatch(time, deltaTime)\r\n }\r\n }\r\n\r\n add({ callback, priority }) {\r\n if (typeof callback !== 'function') console.error('Tempus.add: callback is not a function')\r\n\r\n const uid = getUID()\r\n this.callbacks.push({ callback, priority, uid })\r\n this.callbacks.sort((a, b) => a.priority - b.priority)\r\n\r\n return () => this.remove(uid)\r\n }\r\n\r\n remove(uid) {\r\n this.callbacks = this.callbacks.filter(({ uid: u }) => uid !== u)\r\n }\r\n}\r\n\r\nclass Tempus {\r\n constructor() {\r\n /**\r\n * @private\r\n */\r\n this.framerates = {}\r\n\r\n /**\r\n * @private\r\n */\r\n this.time = performance.now()\r\n requestAnimationFrame(this.raf)\r\n }\r\n\r\n /**\r\n * @param {Function} callback\r\n * @param {{ priority?: number, fps?: number }} [options]\r\n * @returns {Function}\r\n */\r\n add(callback, { priority = 0, fps = Infinity } = {}) {\r\n if (typeof fps === 'number') {\r\n if (!this.framerates[fps]) this.framerates[fps] = new Framerate(fps)\r\n\r\n return this.framerates[fps].add({ callback, priority })\r\n }\r\n }\r\n\r\n /**\r\n * @private\r\n */\r\n raf = (time) => {\r\n requestAnimationFrame(this.raf, true)\r\n\r\n const deltaTime = time - this.time\r\n this.time = time\r\n\r\n for (const framerate of Object.values(this.framerates)) {\r\n framerate.raf(time, deltaTime)\r\n }\r\n }\r\n\r\n patch() {\r\n window.requestAnimationFrame = (callback, { priority = 0, fps = Infinity } = {}) => {\r\n if (callback === this.raf || !callback.toString().includes('requestAnimationFrame(')) {\r\n return originalRAF(callback)\r\n }\r\n\r\n if (!callback.__tempusPatched) {\r\n callback.__tempusPatched = true\r\n callback.__tempusUnsubscribe = this.add(callback, { priority, fps })\r\n }\r\n\r\n return callback.__tempusUnsubscribe\r\n }\r\n\r\n window.cancelAnimationFrame = (callback) => {\r\n if (typeof callback === 'function') {\r\n callback?.()\r\n return\r\n }\r\n\r\n return originalCancelRAF(callback)\r\n }\r\n }\r\n\r\n unpatch() {\r\n window.requestAnimationFrame = originalRAF\r\n window.cancelAnimationFrame = originalCancelRAF\r\n }\r\n}\r\n\r\nconst isClient = typeof window !== 'undefined'\r\n\r\nexport default isClient && new Tempus()\r\n"],"names":["index","originalRAF","window","requestAnimationFrame","originalCancelRAF","cancelAnimationFrame","Framerate","constructor","fps","Infinity","this","callbacks","time","lastTickDate","performance","now","executionTime","dispatch","deltaTime","i","length","callback","raf","add","priority","console","error","uid","push","sort","a","b","remove","filter","u","framerate","Object","values","framerates","patch","toString","includes","__tempusPatched","__tempusUnsubscribe","unpatch"],"mappings":"AAAA,IAAIA,EAAQ,ECIZ,MAAMC,EAAcC,OAAOC,sBACr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lB5B,KAAKE,KAAOE,YAAYC,MACxBZ,sBAAsBO,KAAKY,IAC7B,CAOAC,IAAIF,GAAUG,SAAEA,EAAW,EAAChB,IAAEA,EAAMC,UAAa,CAAA,GAC/C,GAAmB,iBAARD,EAGT,OAFKE,KAAK4B,WAAW9B,KAAME,KAAK4B,WAAW9B,GAAO,IAAIF,EAAUE,IAErDE,KAAC4B,WAAW9B,GAAKe,IAAI,CAAEF,WAAUG,YAEhD,CAgBAe,QACErC,OAAOC,sBAAwB,CAACkB,GAAYG,SAAAA,EAAW,EAAGhB,IAAAA,EAAMC,UAAa,KACvEY,IAAaX,KAAKY,KAAQD,EAASmB,WAAWC,SAAS,2BAItDpB,EAASqB,kBACZrB,EAASqB,iBAAkB,EAC3BrB,EAASsB,oBAAsBjC,KAAKa,IAAIF,EAAU,CAAEG,SAAAA,EAAUhB,IAAAA,KAGzDa,EAASsB,qBARP1C,EAAYoB,GAWvBnB,OAAOG,qBAAwBgB,IAC7B,GAAwB,mBAAbA,EAKX,OAAOjB,EAAkBiB,GAJvBA,MAAAA,GAAAA,GAI+B,CAErC,CAEAuB,UACE1C,OAAOC,sBAAwBF,EAC/BC,OAAOG,qBAAuBD,CAChC"} \ No newline at end of file diff --git a/dist/tempus.umd.js b/dist/tempus.umd.js index e16c8d6..830a44d 100644 --- a/dist/tempus.umd.js +++ b/dist/tempus.umd.js @@ -1,2 +1,2 @@ -!function(t,i){"object"==typeof exports&&"undefined"!=typeof module?module.exports=i():"function"==typeof define&&define.amd?define(i):(t||self).tempus=i()}(this,function(){var t=0,i=window.requestAnimationFrame,e=window.cancelAnimationFrame,n=/*#__PURE__*/function(){function i(t){void 0===t&&(t=Infinity),this.callbacks=[],this.fps=t,this.time=0,this.lastTickDate=performance.now()}var e,n,r=i.prototype;return r.dispatch=function(t,i){for(var e=0;e=this.executionTime){this.time=this.time%this.executionTime;var e=t-this.lastTickDate;this.lastTickDate=t,this.dispatch(t,e)}},r.add=function(i){var e=this,n=i.callback,r=i.priority;"function"!=typeof n&&console.error("Tempus.add: callback is not a function");var a=t++;return this.callbacks.push({callback:n,priority:r,uid:a}),this.callbacks.sort(function(t,i){return t.priority-i.priority}),function(){return e.remove(a)}},r.remove=function(t){this.callbacks=this.callbacks.filter(function(i){return t!==i.uid})},e=i,(n=[{key:"executionTime",get:function(){return 1e3/this.fps}}])&&function(t,i){for(var e=0;e=this.executionTime){this.time=this.time%this.executionTime;var e=t-this.lastTickDate;this.lastTickDate=t,this.dispatch(t,e)}},r.add=function(i){var e=this,n=i.callback,r=i.priority;"function"!=typeof n&&console.error("Tempus.add: callback is not a function");var a=t++;return this.callbacks.push({callback:n,priority:r,uid:a}),this.callbacks.sort(function(t,i){return t.priority-i.priority}),function(){return e.remove(a)}},r.remove=function(t){this.callbacks=this.callbacks.filter(function(i){return t!==i.uid})},e=i,(n=[{key:"executionTime",get:function(){return 1e3/this.fps}}])&&function(t,i){for(var e=0;e= this.executionTime) {\r\n this.time = this.time % this.executionTime\r\n const deltaTime = time - this.lastTickDate\r\n this.lastTickDate = time\r\n\r\n this.dispatch(time, deltaTime)\r\n }\r\n }\r\n\r\n add({ callback, priority }) {\r\n if (typeof callback !== 'function') console.error('Tempus.add: callback is not a function')\r\n\r\n const uid = getUID()\r\n this.callbacks.push({ callback, priority, uid })\r\n this.callbacks.sort((a, b) => a.priority - b.priority)\r\n\r\n return () => this.remove(uid)\r\n }\r\n\r\n remove(uid) {\r\n this.callbacks = this.callbacks.filter(({ uid: u }) => uid !== u)\r\n }\r\n}\r\n\r\nclass Tempus {\r\n constructor() {\r\n /**\r\n * @private\r\n */\r\n this.framerates = {}\r\n\r\n /**\r\n * @private\r\n */\r\n this.time = performance.now()\r\n requestAnimationFrame(this.raf)\r\n }\r\n\r\n /**\r\n * @param {Function} callback\r\n * @param {{ priority?: number, fps?: number }} [options]\r\n * @returns {Function}\r\n */\r\n add(callback, { priority = 0, fps = Infinity } = {}) {\r\n if (typeof fps === 'number') {\r\n if (!this.framerates[fps]) this.framerates[fps] = new Framerate(fps)\r\n\r\n return this.framerates[fps].add({ callback, priority })\r\n }\r\n }\r\n\r\n /**\r\n * @private\r\n */\r\n raf = (time) => {\r\n requestAnimationFrame(this.raf, true)\r\n\r\n const deltaTime = time - this.time\r\n this.time = time\r\n\r\n for (const framerate of Object.values(this.framerates)) {\r\n framerate.raf(time, deltaTime)\r\n }\r\n }\r\n\r\n patch() {\r\n window.requestAnimationFrame = (callback, { priority = 0, fps = Infinity } = {}) => {\r\n if (callback === this.raf || !callback.toString().includes('requestAnimationFrame(')) {\r\n return originalRAF(callback)\r\n }\r\n\r\n if (!callback.__tempusPatched) {\r\n callback.__tempusPatched = true\r\n callback.__tempusUnsubscribe = this.add(callback, { priority, fps })\r\n }\r\n\r\n return callback.__tempusUnsubscribe\r\n }\r\n\r\n window.cancelAnimationFrame = (callback) => {\r\n if (typeof callback === 'function') callback?.()\r\n\r\n return originalCancelRAF(callback)\r\n }\r\n }\r\n\r\n unpatch() {\r\n window.requestAnimationFrame = originalRAF\r\n window.cancelAnimationFrame = originalCancelRAF\r\n }\r\n}\r\n\r\nconst isClient = typeof window !== 'undefined'\r\n\r\nexport default isClient && new Tempus()\r\n"],"names":["index","originalRAF","window","requestAnimationFrame","originalCancelRAF","cancelAnimationFrame","Framerate","fps","Infinity","this","callbacks","time","lastTickDate","performance","now","_proto","prototype","dispatch","deltaTime","i","length","callback","raf","executionTime","add","_ref","_this","priority","console","error","uid","push","sort","a","b","remove","filter","_ref2","key","get","Tempus","_this2","_i","_Object$values","Object","values","framerates","_proto2","_temp","_ref3","_ref3$priority","_ref3$fps","patch","_this3","_temp2","_ref4","_ref4$priority","_ref4$fps","toString","includes","__tempusPatched","__tempusUnsubscribe","unpatch"],"mappings":"yNAAA,IAAIA,EAAQ,ECINC,EAAcC,OAAOC,sBACrBC,EAAoBF,OAAOG,qBAE3BC,eACJ,WAAA,SAAAA,EAAYC,QAAG,IAAHA,IAAAA,EAAMC,UAChBC,KAAKC,UAAY,GACjBD,K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,EAEDe,QAAA,WACE5D,OAAOC,sBAAwBF,EAC/BC,OAAOG,qBAAuBD,CAChC,EAACoC,CAAA,CAhED,SAmEiC,oBAAXtC,QAEG,IAAIsC"} \ No newline at end of file +{"version":3,"file":"tempus.umd.js","sources":["../src/uid.js","../src/index.js"],"sourcesContent":["let index = 0\r\n\r\nexport function getUID() {\r\n return index++\r\n}\r\n","// Infinity = max FPS (system default)\r\n\r\nimport { getUID } from './uid'\r\n\r\nconst originalRAF = window.requestAnimationFrame\r\nconst originalCancelRAF = window.cancelAnimationFrame\r\n\r\nclass Framerate {\r\n constructor(fps = Infinity) {\r\n this.callbacks = []\r\n this.fps = fps\r\n this.time = 0\r\n this.lastTickDate = performance.now()\r\n }\r\n\r\n get executionTime() {\r\n return 1000 / this.fps\r\n }\r\n\r\n dispatch(time, deltaTime) {\r\n for (let i = 0; i < this.callbacks.length; i++) {\r\n this.callbacks[i].callback(time, deltaTime)\r\n }\r\n }\r\n\r\n raf(time, deltaTime) {\r\n this.time += deltaTime\r\n\r\n if (this.fps === Infinity) {\r\n this.dispatch(time, deltaTime)\r\n } else if (this.time >= this.executionTime) {\r\n this.time = this.time % this.executionTime\r\n const deltaTime = time - this.lastTickDate\r\n this.lastTickDate = time\r\n\r\n this.dispatch(time, deltaTime)\r\n }\r\n }\r\n\r\n add({ callback, priority }) {\r\n if (typeof callback !== 'function') console.error('Tempus.add: callback is not a function')\r\n\r\n const uid = getUID()\r\n this.callbacks.push({ callback, priority, uid })\r\n this.callbacks.sort((a, b) => a.priority - b.priority)\r\n\r\n return () => this.remove(uid)\r\n }\r\n\r\n remove(uid) {\r\n this.callbacks = this.callbacks.filter(({ uid: u }) => uid !== u)\r\n }\r\n}\r\n\r\nclass Tempus {\r\n constructor() {\r\n /**\r\n * @private\r\n */\r\n this.framerates = {}\r\n\r\n /**\r\n * @private\r\n */\r\n this.time = performance.now()\r\n requestAnimationFrame(this.raf)\r\n }\r\n\r\n /**\r\n * @param {Function} callback\r\n * @param {{ priority?: number, fps?: number }} [options]\r\n * @returns {Function}\r\n */\r\n add(callback, { priority = 0, fps = Infinity } = {}) {\r\n if (typeof fps === 'number') {\r\n if (!this.framerates[fps]) this.framerates[fps] = new Framerate(fps)\r\n\r\n return this.framerates[fps].add({ callback, priority })\r\n }\r\n }\r\n\r\n /**\r\n * @private\r\n */\r\n raf = (time) => {\r\n requestAnimationFrame(this.raf, true)\r\n\r\n const deltaTime = time - this.time\r\n this.time = time\r\n\r\n for (const framerate of Object.values(this.framerates)) {\r\n framerate.raf(time, deltaTime)\r\n }\r\n }\r\n\r\n patch() {\r\n window.requestAnimationFrame = (callback, { priority = 0, fps = Infinity } = {}) => {\r\n if (callback === this.raf || !callback.toString().includes('requestAnimationFrame(')) {\r\n return originalRAF(callback)\r\n }\r\n\r\n if (!callback.__tempusPatched) {\r\n callback.__tempusPatched = true\r\n callback.__tempusUnsubscribe = this.add(callback, { priority, fps })\r\n }\r\n\r\n return callback.__tempusUnsubscribe\r\n }\r\n\r\n window.cancelAnimationFrame = (callback) => {\r\n if (typeof callback === 'function') {\r\n callback?.()\r\n return\r\n }\r\n\r\n return originalCancelRAF(callback)\r\n }\r\n }\r\n\r\n unpatch() {\r\n window.requestAnimationFrame = originalRAF\r\n window.cancelAnimationFrame = originalCancelRAF\r\n }\r\n}\r\n\r\nconst isClient = typeof window !== 'undefined'\r\n\r\nexport default isClient && new Tempus()\r\n"],"names":["index","originalRAF","window","requestAnimationFrame","originalCancelRAF","cancelAnimationFrame","Framerate","fps","Infinity","this","callbacks","time","lastTickDate","performance","now","_proto","prototype","dispatch","deltaTime","i","length","callback","raf","executionTime","add","_ref","_this","priority","console","error","uid","push","sort","a","b","remove","filter","_ref2","key","get","Tempus","_this2","_i","_Object$values","Object","values","framerates","_proto2","_temp","_ref3","_ref3$priority","_ref3$fps","patch","_this3","_temp2","_ref4","_ref4$priority","_ref4$fps","toString","includes","__tempusPatched","__tempusUnsubscribe","unpatch"],"mappings":"yNAAA,IAAIA,EAAQ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qBAAuB,SAACgB,GAC7B,GAAwB,mBAAbA,EAKX,OAAOjB,EAAkBiB,GAJvBA,MAAAA,GAAAA,GAKJ,CACF,EAAC0B,EAEDe,QAAA,WACE5D,OAAOC,sBAAwBF,EAC/BC,OAAOG,qBAAuBD,CAChC,EAACoC,CAAA,CApES,SAuEuB,oBAAXtC,QAEG,IAAIsC"} \ No newline at end of file diff --git a/docs/main.js b/docs/main.js index 6bbf2bc..0d242e9 100644 --- a/docs/main.js +++ b/docs/main.js @@ -18,13 +18,14 @@ function onFrame(time, deltaTime) { } // const unsubscribe = Tempus.add(onFrame, { priority: 0 }) // max FPS, will executed before priority: 1 -// // Tempus.add(onFrame, { priority: 1 }) // max FPS, will executed after priority: 0 -// // Tempus.add(onFrame, { priority: 0, fps: 30 }) // 30 FPS, (e.g. playing frame sequence) +// const unsubscribe = Tempus.add(onFrame, { priority: 1 }) // max FPS, will executed after priority: 0 +// Tempus.add(onFrame, { priority: 0, fps: 30 }) // 30 FPS, (e.g. playing frame sequence) rAFID = requestAnimationFrame(onFrame) setTimeout(() => { - console.log(rAFID) + // unsubscribe() + // console.log(rAFID) cancelAnimationFrame(rAFID) // be sure to cancel the rAF }, 5000) diff --git a/src/index.js b/src/index.js index e74ef6f..d7c9ffd 100644 --- a/src/index.js +++ b/src/index.js @@ -108,7 +108,10 @@ class Tempus { } window.cancelAnimationFrame = (callback) => { - if (typeof callback === 'function') callback?.() + if (typeof callback === 'function') { + callback?.() + return + } return originalCancelRAF(callback) }