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