UNPKG

azure-devops-ui

Version:

React components for building web UI in Azure DevOps

1 lines 1.62 kB
import{__awaiter,__generator}from"tslib";import*as React from"react";var makeCancelable=function(e){var u=!1,o=new Promise(function(n,t){function r(){return u&&(o.catch(function(){}),t({isCanceled:!0})),u}e.then(function(e){return r()||n(e)}),e.catch(function(e){return r()||t(e)})});return{promise:o,cancel:function(){u=!0}}};function timeout(e,u,o){return new Promise(function(n,t){var r=setTimeout(function(){t(null==o?"Timed out after ".concat(u," ms."):o)},u);e.then(function(e){n(e),clearTimeout(r)},function(e){t(e),clearTimeout(r)})})}function wait(n){return __awaiter(this,void 0,void 0,function(){return __generator(this,function(e){return[2,new Promise(function(e){setTimeout(e,n)})]})})}function delay(n,t){return __awaiter(this,void 0,void 0,function(){return __generator(this,function(e){switch(e.label){case 0:return[4,Promise.all([n,wait(t)])];case 1:return[2,e.sent()[0]]}})})}function allSettled(r){var u=new Array(r.length);return new Promise(function(e){for(var t=0,n=0;n<r.length;++n)!function(n){r[n].then(function(e){u[n]={state:"fulfilled",value:e}},function(e){u[n]={state:"rejected",reason:e}}).then(function(){++t===r.length&&e(u)})}(n)})}function usePromise(){var n=React.useRef([]);return React.useEffect(function(){return function(){n.current&&(n.current.forEach(function(e){return e.cancel()}),n.current=null)}},[]),{trackPromise:React.useCallback(function(e){e=makeCancelable(e);if(n.current)return n.current.push(e),e.promise;throw new Error("usePromise hook: `trackPromise` called after using component was unmounted.")},[])}}export{makeCancelable,timeout,wait,delay,allSettled,usePromise};