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AC,CAAC,CAAC,OAAO,CAAC;YACT,cAAc,CAAC,IAAI,EAAE,CAAC;QACxB,CAAC,CAAC,CAAC;IACL,CAAC;IAED,OAAO,OAAO,CAAC,OAAO,EAAE,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;AACzC,CAAC","sourcesContent":["import * as React from 'react';\nimport { getApolloContext } from '../context';\nimport { RenderPromises } from './RenderPromises';\n\nexport function getDataFromTree(\n tree: React.ReactNode,\n context: { [key: string]: any } = {}\n) {\n return getMarkupFromTree({\n tree,\n context,\n // If you need to configure this renderFunction, call getMarkupFromTree\n // directly instead of getDataFromTree.\n renderFunction: require('react-dom/server').renderToStaticMarkup\n });\n}\n\nexport type GetMarkupFromTreeOptions = {\n tree: React.ReactNode;\n context?: { [key: string]: any };\n renderFunction?: (\n tree: React.ReactElement<any>,\n ) => string | PromiseLike<string>;\n};\n\nexport function getMarkupFromTree({\n tree,\n context = {},\n // The rendering function is configurable! We use renderToStaticMarkup as\n // the default, because it's a little less expensive than renderToString,\n // and legacy usage of getDataFromTree ignores the return value anyway.\n renderFunction = require('react-dom/server').renderToStaticMarkup\n}: GetMarkupFromTreeOptions): Promise<string> {\n const renderPromises = new RenderPromises();\n\n function process(): Promise<string> {\n // Always re-render from the rootElement, even though it might seem\n // better to render the children of the component responsible for the\n // promise, because it is not possible to reconstruct the full context\n // of the original rendering (including all unknown context provider\n // elements) for a subtree of the original component tree.\n const ApolloContext = getApolloContext();\n\n return new Promise<string>(resolve => {\n const element = React.createElement(\n ApolloContext.Provider,\n { value: { ...context, renderPromises }},\n tree,\n );\n resolve(renderFunction(element));\n }).then(html => {\n return renderPromises.hasPromises()\n ? renderPromises.consumeAndAwaitPromises().then(process)\n : html;\n }).finally(() => {\n renderPromises.stop();\n });\n }\n\n return Promise.resolve().then(process);\n}\n"]}
\No newline at end of file