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