Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

digiTrustIdSystem.js add the synchronous behavior to facade call of D… #3913

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 44 additions & 12 deletions modules/digiTrustIdSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,21 +85,23 @@ function writeDigiId(id) {
}

/**
* Set up a DigiTrust fascade object to mimic the API
* Set up a DigiTrust facade object to mimic the API
*
*/
function initDigitrustFascade(config) {
function initDigitrustFacade(config) {
var _savedId = null; // closure variable for storing Id to avoid additional requests
var fascade = {
var facade = {
isClient: true,
isMock: true,
_internals: {
callCount: 0,
initCallback: null
},
getUser: function (obj, callback) {
var cb = callback || noop;
var inter = fascade._internals;
var isAsync = !!isFunc(callback);
var cb = isAsync ? callback : noop;
var errResp = { success: false };
var inter = facade._internals;
inter.callCount++;

// wrap the initializer callback, if present
Expand All @@ -113,10 +115,23 @@ function initDigitrustFascade(config) {
}
}

if (!isMemberIdValid) {
if (!isAsync) {
return errResp
} else {
cb(errResp);
return;
}
}

if (_savedId != null) {
checkCallInitializeCb(_savedId);
cb(_savedId);
return;
if (isAsync) {
cb(_savedId);
return;
} else {
return _savedId;
}
}

var opts = {
Expand All @@ -140,14 +155,31 @@ function initDigitrustFascade(config) {
}

callApi(opts);

if (!isAsync) {
return errResp; // even if it will be successful later, without a callback we report a "failure in this moment"
}
}
}

if (window && window.DigiTrust == null) {
window.DigiTrust = fascade;
window.DigiTrust = facade;
}
}

/**
* Tests to see if a member ID is valid within facade
* @param {any} memberId
*/
var isMemberIdValid = function (memberId) {
if (memberId && memberId.length > 0) {
return true;
} else {
utils.logError('[DigiTrust Prebid Client Error] Missing member ID, add the member ID to the function call options');
return false;
}
};

/**
* Encapsulation of needed info for the callback return.
*
Expand Down Expand Up @@ -237,9 +269,9 @@ function getDigiTrustId(configParams) {
getDigiTrustId(configParams);
}, 100 * (1 + resultHandler.retries++));
return resultHandler.userIdCallback;
} else if (!isInitialized()) { // Second see if we should build a fascade object
} else if (!isInitialized()) { // Second see if we should build a facade object
if (resultHandler.retries >= MAX_RETRIES) {
initDigitrustFascade(configParams); // initialize a fascade object that relies on the AJAX call
initDigitrustFacade(configParams); // initialize a facade object that relies on the AJAX call
resultHandler.executeIdRequest(configParams);
} else {
// use expanding envelope
Expand All @@ -264,7 +296,7 @@ function initializeDigiTrust(config) {
dt.initialize(config.init, config.callback);
} else if (dt == null) {
// Assume we are already on a delay and DigiTrust is not on page
initDigitrustFascade(config);
initDigitrustFacade(config);
}
}

Expand All @@ -278,7 +310,7 @@ function surfaceTestHook() {
digitrustIdModule['_testHook'] = testHook;
}

testHook.initDigitrustFascade = initDigitrustFascade;
testHook.initDigitrustFacade = initDigitrustFacade;

/** @type {Submodule} */
export const digiTrustIdSubmodule = {
Expand Down