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AI,EAAE,eAAe,CAAC,CAAC;wBAExE,aAAa;wBACb,KAAI,CAAC,QAAQ,CACX;4BACE,SAAS,EAAE,eAAe;yBAC3B,EACD,OAAO,CAAC,MAAM,CACf,CAAC;qBACH;gBACH,CAAC,CAAC;qBACD,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;aAC3B;QACH,CAAC;QACH,YAAC;IAAD,CAAC,AA9CD,CAAoB,KAAK,CAAC,SAAS,GA8ClC;IAED,OAAO,KAAK,CAAC,UAAU,CACrB,UAAC,KAAK,EAAE,GAAG,IAAK,OAAA,oBAAC,KAAK,eAAK,KAAK,IAAE,YAAY,EAAE,GAAG,IAAI,EAAvC,CAAuC,CACxD,CAAC;AACJ,CAAC","sourcesContent":["/**\n * asAsync - a HOC for async loading components.\n *\n * Usage:\n *\n * const AsyncDialog = asAsync({\n * load: () => import('Dialog').then(result => result.default),\n * });\n *\n * React.render(domElement, <AsyncDialog asyncPlaceholder={ () => <Spinner/> } { ...dialogProps } />);\n *\n * Note the `asyncPlaceholder` prop will be respected when rendering the async component and it hasn't\n * been loaded yet.\n */\n\nimport * as React from 'react';\n\nexport interface IAsAsyncOptions<TProps> {\n /**\n * Callback which returns a promise resolving an object which exports the component.\n */\n load: () => Promise<React.ElementType<TProps>>;\n\n /**\n * Callback executed when async loading is complete.\n */\n onLoad?: () => void;\n\n /**\n * Callback when async loading fails.\n */\n onError?: (error: Error) => void;\n}\n\n/**\n * If possible, use a WeakMap to maintain a cache of loaded components.\n * This can be used to synchronously render components that have already been loaded,\n * rather than having to wait for at least one async tick.\n */\nconst _syncModuleCache =\n typeof WeakMap !== 'undefined'\n ? // eslint-disable-next-line @typescript-eslint/no-explicit-any\n new WeakMap<() => Promise<React.ElementType<any>>, React.ElementType<any> | undefined>()\n : undefined;\n\n/**\n * Produces a component which internally loads the target component before first mount.\n * The component passes all props through to the loaded component.\n *\n * This overload accepts a module with a default export for the component.\n */\nexport function asAsync<TProps>(options: IAsAsyncOptions<TProps>) {\n class Async extends React.Component<\n TProps & {\n asyncPlaceholder?: React.ElementType;\n forwardedRef: React.Ref<React.ElementType<TProps>>;\n },\n { Component?: React.ElementType<TProps> }\n > {\n public state = {\n Component: _syncModuleCache ? (_syncModuleCache.get(options.load) as React.ElementType<TProps>) : undefined,\n };\n\n public render(): JSX.Element | null {\n // Typescript issue: the rest can't be pulled without the any cast, as TypeScript fails with rest on generics.\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const { forwardedRef, asyncPlaceholder: Placeholder, ...rest } = this.props as any;\n const { Component } = this.state;\n return Component ? (\n React.createElement(Component, { ...rest, ref: forwardedRef })\n ) : Placeholder ? (\n <Placeholder />\n ) : null;\n }\n\n public componentDidMount(): void {\n let { Component } = this.state;\n\n if (!Component) {\n options\n .load()\n .then((LoadedComponent: React.ElementType<TProps>) => {\n if (LoadedComponent) {\n // Cache component for future reference.\n _syncModuleCache && _syncModuleCache.set(options.load, LoadedComponent);\n\n // Set state.\n this.setState(\n {\n Component: LoadedComponent,\n },\n options.onLoad,\n );\n }\n })\n .catch(options.onError);\n }\n }\n }\n\n return React.forwardRef<React.ElementType<TProps>, TProps & { asyncPlaceholder?: React.ElementType }>(\n (props, ref) => <Async {...props} forwardedRef={ref} />,\n );\n}\n"]}
\No newline at end of file