You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
1 line
3.3 KiB
1 line
3.3 KiB
{"ast":null,"code":"import _asyncToGenerator from \"@babel/runtime/helpers/asyncToGenerator\";\nimport _slicedToArray from \"@babel/runtime/helpers/slicedToArray\";\nimport * as React from 'react';\nexport default function useThenable(create) {\n var _React$useState = React.useState(create),\n _React$useState2 = _slicedToArray(_React$useState, 1),\n promise = _React$useState2[0];\n var initialState = [false, undefined];\n promise.then(function (result) {\n initialState = [true, result];\n });\n var _React$useState3 = React.useState(initialState),\n _React$useState4 = _slicedToArray(_React$useState3, 2),\n state = _React$useState4[0],\n setState = _React$useState4[1];\n var _state = _slicedToArray(state, 1),\n resolved = _state[0];\n React.useEffect(function () {\n var cancelled = false;\n var resolve = function () {\n var _ref = _asyncToGenerator(function* () {\n var result;\n try {\n result = yield promise;\n } finally {\n if (!cancelled) {\n setState([true, result]);\n }\n }\n });\n return function resolve() {\n return _ref.apply(this, arguments);\n };\n }();\n if (!resolved) {\n resolve();\n }\n return function () {\n cancelled = true;\n };\n }, [promise, resolved]);\n return state;\n}","map":{"version":3,"sources":["useThenable.tsx"],"names":["React","useThenable","create","promise","useState","initialState","undefined","then","result","state","setState","resolved","useEffect","cancelled","resolve"],"mappings":";;AAAA,OAAO,KAAKA,KAAK,MAAM,OAAO;AAE9B,eAAe,SAASC,WAAW,CAAIC,MAA4B,EAAE;EACnE,sBAAkBF,KAAK,CAACI,QAAQ,CAACF,MAAM,CAAC;IAAA;IAAjCC,OAAO;EAEd,IAAIE,YAAsC,GAAG,CAAC,KAAK,EAAEC,SAAS,CAAC;EAG/DH,OAAO,CAACI,IAAI,CAAEC,UAAAA,MAAM,EAAK;IACvBH,YAAY,GAAG,CAAC,IAAI,EAAEG,MAAM,CAAC;EAC/B,CAAC,CAAC;EAEF,uBAA0BR,KAAK,CAACI,QAAQ,CAACC,YAAY,CAAC;IAAA;IAA/CI,KAAK;IAAEC,QAAQ;EACtB,4BAAmBD,KAAK;IAAjBE,QAAQ;EAEfX,KAAK,CAACY,SAAS,CAAC,YAAM;IACpB,IAAIC,SAAS,GAAG,KAAK;IAErB,IAAMC,OAAO;MAAA,6BAAG,aAAY;QAC1B,IAAIN,MAAM;QAEV,IAAI;UACFA,MAAM,SAASL,OAAO;QACxB,CAAC,SAAS;UACR,IAAI,CAACU,SAAS,EAAE;YACdH,QAAQ,CAAC,CAAC,IAAI,EAAEF,MAAM,CAAC,CAAC;UAC1B;QACF;MACF,CAAC;MAAA,gBAVKM,OAAO;QAAA;MAAA;IAAA,GAUZ;IAED,IAAI,CAACH,QAAQ,EAAE;MACbG,OAAO,EAAE;IACX;IAEA,OAAO,YAAM;MACXD,SAAS,GAAG,IAAI;IAClB,CAAC;EACH,CAAC,EAAE,CAACV,OAAO,EAAEQ,QAAQ,CAAC,CAAC;EAEvB,OAAOF,KAAK;AACd","sourceRoot":"../../src","sourcesContent":["import * as React from 'react';\nexport default function useThenable(create) {\n const [promise] = React.useState(create);\n let initialState = [false, undefined];\n\n // Check if our thenable is synchronous\n promise.then(result => {\n initialState = [true, result];\n });\n const [state, setState] = React.useState(initialState);\n const [resolved] = state;\n React.useEffect(() => {\n let cancelled = false;\n const resolve = async () => {\n let result;\n try {\n result = await promise;\n } finally {\n if (!cancelled) {\n setState([true, result]);\n }\n }\n };\n if (!resolved) {\n resolve();\n }\n return () => {\n cancelled = true;\n };\n }, [promise, resolved]);\n return state;\n}\n//# sourceMappingURL=useThenable.js.map"]},"metadata":{},"sourceType":"module"} |