raf-handler
Version:
Use for centralize request animation frame
1 lines • 3.05 kB
Source Map (JSON)
{"version":3,"file":"index.mjs","sources":["../src/index.ts"],"sourcesContent":["if (!window || !window.requestAnimationFrame) {\n window.requestAnimationFrame = () => null;\n window.cancelAnimationFrame = () => null;\n}\n\nexport interface TypesOptions {\n fps?: number;\n}\n\nexport interface TypesStore {\n rAF?: { (timestamp: number): void }[];\n fps: number;\n then: number;\n id?: number;\n now?: number;\n delta?: number;\n interval?: number;\n}\n\nexport const store: TypesStore = {\n rAF: [],\n fps: 60,\n then: Date.now(),\n};\n\nexport const update = (timestamp: number) => {\n const { rAF } = store;\n\n store.interval = 1000 / store.fps;\n store.now = Date.now();\n store.delta = store.now - store.then;\n\n if (store.delta > store.interval) {\n store.then = store.now - (store.delta % store.interval);\n\n for (let i = 0, { length } = rAF; i < length; i += 1) rAF[i](timestamp);\n }\n\n return requestAnimationFrame(update);\n};\n\nexport const resetUpdate = () => {\n if (store.id) window.cancelAnimationFrame(store.id);\n store.id = null;\n store.rAF = null;\n};\n\nexport const subscribeUpdate = (fn: (timestamp: number) => any) => {\n if (!store.rAF) store.rAF = [];\n store.rAF.push(fn);\n};\n\nexport const unsubscribeUpdate = (fn: (timestamp: number) => any) => {\n const { rAF } = store;\n for (let i = 0, { length } = rAF; i < length; i += 1) {\n if (rAF[i] === fn) {\n store.rAF.splice(i, 1);\n i--;\n }\n }\n};\n\nconst initUpdate = (options?: TypesOptions) => {\n if (options?.fps) store.fps = options.fps;\n\n store.id = window?.requestAnimationFrame(update);\n\n return {\n reset: resetUpdate,\n add: subscribeUpdate,\n remove: unsubscribeUpdate,\n update,\n store,\n };\n};\n\nexport default initUpdate;\n"],"names":["window","requestAnimationFrame","cancelAnimationFrame","store","rAF","fps","then","Date","now","update","timestamp","interval","delta","i","length","resetUpdate","id","subscribeUpdate","fn","push","unsubscribeUpdate","splice","initUpdate","options","_window","reset","add","remove"],"mappings":"AAAKA,QAAWA,OAAOC,wBACrBD,OAAOC,sBAAwB,wBAC/BD,OAAOE,qBAAuB,yBAiBnBC,IAAAA,EAAoB,CAC/BC,IAAK,GACLC,IAAK,GACLC,KAAMC,KAAKC,OAGAC,EAAS,SAATA,EAAUC,GACrB,IAAQN,EAAQD,EAARC,IAMR,GAJAD,EAAMQ,SAAW,IAAOR,EAAME,IAC9BF,EAAMK,IAAMD,KAAKC,MACjBL,EAAMS,MAAQT,EAAMK,IAAML,EAAMG,KAE5BH,EAAMS,MAAQT,EAAMQ,SAAU,CAChCR,EAAMG,KAAOH,EAAMK,IAAOL,EAAMS,MAAQT,EAAMQ,SAE9C,IAAS,IAAAE,EAAI,EAAKC,EAAWV,EAAXU,OAAgBD,EAAIC,EAAQD,GAAK,EAAGT,EAAIS,GAAGH,GAG/D,OAAOT,sBAAsBQ,IAGlBM,EAAc,WACrBZ,EAAMa,IAAIhB,OAAOE,qBAAqBC,EAAMa,IAChDb,EAAMa,GAAK,KACXb,EAAMC,IAAM,MAGDa,EAAkB,SAACC,GACzBf,EAAMC,MAAKD,EAAMC,IAAM,IAC5BD,EAAMC,IAAIe,KAAKD,IAGJE,EAAoB,SAACF,GAEhC,IADA,IAAQd,EAAQD,EAARC,IACCS,EAAI,EAAKC,EAAWV,EAAXU,OAAgBD,EAAIC,EAAQD,GAAK,EAC7CT,EAAIS,KAAOK,IACbf,EAAMC,IAAIiB,OAAOR,EAAG,GACpBA,MAKAS,EAAa,SAACC,SAKlB,aAJIA,GAAAA,EAASlB,MAAKF,EAAME,IAAMkB,EAAQlB,KAEtCF,EAAMa,YAAKhB,eAAAwB,EAAQvB,sBAAsBQ,GAElC,CACLgB,MAAOV,EACPW,IAAKT,EACLU,OAAQP,EACRX,OAAAA,EACAN,MAAAA"}