Skip to content

Commit da2f766

Browse files
Merge pull request #94 from IABTechLab/ans-UID2-3834-allow-js-sdk-accept-multiple-init-calls
Ans UI d2 3834 allow js sdk accept multiple init calls
2 parents 0c10083 + 330e09b commit da2f766

8 files changed

+459
-57
lines changed

src/apiClient.ts

+4
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ export class ApiClient {
127127
return this._requestsInFlight.length > 0;
128128
}
129129

130+
public updateBaseUrl(newBaseUrl: string) {
131+
this._baseUrl = newBaseUrl;
132+
}
133+
130134
private ResponseToRefreshResult(
131135
response: UnvalidatedRefreshResponse | unknown
132136
): RefreshResult | string {

src/configManager.ts

+20-5
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,18 @@ export const loadConfig = (
3636
}
3737
};
3838

39-
export const removeConfig = (options: SdkOptions, productDetails: ProductDetails) => {
40-
if (options.useCookie) {
41-
removeConfigCookie(options, productDetails);
39+
export const updateConfig = (
40+
options: SdkOptions,
41+
productDetails: ProductDetails,
42+
previousOptions: SdkOptions
43+
) => {
44+
removeConfig(previousOptions, productDetails);
45+
storeConfig(options, productDetails);
46+
};
47+
48+
export const removeConfig = (previousOptions: SdkOptions, productDetails: ProductDetails) => {
49+
if (previousOptions.useCookie) {
50+
removeConfigCookie(previousOptions, productDetails);
4251
} else {
4352
removeConfigFromLocalStorage(productDetails);
4453
}
@@ -56,9 +65,15 @@ const setConfigCookie = (options: SdkOptions, productDetails: ProductDetails) =>
5665
document.cookie = cookie;
5766
};
5867

59-
const removeConfigCookie = (options: SdkOptions, productDetails: ProductDetails) => {
68+
const removeConfigCookie = (previousOptions: SdkOptions, productDetails: ProductDetails) => {
6069
document.cookie =
61-
productDetails.cookieName + '_config' + '=;expires=Tue, 1 Jan 1980 23:59:59 GMT;path=/';
70+
productDetails.cookieName +
71+
'_config' +
72+
'=;path=' +
73+
(previousOptions.cookiePath ?? '/') +
74+
';domain=' +
75+
(previousOptions.cookieDomain ?? '') +
76+
';expires=Tue, 1 Jan 1980 23:59:59 GMT';
6277
};
6378

6479
const getConfigCookie = (productDetails: ProductDetails) => {

src/cookieManager.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { isValidIdentity, Identity, OptoutIdentity, isOptoutIdentity } from './Identity';
2+
import { SdkOptions } from './sdkOptions';
23

34
export type CookieOptions = {
45
cookieDomain?: string;
@@ -52,8 +53,14 @@ export class CookieManager {
5253
}
5354
document.cookie = cookie;
5455
}
55-
public removeCookie() {
56-
document.cookie = this._cookieName + '=;expires=Tue, 1 Jan 1980 23:59:59 GMT';
56+
public removeCookie(previousOptions: SdkOptions) {
57+
document.cookie =
58+
this._cookieName +
59+
'=;path=' +
60+
(previousOptions.cookiePath ?? '/') +
61+
';domain=' +
62+
(previousOptions.cookieDomain ?? '') +
63+
';expires=Tue, 1 Jan 1980 23:59:59 GMT';
5764
}
5865
private getCookie() {
5966
const docCookie = document.cookie;

src/initCallbacks.ts

+34-19
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,39 @@ export enum IdentityStatus {
2121
OPTOUT = -4,
2222
}
2323

24-
export function notifyInitCallback(
25-
options: InitCallbackOptions,
26-
status: IdentityStatus,
27-
statusText: string,
28-
advertisingToken: string | undefined,
29-
logger: Logger
30-
) {
31-
if (options.callback) {
32-
const payload = {
33-
advertisingToken: advertisingToken,
34-
advertising_token: advertisingToken,
35-
status: status,
36-
statusText: statusText,
37-
};
38-
try {
39-
options.callback(payload);
40-
} catch (exception) {
41-
logger.warn('SDK init callback threw an exception', exception);
42-
}
24+
export class InitCallbackManager {
25+
private _initCallbacks: InitCallbackFunction[];
26+
27+
constructor(opts: InitCallbackOptions) {
28+
this._initCallbacks = opts.callback ? [opts.callback] : [];
29+
}
30+
31+
public addInitCallback(callback: InitCallbackFunction) {
32+
this._initCallbacks.push(callback);
33+
}
34+
35+
public getInitCallbacks() {
36+
return this._initCallbacks;
37+
}
38+
39+
public notifyInitCallbacks(
40+
status: IdentityStatus,
41+
statusText: string,
42+
advertisingToken: string | undefined,
43+
logger: Logger
44+
) {
45+
this._initCallbacks.forEach((initCallback) => {
46+
const payload = {
47+
advertisingToken: advertisingToken,
48+
advertising_token: advertisingToken,
49+
status: status,
50+
statusText: statusText,
51+
};
52+
try {
53+
initCallback(payload);
54+
} catch (exception) {
55+
logger.warn('SDK init callback threw an exception', exception);
56+
}
57+
});
4358
}
4459
}

src/integrationTests/basic.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,9 @@ testCookieAndLocalStorage(() => {
134134
});
135135
});
136136

137-
test('init() should fail if called multiple times', () => {
138-
uid2.init({ callback: () => {} });
139-
expect(() => uid2.init({ callback: () => {} })).toThrow();
137+
test('init() should not fail if called multiple times', () => {
138+
uid2.init({ callback: () => {}, identity: makeIdentityV2() });
139+
expect(() => uid2.init({ callback: () => {} })).not.toThrow();
140140
});
141141

142142
describe('when initialised without identity', () => {

0 commit comments

Comments
 (0)