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

new no identity available event #184

Merged
merged 28 commits into from
Feb 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
a34660a
first examples of no identity available
ashleysmithTTD Feb 13, 2025
8460640
added more tests where identity could be unavailable
ashleysmithTTD Feb 13, 2025
bacfbf6
no identity available on set identity
ashleysmithTTD Feb 13, 2025
59856db
more set identity examples
ashleysmithTTD Feb 13, 2025
f8b39dd
no identity available new event
ashleysmithTTD Feb 14, 2025
d9b53b0
callback changes no need to init
ashleysmithTTD Feb 14, 2025
d549c07
Merge branch 'main' into ans-UID2-2295-no-identity-available-event
ashleysmithTTD Feb 14, 2025
d8cc05d
not needed from prev branch
ashleysmithTTD Feb 14, 2025
75b4952
wrote separate identity function for callback instead
ashleysmithTTD Feb 14, 2025
ea6f0e4
added tests for cstg and refresh timer
ashleysmithTTD Feb 14, 2025
1879ddc
added test for local storage
ashleysmithTTD Feb 14, 2025
f0d0bed
added test without init
ashleysmithTTD Feb 14, 2025
b991a98
identity should always be null
ashleysmithTTD Feb 14, 2025
7aa9c23
organizing tests
ashleysmithTTD Feb 14, 2025
dc38026
added cookie example also
ashleysmithTTD Feb 14, 2025
2a7f821
cleaned up valid identity test
ashleysmithTTD Feb 14, 2025
50d0cea
added sdk loaded check in run callbacks
ashleysmithTTD Feb 18, 2025
3c0a13a
removed identity forced to be null
ashleysmithTTD Feb 18, 2025
ccd68ae
fixed tests to expect expired identity sometimes
ashleysmithTTD Feb 18, 2025
6fcb0d3
cleaned up get identity to be more consistent with isvalididentity fu…
ashleysmithTTD Feb 18, 2025
6bea2a5
the check iwll already happen in islogin required
ashleysmithTTD Feb 18, 2025
c16234b
unavailable check still needed
ashleysmithTTD Feb 19, 2025
f1d89c5
removed any no identity available event reference
ashleysmithTTD Feb 25, 2025
12ae483
dont need separate get identity functions anymore
ashleysmithTTD Feb 25, 2025
1c4167c
no need to call is identity available everywhere
ashleysmithTTD Feb 25, 2025
4053cb7
clean up get identity
ashleysmithTTD Feb 25, 2025
c4061fe
get identity return
ashleysmithTTD Feb 25, 2025
86501e8
fixed is identity available
ashleysmithTTD Feb 25, 2025
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
3 changes: 2 additions & 1 deletion src/callbackManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ export class CallbackManager {
public runCallbacks(event: EventType, payload: CallbackPayload) {
if (event === EventType.InitCompleted) this._sentInit = true;
if (event === EventType.SdkLoaded) CallbackManager._sentSdkLoaded[this._productName] = true;
if (!this._sentInit && event !== EventType.SdkLoaded) return;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With everything that can be done without init, I dont think it makes sense anymore to not run callbacks if init is not complete

// if SDK has not been loaded yet, callbacks should not run
if (!CallbackManager._sentSdkLoaded[this._productName]) return;

const enrichedPayload = {
...payload,
Expand Down
2 changes: 1 addition & 1 deletion src/integrationTests/options.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { afterEach, beforeEach, describe, expect, jest, test } from '@jest/globals';

import * as mocks from '../mocks';
import { SdkOptions, sdkWindow, UID2 } from '../uid2Sdk';
import { EventType, SdkOptions, sdkWindow, UID2 } from '../uid2Sdk';
import { loadConfig, removeConfig } from '../configManager';
import { ProductDetails } from '../product';

Expand Down
38 changes: 24 additions & 14 deletions src/sdkBase.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { version } from '../package.json';
import { OptoutIdentity, Identity, isOptoutIdentity } from './Identity';
import { OptoutIdentity, Identity, isOptoutIdentity, isValidIdentity } from './Identity';
import { IdentityStatus, InitCallbackManager } from './initCallbacks';
import { SdkOptions, isSDKOptionsOrThrow } from './sdkOptions';
import { Logger, MakeLogger } from './sdk/logger';
Expand Down Expand Up @@ -163,9 +163,8 @@ export abstract class SdkBase {

public getIdentity(): Identity | null {
const identity = this._identity ?? this.getIdentityNoInit();
return identity && !this.temporarilyUnavailable(identity) && !isOptoutIdentity(identity)
? identity
: null;
// if identity is valid (includes not opted out) and available, return it
return isValidIdentity(identity) && !this.temporarilyUnavailable(identity) ? identity : null;
}

// When the SDK has been initialized, this function should return the token
Expand All @@ -181,11 +180,23 @@ export abstract class SdkBase {
}

public isLoginRequired() {
const identity = this._identity ?? this.getIdentityNoInit();
// if identity temporarily unavailable, login is not required
if (this.temporarilyUnavailable(identity)) {
return false;
}
return !this.isIdentityAvailable();
}

public isIdentityAvailable() {
return this.isIdentityValid() || this._apiClient?.hasActiveRequests();
const identity = this._identity ?? this.getIdentityNoInit();
// available if identity exists and has not expired or if there active requests
return (
(identity &&
!hasExpired(identity.refresh_expires) &&
!this.temporarilyUnavailable(identity)) ||
this._apiClient?.hasActiveRequests()
);
}

public hasOptedOut() {
Expand Down Expand Up @@ -230,10 +241,14 @@ export abstract class SdkBase {
this._initCallbackManager?.addInitCallback(opts.callback);
}

const useNewIdentity =
opts.identity &&
(!previousOpts.identity ||
opts.identity.identity_expires > previousOpts.identity.identity_expires);
let useNewIdentity;
if (!opts.identity) useNewIdentity = true;
else {
useNewIdentity =
!previousOpts.identity ||
opts.identity.identity_expires > previousOpts.identity.identity_expires;
}

Copy link
Contributor Author

@ashleysmithTTD ashleysmithTTD Feb 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

found in testing that we didnt have the possibility that opts.identity just doesn't exist, which would automatically make useNewIdentity true

if (useNewIdentity || opts.callback) {
let identity = useNewIdentity ? opts.identity : previousOpts.identity ?? null;
if (identity) {
Expand Down Expand Up @@ -280,11 +295,6 @@ export abstract class SdkBase {
if (this.hasOptedOut()) this._callbackManager.runCallbacks(EventType.OptoutReceived, {});
}

private isIdentityValid() {
const identity = this._identity ?? this.getIdentityNoInit();
return identity && !hasExpired(identity.refresh_expires);
}

private temporarilyUnavailable(identity: Identity | OptoutIdentity | null | undefined) {
if (!identity && this._apiClient?.hasActiveRequests()) return true;
// returns true if identity is expired but refreshable
Expand Down
Loading