Skip to content

Commit

Permalink
Merge pull request #96 from IABTechLab/sas-UID2-3837-allow-include-js…
Browse files Browse the repository at this point in the history
…-sdk-multiple-times

allow inclusion of sdk script multiple times
  • Loading branch information
ssundahlTTD authored Aug 21, 2024
2 parents 40616e0 + 99344bc commit 0c10083
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 3 deletions.
2 changes: 1 addition & 1 deletion examples/cstg/html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ <h1>UID2 Publisher Client-Side Integration Example using UID2 JavaScript SDK</h1
name="email"
placeholder="Enter an email address"
style="border-style: none"
value="test@example.com"
value="testing@example.com"
/>
</div>
<div><button type="button" class="button" id="login">Log In</button>></div>
Expand Down
5 changes: 5 additions & 0 deletions src/euidSdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ export function assertEUID(sdk: typeof window.__euid): asserts sdk is EUID {
}

export function __euidInternalHandleScriptLoad() {
if (window.__euid && 'init' in window.__euid) {
// This has already been run
return;
}

const callbacks = window?.__euid?.callbacks || [];
const callbackContainer: CallbackContainer = {};
window.__euid = new EUID(callbacks, callbackContainer);
Expand Down
51 changes: 49 additions & 2 deletions src/integrationTests/basic.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { afterEach, beforeEach, describe, expect, jest, test } from '@jest/globals';

import * as mocks from '../mocks';
import { sdkWindow, UID2 } from '../uid2Sdk';
import { EventType } from '../callbackManager';
import { __uid2InternalHandleScriptLoad, sdkWindow, UID2 } from '../uid2Sdk';
import { CallbackHandler, EventType } from '../callbackManager';

let callback: any;
let uid2: UID2;
Expand Down Expand Up @@ -1014,3 +1014,50 @@ testCookieAndLocalStorage(() => {
});
});
});

describe('Include sdk script multiple times', () => {
const makeIdentity = mocks.makeIdentityV2;
let asyncCallback: jest.Mock<CallbackHandler>;
const debugOutput = false;

asyncCallback = jest.fn((event, payload) => {
if (debugOutput) {
console.log('Async Callback Event:', event);
console.log('Payload:', payload);
}
});

beforeEach(() => {
sdkWindow.__uid2 = new UID2();
});

afterEach(() => {
sdkWindow.__uid2 = undefined;
});

test('call init after script inclusion', async () => {
const identity = { ...makeIdentity(), refresh_from: Date.now() + 100 };

__uid2InternalHandleScriptLoad();
__uid2InternalHandleScriptLoad();
__uid2InternalHandleScriptLoad();

(sdkWindow.__uid2 as UID2).init({ identity });

sdkWindow.__uid2!.callbacks!.push(asyncCallback);
expect(asyncCallback).toBeCalledWith(EventType.SdkLoaded, expect.anything());
});

test('call init prior to script inclusion', async () => {
const identity = { ...makeIdentity(), refresh_from: Date.now() + 100 };

(sdkWindow.__uid2 as UID2).init({ identity });

__uid2InternalHandleScriptLoad();
__uid2InternalHandleScriptLoad();
__uid2InternalHandleScriptLoad();

sdkWindow.__uid2!.callbacks!.push(asyncCallback);
expect(asyncCallback).toBeCalledWith(EventType.SdkLoaded, expect.anything());
});
});
4 changes: 4 additions & 0 deletions src/sdkBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ export abstract class SdkBase {
return this._tokenPromiseHandler.createMaybeDeferredPromise(token ?? null);
}

public initComplete(): boolean {
return this._initComplete;
}

/**
* Deprecated
*/
Expand Down
5 changes: 5 additions & 0 deletions src/uid2Sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ export function assertUID2(sdk: typeof window.__uid2): asserts sdk is UID2 {
}

export function __uid2InternalHandleScriptLoad() {
if (window.__uid2 && 'init' in window.__uid2) {
// This has already been run
return;
}

const callbacks = window?.__uid2?.callbacks || [];
const callbackContainer: CallbackContainer = {};
window.__uid2 = new UID2(callbacks, callbackContainer);
Expand Down

0 comments on commit 0c10083

Please sign in to comment.