@uifabric/utilities
Version:
Fluent UI React utilities for building components.
1 lines • 1.79 kB
Source Map (JSON)
{"version":3,"file":"safeRequestAnimationFrame.js","sourceRoot":"../src/","sources":["safeRequestAnimationFrame.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAEpD;;;;;GAKG;AACH,MAAM,CAAC,IAAM,yBAAyB,GAAG,UAAC,SAA0B;IAClE,IAAI,cAA2B,CAAC;IAEhC,OAAO,UAAC,EAAY;QAClB,IAAI,CAAC,cAAc,EAAE;YACnB,cAAc,GAAG,IAAI,GAAG,EAAU,CAAC;YAEnC,eAAe,CAAC,SAAS,EAAE;gBACzB,oBAAoB,EAAE;oBACpB,cAAc,CAAC,OAAO,CAAC,UAAC,EAAU,IAAK,OAAA,oBAAoB,CAAC,EAAE,CAAC,EAAxB,CAAwB,CAAC,CAAC;gBACnE,CAAC;aACF,CAAC,CAAC;SACJ;QAED,IAAM,SAAS,GAAG,qBAAqB,CAAC;YACtC,cAAc,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;YACjC,EAAE,EAAE,CAAC;QACP,CAAC,CAAC,CAAC;QAEH,cAAc,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;IAChC,CAAC,CAAC;AACJ,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"]}