UNPKG

@wordpress/block-editor

Version:
8 lines (7 loc) 1.89 kB
{ "version": 3, "sources": ["../../../src/components/block-preview/async.js"], "sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { useEffect, useState, flushSync } from '@wordpress/element';\nimport { createQueue } from '@wordpress/priority-queue';\n\nconst blockPreviewQueue = createQueue();\n\n/**\n * Renders a component at the next idle time.\n * @param {*} props\n */\nexport function Async( { children, placeholder } ) {\n\tconst [ shouldRender, setShouldRender ] = useState( false );\n\n\t// In the future, we could try to use startTransition here, but currently\n\t// react will batch all transitions, which means all previews will be\n\t// rendered at the same time.\n\t// https://react.dev/reference/react/startTransition#caveats\n\t// > If there are multiple ongoing Transitions, React currently batches them\n\t// > together. This is a limitation that will likely be removed in a future\n\t// > release.\n\n\tuseEffect( () => {\n\t\tconst context = {};\n\t\tblockPreviewQueue.add( context, () => {\n\t\t\t// Synchronously run all renders so it consumes timeRemaining.\n\t\t\t// See https://github.com/WordPress/gutenberg/pull/48238\n\t\t\tflushSync( () => {\n\t\t\t\tsetShouldRender( true );\n\t\t\t} );\n\t\t} );\n\t\treturn () => {\n\t\t\tblockPreviewQueue.cancel( context );\n\t\t};\n\t}, [] );\n\n\tif ( ! shouldRender ) {\n\t\treturn placeholder;\n\t}\n\n\treturn children;\n}\n"], "mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,qBAA+C;AAC/C,4BAA4B;AAE5B,IAAM,wBAAoB,mCAAY;AAM/B,SAAS,MAAO,EAAE,UAAU,YAAY,GAAI;AAClD,QAAM,CAAE,cAAc,eAAgB,QAAI,yBAAU,KAAM;AAU1D,gCAAW,MAAM;AAChB,UAAM,UAAU,CAAC;AACjB,sBAAkB,IAAK,SAAS,MAAM;AAGrC,oCAAW,MAAM;AAChB,wBAAiB,IAAK;AAAA,MACvB,CAAE;AAAA,IACH,CAAE;AACF,WAAO,MAAM;AACZ,wBAAkB,OAAQ,OAAQ;AAAA,IACnC;AAAA,EACD,GAAG,CAAC,CAAE;AAEN,MAAK,CAAE,cAAe;AACrB,WAAO;AAAA,EACR;AAEA,SAAO;AACR;", "names": [] }