@quadible/web-sdk
Version:
The web sdk for Quadible's behavioral authentication service.
62 lines • 3.28 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.loadSource = loadSource;
exports.loadSources = loadSources;
const async_1 = require("./async");
const data_1 = require("./data");
function isFinalResultLoaded(loadResult) {
return typeof loadResult !== 'function';
}
function loadSource(source, sourceOptions) {
const sourceLoadPromise = (0, async_1.suppressUnhandledRejectionWarning)(new Promise((resolveLoad) => {
const loadStartTime = Date.now();
// `awaitIfAsync` is used instead of just `await` in order to measure the duration of synchronous sources
// correctly (other microtasks won't affect the duration).
(0, async_1.awaitIfAsync)(source.bind(null, sourceOptions), (...loadArgs) => {
const loadDuration = Date.now() - loadStartTime;
// Source loading failed
if (!loadArgs[0]) {
return resolveLoad(() => ({ error: loadArgs[1], d: loadDuration }));
}
const loadResult = loadArgs[1];
// Source loaded with the final result
if (isFinalResultLoaded(loadResult)) {
return resolveLoad(() => ({ value: loadResult, d: loadDuration }));
}
// Source loaded with "get" stage
resolveLoad(() => new Promise((resolveGet) => {
const getStartTime = Date.now();
(0, async_1.awaitIfAsync)(loadResult, (...getArgs) => {
const duration = loadDuration + Date.now() - getStartTime;
// Source getting failed
if (!getArgs[0]) {
return resolveGet({ error: getArgs[1], d: duration });
}
// Source getting succeeded
resolveGet({ value: getArgs[1], d: duration });
});
}));
});
}));
return function getComponent() {
return sourceLoadPromise.then((finalizeSource) => finalizeSource());
};
}
function loadSources(sources, sourceOptions, excludeSources, loopReleaseInterval) {
const includedSources = Object.keys(sources).filter((sourceKey) => (0, data_1.excludes)(excludeSources, sourceKey));
// Using `mapWithBreaks` allows asynchronous sources to complete between synchronous sources
// and measure the duration correctly
const sourceGettersPromise = (0, async_1.suppressUnhandledRejectionWarning)((0, async_1.mapWithBreaks)(includedSources, (sourceKey) => loadSource(sources[sourceKey], sourceOptions), loopReleaseInterval));
return async function getComponents() {
const sourceGetters = await sourceGettersPromise;
const componentPromises = await (0, async_1.mapWithBreaks)(sourceGetters, (sourceGetter) => (0, async_1.suppressUnhandledRejectionWarning)(sourceGetter()), loopReleaseInterval);
const componentArray = await Promise.all(componentPromises);
// Keeping the component keys order the same as the source keys order
const components = {};
for (let index = 0; index < includedSources.length; ++index) {
components[includedSources[index]] = componentArray[index];
}
return components;
};
}
//# sourceMappingURL=entropy_source.js.map