Skip to content

Commit

Permalink
Merge pull request #94 from IABTechLab/ans-UID2-3834-allow-js-sdk-acc…
Browse files Browse the repository at this point in the history
…ept-multiple-init-calls

Ans UI d2 3834 allow js sdk accept multiple init calls
  • Loading branch information
ashleysmithTTD authored Aug 22, 2024
2 parents 0c10083 + 330e09b commit da2f766
Show file tree
Hide file tree
Showing 8 changed files with 459 additions and 57 deletions.
4 changes: 4 additions & 0 deletions src/apiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ export class ApiClient {
return this._requestsInFlight.length > 0;
}

public updateBaseUrl(newBaseUrl: string) {
this._baseUrl = newBaseUrl;
}

private ResponseToRefreshResult(
response: UnvalidatedRefreshResponse | unknown
): RefreshResult | string {
Expand Down
25 changes: 20 additions & 5 deletions src/configManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,18 @@ export const loadConfig = (
}
};

export const removeConfig = (options: SdkOptions, productDetails: ProductDetails) => {
if (options.useCookie) {
removeConfigCookie(options, productDetails);
export const updateConfig = (
options: SdkOptions,
productDetails: ProductDetails,
previousOptions: SdkOptions
) => {
removeConfig(previousOptions, productDetails);
storeConfig(options, productDetails);
};

export const removeConfig = (previousOptions: SdkOptions, productDetails: ProductDetails) => {
if (previousOptions.useCookie) {
removeConfigCookie(previousOptions, productDetails);
} else {
removeConfigFromLocalStorage(productDetails);
}
Expand All @@ -56,9 +65,15 @@ const setConfigCookie = (options: SdkOptions, productDetails: ProductDetails) =>
document.cookie = cookie;
};

const removeConfigCookie = (options: SdkOptions, productDetails: ProductDetails) => {
const removeConfigCookie = (previousOptions: SdkOptions, productDetails: ProductDetails) => {
document.cookie =
productDetails.cookieName + '_config' + '=;expires=Tue, 1 Jan 1980 23:59:59 GMT;path=/';
productDetails.cookieName +
'_config' +
'=;path=' +
(previousOptions.cookiePath ?? '/') +
';domain=' +
(previousOptions.cookieDomain ?? '') +
';expires=Tue, 1 Jan 1980 23:59:59 GMT';
};

const getConfigCookie = (productDetails: ProductDetails) => {
Expand Down
11 changes: 9 additions & 2 deletions src/cookieManager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { isValidIdentity, Identity, OptoutIdentity, isOptoutIdentity } from './Identity';
import { SdkOptions } from './sdkOptions';

export type CookieOptions = {
cookieDomain?: string;
Expand Down Expand Up @@ -52,8 +53,14 @@ export class CookieManager {
}
document.cookie = cookie;
}
public removeCookie() {
document.cookie = this._cookieName + '=;expires=Tue, 1 Jan 1980 23:59:59 GMT';
public removeCookie(previousOptions: SdkOptions) {
document.cookie =
this._cookieName +
'=;path=' +
(previousOptions.cookiePath ?? '/') +
';domain=' +
(previousOptions.cookieDomain ?? '') +
';expires=Tue, 1 Jan 1980 23:59:59 GMT';
}
private getCookie() {
const docCookie = document.cookie;
Expand Down
53 changes: 34 additions & 19 deletions src/initCallbacks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,39 @@ export enum IdentityStatus {
OPTOUT = -4,
}

export function notifyInitCallback(
options: InitCallbackOptions,
status: IdentityStatus,
statusText: string,
advertisingToken: string | undefined,
logger: Logger
) {
if (options.callback) {
const payload = {
advertisingToken: advertisingToken,
advertising_token: advertisingToken,
status: status,
statusText: statusText,
};
try {
options.callback(payload);
} catch (exception) {
logger.warn('SDK init callback threw an exception', exception);
}
export class InitCallbackManager {
private _initCallbacks: InitCallbackFunction[];

constructor(opts: InitCallbackOptions) {
this._initCallbacks = opts.callback ? [opts.callback] : [];
}

public addInitCallback(callback: InitCallbackFunction) {
this._initCallbacks.push(callback);
}

public getInitCallbacks() {
return this._initCallbacks;
}

public notifyInitCallbacks(
status: IdentityStatus,
statusText: string,
advertisingToken: string | undefined,
logger: Logger
) {
this._initCallbacks.forEach((initCallback) => {
const payload = {
advertisingToken: advertisingToken,
advertising_token: advertisingToken,
status: status,
statusText: statusText,
};
try {
initCallback(payload);
} catch (exception) {
logger.warn('SDK init callback threw an exception', exception);
}
});
}
}
6 changes: 3 additions & 3 deletions src/integrationTests/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,9 @@ testCookieAndLocalStorage(() => {
});
});

test('init() should fail if called multiple times', () => {
uid2.init({ callback: () => {} });
expect(() => uid2.init({ callback: () => {} })).toThrow();
test('init() should not fail if called multiple times', () => {
uid2.init({ callback: () => {}, identity: makeIdentityV2() });
expect(() => uid2.init({ callback: () => {} })).not.toThrow();
});

describe('when initialised without identity', () => {
Expand Down
Loading

0 comments on commit da2f766

Please sign in to comment.