{"version":3,"file":"safeSetTimeout.js","sourceRoot":"../src/","sources":["safeSetTimeout.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAEpD;;;;;GAKG;AACH,MAAM,CAAC,IAAM,cAAc,GAAG,UAAC,SAA0B;IACvD,IAAI,cAAiC,CAAC;IAEtC,OAAO,UAAC,EAAY,EAAE,QAAgB;QACpC,IAAI,CAAC,cAAc,EAAE;YACnB,cAAc,GAAG,IAAI,GAAG,EAAgB,CAAC;YAEzC,eAAe,CAAC,SAAS,EAAE;gBACzB,oBAAoB,EAAE;oBACpB,cAAc,CAAC,OAAO,CAAC,UAAC,EAAgB,IAAK,OAAA,YAAY,CAAC,EAAE,CAAC,EAAhB,CAAgB,CAAC,CAAC;gBACjE,CAAC;aACF,CAAC,CAAC;SACJ;QAED,IAAM,SAAS,GAAG,UAAU,CAAC;YAC3B,cAAc,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;YACjC,EAAE,EAAE,CAAC;QACP,CAAC,EAAE,QAAQ,CAAC,CAAC;QACb,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 setTimeout. 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 safeSetTimeout = (component: React.Component) => {\r\n  let activeTimeouts: Set<NodeJS.Timer>;\r\n\r\n  return (cb: Function, duration: number) => {\r\n    if (!activeTimeouts) {\r\n      activeTimeouts = new Set<NodeJS.Timer>();\r\n\r\n      extendComponent(component, {\r\n        componentWillUnmount: () => {\r\n          activeTimeouts.forEach((id: NodeJS.Timer) => clearTimeout(id));\r\n        },\r\n      });\r\n    }\r\n\r\n    const timeoutId = setTimeout(() => {\r\n      activeTimeouts.delete(timeoutId);\r\n      cb();\r\n    }, duration);\r\n    activeTimeouts.add(timeoutId);\r\n  };\r\n};\r\n"]}