@adyen/adyen-web
Version:
[](https://www.npmjs.com/package/@adyen/adyen-web)
3 lines (2 loc) • 3.47 kB
JavaScript
import t from"../core/Errors/AdyenCheckoutError.js";import{AnalyticsInfoEvent as s,InfoEventType as i}from"../core/Analytics/events/AnalyticsInfoEvent.js";function e(t,s,i){return s in t?Object.defineProperty(t,s,{value:i,enumerable:!0,configurable:!0,writable:!0}):t[s]=i,t}class r{removeScript(){var t,s;this.cleanupListeners(),null===(s=this.script)||void 0===s||null===(t=s.parentNode)||void 0===t||t.removeChild(this.script),this.script=null}cleanupListeners(){this.script&&(this.script.removeEventListener("load",this.handleOnLoad),this.script.removeEventListener("error",this.handleOnError))}attachListeners(){this.script&&(this.cleanupListeners(),this.script.addEventListener("load",this.handleOnLoad),this.script.addEventListener("error",this.handleOnError))}loadScript(){return new Promise((s,i)=>{var e;const r=document.querySelector(this.node);r?(this.resolveLoadScript=s,this.rejectLoadScript=i,this.script=r.querySelector(`script[src="${this.src}"]`),(null===(e=this.script)||void 0===e?void 0:e.getAttribute("data-script-loaded"))?this.resolveLoadScript():this.script?this.attachListeners():(this.script=document.createElement("script"),Object.assign(this.script,this.attributes),Object.assign(this.script.dataset,this.dataAttributes),this.script.src=this.src,this.script.async=!0,this.attachListeners(),r.appendChild(this.script))):i(new t("SCRIPT_ERROR",`Unable to find script container node: ${this.node}`))})}trackEvent(t){var i;const e=new s({type:t,component:this.component,cdnUrl:this.baseUrl});null===(i=this.analytics)||void 0===i||i.sendAnalytics(e)}constructor({src:s,component:o,node:n="body",attributes:a,dataAttributes:c,analytics:l}){e(this,"src",void 0),e(this,"component",void 0),e(this,"node",void 0),e(this,"attributes",void 0),e(this,"dataAttributes",void 0),e(this,"analytics",void 0),e(this,"baseUrl",void 0),e(this,"script",void 0),e(this,"loadPromise",null),e(this,"rejectLoadPromise",null),e(this,"resolveLoadScript",null),e(this,"rejectLoadScript",null),e(this,"handleOnLoad",()=>{var t,s;null===(t=this.script)||void 0===t||t.setAttribute("data-script-loaded","true"),this.cleanupListeners(),null===(s=this.resolveLoadScript)||void 0===s||s.call(this)}),e(this,"handleOnError",s=>{var i;this.cleanupListeners();const e=new t("SCRIPT_ERROR",`Unable to load script ${this.src}.${(null==s?void 0:s.message)&&`Message: ${s.message}`}`,{cause:(null==s?void 0:s.error)||s});null===(i=this.rejectLoadScript)||void 0===i||i.call(this,e)}),e(this,"load",()=>(null!==this.loadPromise||(this.loadPromise=new Promise((t,s)=>{this.rejectLoadPromise=s;let e=0;this.trackEvent(i.sdkDownloadInitiated);const o=async()=>{try{e++,await this.loadScript(),this.trackEvent(i.sdkDownloadCompleted),t()}catch(t){if(null===this.loadPromise)return;this.trackEvent(i.sdkDownloadFailed),this.removeScript(),e<r.MAX_NUMBER_OF_RETRIES?setTimeout(()=>{o()},r.RETRY_DELAY):(this.trackEvent(i.sdkDownloadAborted),this.loadPromise=null,this.rejectLoadPromise=null,s(t))}};o()})),this.loadPromise)),e(this,"remove",()=>{var s;null===(s=this.rejectLoadPromise)||void 0===s||s.call(this,new t("CANCEL","Script loading cancelled.")),this.removeScript(),this.loadPromise=null}),this.src=s,this.component=o,this.node=n,this.attributes=a,this.dataAttributes=c,this.analytics=l,this.baseUrl=function(t){const s=new URL(t);return s.origin+s.pathname}(this.src)}}e(r,"RETRY_DELAY",1e3),e(r,"MAX_NUMBER_OF_RETRIES",3);export{r as default};
//# sourceMappingURL=Script.js.map