UNPKG

@kwiz/common

Version:

KWIZ common utilities and helpers for M365 platform

187 lines 6.96 kB
import { CommonLogger } from "../config"; import { isDebug } from "../helpers/debug"; import { getFromFullName, isFunction, isNullOrEmptyString, isNullOrUndefined, isString, isTypeofFullNameNullOrUndefined, typeofFullName } from "../helpers/typecheckers"; import { getResourceNonce } from "./sharepoint.rest/common"; const logger = new CommonLogger("sod"); // eslint-disable-next-line no-shadow var SodState; (function (SodState) { SodState["pending"] = "pending"; SodState["done"] = "done"; SodState["error"] = "error"; })(SodState || (SodState = {})); export default class Sod { constructor(url, sodName) { this.url = url; this.sodName = sodName; this.state = SodState.pending; this.notified = false; this.callbacks = []; this.script = null; } loadScript(scriptUrl, sync = false) { let self = this; var successCallback = () => { self.load(); }; var errorCallback = () => { self.error(); }; self.script = Sod.loadScript(scriptUrl, successCallback, errorCallback, sync); self.state = SodState.pending; } load() { let self = this; self.state = SodState.done; if (!self.notified) { self.notify(); } } error() { let self = this; if (isDebug()) logger.error('unhandled error in sod: 0x0000002'); //after error was logged - resolve all pending promises... //otherwise callers promises will never stop awaiting self.state = SodState.error; if (!self.notified) { self.notify(); } } notify() { let self = this; var callbackLength = self.callbacks.length; for (var i = 0; i < callbackLength; i++) { var sodCallback = self.callbacks[i]; if (!sodCallback.called && typeof (sodCallback.success) === "function") { try { self.state === SodState.error ? sodCallback.error() : sodCallback.success(); sodCallback.called = true; } catch (ex) { if (isDebug()) logger.error('unhandled error in sod: 0x0000001'); } } } self.notified = true; } reset() { let self = this; var callbackLength = self.callbacks.length; for (var i = 0; i < callbackLength; i++) { this.callbacks[i].called = false; } self.notified = false; } static loadScript(url, successCallback, errCallback, sync = false) { let scriptElm = document.createElement("script"); scriptElm.className = "kwiz-sod"; scriptElm.setAttribute("nonce", getResourceNonce()); scriptElm.nonce = getResourceNonce(); if (sync === true) { let req = new XMLHttpRequest(); req.open("GET", url, false); req.send(); scriptElm.appendChild(document.createTextNode(req.responseText)); successCallback(); } else { let agt = navigator.userAgent.toLowerCase(); let ie8down = agt.indexOf("msie") !== -1 && parseInt(agt.substring(agt.indexOf("msie ") + 5), 10) <= 8; let getCallback = (cb) => { return () => { var loaded = false; if (ie8down && typeof scriptElm.readyState !== "undefined") { loaded = scriptElm.readyState === "complete" || scriptElm.readyState === "loaded"; } else { loaded = true; } if (loaded) { scriptElm.onreadystatechange = null; scriptElm.onload = null; scriptElm.onerror = null; cb(); } }; }; scriptElm.type = "text/javascript"; scriptElm.src = url; if (ie8down) { scriptElm.onreadystatechange = getCallback(successCallback); } else { scriptElm.onload = getCallback(successCallback); scriptElm.onerror = getCallback(errCallback); } } (document.head || document.getElementsByTagName("HEAD")[0]).appendChild(scriptElm); return scriptElm; } static getGlobal(global) { if (isString(global)) return getFromFullName(global); else return global.getter(); } static ensureScriptNoPromise(scriptUrl, global, callbacks, sodName, sync = false) { if (!isNullOrEmptyString(global) && typeofFullName(Sod.getGlobal(global)) !== "undefined") { //this global object already exists, no need to reload this script. if (isFunction(callbacks && callbacks.success)) { callbacks.success(); } } else { sodName = (isNullOrEmptyString(sodName) === false && sodName || scriptUrl).toLowerCase(); var sod = Sod._getGlobalSod(sodName); if (!sod) { sod = Sod._addGlobalSod(sodName, scriptUrl); } if (!isNullOrUndefined(callbacks)) { sod.callbacks.push({ called: false, success: callbacks.success, error: callbacks.error }); } if (!sod.script) { sod.loadScript(scriptUrl, sync); } else if (sod.state === SodState.done || sod.state === SodState.error || sod.notified) { sod.notify(); } } } static async ensureScript(scriptUrl, global, callbacks, sodName, sync = false) { return new Promise((resolve, reject) => { let resolveCallback = () => { callbacks && callbacks.success(); resolve(); }; let rejectCallback = () => { callbacks && callbacks.error(); reject(); }; Sod.ensureScriptNoPromise(scriptUrl, global, { success: resolveCallback, error: rejectCallback }, sodName, sync); }); } static _initGlobalSods() { //static must be globally shared between all instances... if (isTypeofFullNameNullOrUndefined("g_kwizcom_sods")) { window.g_kwizcom_sods = {}; } } static _getGlobalSod(name) { Sod._initGlobalSods(); return window.g_kwizcom_sods[name]; } static _addGlobalSod(name, scriptUrl) { Sod._initGlobalSods(); window.g_kwizcom_sods[name] = new Sod(scriptUrl, name); return window.g_kwizcom_sods[name]; } } //# sourceMappingURL=sod.js.map