1 | {"version":3,"file":"safeRequestAnimationFrame.js","sourceRoot":"../src/","sources":["safeRequestAnimationFrame.ts"],"names":[],"mappings":";;;IAGA;;;;;OAKG;IACU,QAAA,yBAAyB,GAAG,UAAC,SAA0B;QAClE,IAAI,cAA2B,CAAC;QAEhC,OAAO,UAAC,EAAY;YAClB,IAAI,CAAC,cAAc,EAAE;gBACnB,cAAc,GAAG,IAAI,GAAG,EAAU,CAAC;gBAEnC,iCAAe,CAAC,SAAS,EAAE;oBACzB,oBAAoB,EAAE;wBACpB,cAAc,CAAC,OAAO,CAAC,UAAC,EAAU,IAAK,OAAA,oBAAoB,CAAC,EAAE,CAAC,EAAxB,CAAwB,CAAC,CAAC;oBACnE,CAAC;iBACF,CAAC,CAAC;aACJ;YAED,IAAM,SAAS,GAAG,qBAAqB,CAAC;gBACtC,cAAc,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;gBACjC,EAAE,EAAE,CAAC;YACP,CAAC,CAAC,CAAC;YAEH,cAAc,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;QAChC,CAAC,CAAC;IACJ,CAAC,CAAC","sourcesContent":["import * as React from 'react';\r\nimport { extendComponent } from './extendComponent';\r\n\r\n/**\r\n * Generates a function to be attached to a React component, which can be called\r\n * as a replacement to RAF. In-flight async calls will be auto canceled if the component\r\n * is unmounting before the async code is executed, preventing bugs where code\r\n * accesses things within the component after being unmounted.\r\n */\r\nexport const safeRequestAnimationFrame = (component: React.Component): ((cb: Function) => void) => {\r\n let activeTimeouts: Set<number>;\r\n\r\n return (cb: Function) => {\r\n if (!activeTimeouts) {\r\n activeTimeouts = new Set<number>();\r\n\r\n extendComponent(component, {\r\n componentWillUnmount: () => {\r\n activeTimeouts.forEach((id: number) => cancelAnimationFrame(id));\r\n },\r\n });\r\n }\r\n\r\n const timeoutId = requestAnimationFrame(() => {\r\n activeTimeouts.delete(timeoutId);\r\n cb();\r\n });\r\n\r\n activeTimeouts.add(timeoutId);\r\n };\r\n};\r\n"]} |