@adyen/adyen-web
Version:
[](https://www.npmjs.com/package/@adyen/adyen-web)
3 lines (2 loc) • 2.16 kB
JavaScript
import{useState as e,useRef as t,useEffect as o}from"../../external/preact/hooks/dist/hooks.js";import r from"../../core/Services/payment-status.js";import{processPaymentStatusResponse as a}from"../../core/ProcessResponse/PaymentStatus/PaymentStatus.js";import n from"../../core/Errors/AdyenCheckoutError.js";import{DEFAULT_PAYMENT_STATUS_TIMER_THROTTLE_TIME_MS as s,DEFAULT_PAYMENT_STATUS_TIMER_THROTTLE_INTERVAL_MS as l,DEFAULT_PAYMENT_STATUS_TIMER_DELAY_MS as p}from"./constants.js";function c(c){var i;const[u,d]=e(!1),[m,y]=e(!1),[f,h]=e(!0),[v,R]=e(null!==(i=c.delay)&&void 0!==i?i:p),[E,w]=e(100),[k,T]=e(0),[D,g]=e(!1),j=t(null),x=async()=>{const{paymentData:e,clientKey:t,throttleInterval:o,pollStatus:s}=c,l=null!=s?s:()=>r(e,t,c.loadingContext,o);var p;D||(null===(p=c.onActionHandled)||void 0===p||p.call(c,{componentType:c.type,actionDescription:"polling-started"}),g(!0));return l().then(a).catch(e=>({type:"network-error",props:e})).then(e=>{switch(e.type){case"success":(e=>{if(d(!0),h(!1),e.props.payload){const t={data:{details:{payload:e.props.payload},paymentData:c.paymentData}};c.onComplete(t)}else y(!0),c.onError(new n("ERROR","successful result, but no payload in response"))})(e);break;case"error":(e=>{if(y(!0),h(!1),e.props.payload){const t={data:{details:{payload:e.props.payload},paymentData:c.paymentData}};c.onComplete(t)}else{const e=new n("ERROR","error result with no payload in response");c.onError(e)}})(e);break;default:h(!1)}})};o(()=>{x()},[]),o(()=>{if(m||u||f)return;return j.current=setTimeout(()=>{(async()=>{var e,t;const o=performance.now();await x();const r=performance.now(),a=Math.round(r-o),n=k+a+v;var p;T(n),n>=(null!==(e=c.throttleTime)&&void 0!==e?e:s)&&v!==(null!==(t=c.throttleInterval)&&void 0!==t?t:l)&&R(null!==(p=c.throttleInterval)&&void 0!==p?p:l)})()},v),()=>{clearTimeout(j.current)}},[m,u,f,v,c.throttleTime,c.throttleInterval,k]);return{state:{completed:u,expired:m,loading:f,percentage:E,timePassed:k},actions:{onTick:e=>{w(e.percentage)},onTimeUp:()=>{y(!0),c.onError(new n("ERROR","Payment Expired"))}}}}export{c as usePaymentStatusTimer};
//# sourceMappingURL=usePaymentStatusTimer.js.map