UNPKG

@aappddeevv/dynamics-client-ui

Version:

## What is it? A library to help you create great dynamics applications.

51 lines 1.83 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); /** * Get Xrm from a Promise. Uses polling to wait for * Xrm to become available. Default polling lasts 60 seconds * every 1/2 second. This only checks for Xrm on the parent window * it does *not* walk the window hierarchy. */ function getXrmP(maxIter = 120, delta = 500, getXrm = () => window.parent.Xrm) { return wait(() => { const x = getXrm(); return typeof x !== "undefined"; }, maxIter, delta).then(() => getXrm()); } exports.getXrmP = getXrmP; /** * Get FormContext as a promise either from Xrm.Page or through the * FormContextHolder. */ function getFormContextP(maxIter = 120, delta = 500) { const fchP = wait(() => { const tmp = window.parent; return typeof tmp.FormContextP !== "undefined"; }, maxIter, delta).then(() => window.parent.FormContextP); const parentP = wait(() => { const x = window.parent.Xrm; return x && (typeof x.Page !== "undefined"); }, maxIter, delta).then(() => window.parent.Xrm.Page); return Promise.race([parentP, fchP]); } exports.getFormContextP = getFormContextP; /** Wait for some condition to be true. Uses polling (setInterval) to iterate. */ function wait(cond, maxIter, delta) { if (maxIter <= 0 || delta <= 0) throw Error("maxIter and delta must be > 0"); return new Promise(function (resolve, reject) { let count = 0; const cancellable = setInterval(function () { if (count > maxIter) reject(null); count = count + 1; const proceed = cond(count); if (proceed) { clearInterval(cancellable); resolve(); } }, delta); }); } exports.wait = wait; //# sourceMappingURL=getXrmP.js.map