Skip to content

Commit e84d353

Browse files
refactor: WIP init VuidManager from opti client
1 parent dd1b434 commit e84d353

File tree

4 files changed

+41
-29
lines changed

4 files changed

+41
-29
lines changed

lib/index.browser.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ let hasRetriedEvents = false;
5353
* @return {Client|null} the Optimizely client object
5454
* null on error
5555
*/
56-
// TODO: @raju-opti I'm not sure how to handle async VuidManager.instance() making createInstance async
57-
const createInstance = async function(config: Config): Promise<Client | null> {
56+
const createInstance = function (config: Config): Client | null {
5857
try {
5958
// TODO warn about setting per instance errorHandler / logger / logLevel
6059
let isValidInstance = false;
@@ -154,7 +153,7 @@ const createInstance = async function(config: Config): Promise<Client | null> {
154153
isValidInstance,
155154
odpManager: odpExplicitlyOff ? undefined
156155
: BrowserOdpManager.createInstance({ logger, odpOptions: config.odpOptions, clientEngine, clientVersion }),
157-
vuidManager: await VuidManager.instance(cache, vuidManagerOptions),
156+
vuidManager: new VuidManager(cache, vuidManagerOptions, logger),
158157
};
159158

160159
const optimizely = new Optimizely(optimizelyOptions);
@@ -183,7 +182,7 @@ const createInstance = async function(config: Config): Promise<Client | null> {
183182
}
184183
};
185184

186-
const __internalResetRetryState = function(): void {
185+
const __internalResetRetryState = function (): void {
187186
hasRetriedEvents = false;
188187
};
189188

lib/index.react_native.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ const DEFAULT_EVENT_MAX_QUEUE_SIZE = 10000;
4747
* @return {Client|null} the Optimizely client object
4848
* null on error
4949
*/
50-
const createInstance = async function(config: Config): Promise<Client | null> {
50+
const createInstance = function (config: Config): Client | null {
5151
try {
5252
// TODO warn about setting per instance errorHandler / logger / logLevel
5353
let isValidInstance = false;
@@ -108,7 +108,7 @@ const createInstance = async function(config: Config): Promise<Client | null> {
108108
}
109109

110110
const { clientEngine, clientVersion } = config;
111-
111+
112112
const cache = new BrowserAsyncStorageCache();
113113
const vuidManagerOptions: VuidManagerOptions = {
114114
enableVuid: config.vuidManagerOptions?.enableVuid || false,
@@ -132,8 +132,8 @@ const createInstance = async function(config: Config): Promise<Client | null> {
132132
notificationCenter,
133133
isValidInstance: isValidInstance,
134134
odpManager: odpExplicitlyOff ? undefined
135-
:BrowserOdpManager.createInstance({ logger, odpOptions: config.odpOptions, clientEngine, clientVersion }),
136-
vuidManager: await VuidManager.instance(cache, vuidManagerOptions),
135+
: BrowserOdpManager.createInstance({ logger, odpOptions: config.odpOptions, clientEngine, clientVersion }),
136+
vuidManager: new VuidManager(cache, vuidManagerOptions, logger),
137137
};
138138

139139
// If client engine is react, convert it to react native.

lib/optimizely/index.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ export default class Optimizely implements Client {
180180
projectConfigManagerReadyPromise,
181181
eventProcessorStartedPromise,
182182
config.odpManager ? config.odpManager.onReady() : Promise.resolve(),
183+
this.vuidManager?.vuidEnabled ? this.vuidManager?.initialize() : Promise.resolve(),
183184
]).then(promiseResults => {
184185
// Only return status from project config promise because event processor promise does not return any status.
185186
return promiseResults[0];
@@ -1413,8 +1414,8 @@ export default class Optimizely implements Client {
14131414
};
14141415

14151416
this.readyTimeouts[timeoutId] = {
1416-
readyTimeout: readyTimeout,
1417-
onClose: onClose,
1417+
readyTimeout,
1418+
onClose,
14181419
};
14191420

14201421
this.readyPromise.then(() => {
@@ -1423,13 +1424,12 @@ export default class Optimizely implements Client {
14231424
resolveTimeoutPromise({
14241425
success: true,
14251426
});
1427+
this.vuidManager?.registerUser();
14261428
});
14271429

14281430
return Promise.race([this.readyPromise, timeoutPromise]);
14291431
}
14301432

1431-
//============ decide ============//
1432-
14331433
/**
14341434
* Creates a context of the user for which decision APIs will be called.
14351435
*
@@ -1445,6 +1445,7 @@ export default class Optimizely implements Client {
14451445
const userIdentifier = userId ?? this.vuidManager?.vuid;
14461446

14471447
if (userIdentifier === undefined || !this.validateInputs({ user_id: userIdentifier }, attributes)) {
1448+
this.logger.log(LOG_LEVEL.ERROR, '%s: Valid User ID or VUID not provided. User context not created.', MODULE_NAME);
14481449
return null;
14491450
}
14501451

@@ -1759,7 +1760,7 @@ export default class Optimizely implements Client {
17591760
* @returns {string|undefined} Currently provisioned VUID from local ODP Manager or undefined
17601761
*/
17611762
public getVuid(): string | undefined {
1762-
if (!this.vuidManager?.isVuidEnabled()) {
1763+
if (!this.vuidManager?.vuidEnabled) {
17631764
this.logger.log(LOG_LEVEL.WARNING, 'getVuid() unavailable for this platform or was not explicitly enabled.', MODULE_NAME);
17641765
return undefined;
17651766
}

lib/plugins/vuid_manager/index.ts

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,21 @@ import { uuid } from '../../utils/fns';
2020
import PersistentKeyValueCache from '../key_value_cache/persistentKeyValueCache';
2121

2222
export interface IVuidManager {
23+
/**
24+
* Current VUID value being used
25+
* @returns Current VUID stored in the VuidManager
26+
*/
2327
readonly vuid: string | undefined;
28+
/**
29+
* Indicates whether the VUID use is enabled
30+
* @returns *true* if the VUID use is enabled otherwise *false*
31+
*/
32+
readonly vuidEnabled: boolean;
33+
/**
34+
* Initialize the VuidManager
35+
* @returns Promise that resolves when the VuidManager is initialized
36+
*/
37+
initialize(): Promise<void>;
2438
}
2539

2640
/**
@@ -65,22 +79,28 @@ export class VuidManager implements IVuidManager {
6579
}
6680

6781
/**
68-
* The cache used to store the VUID
69-
* @private
70-
* @readonly
82+
* Current state of the VUID use
83+
* @private
84+
*/
85+
private _vuidEnabled = false;
86+
87+
/**
88+
* Indicates whether the VUID use is enabled
7189
*/
72-
private readonly cache: PersistentKeyValueCache;
90+
get vuidEnabled(): boolean {
91+
return this._vuidEnabled;
92+
}
7393

7494
/**
75-
* The initalization options for the VuidManager
95+
* The cache used to store the VUID
7696
* @private
7797
* @readonly
7898
*/
79-
private readonly options: VuidManagerOptions;
99+
private readonly cache: PersistentKeyValueCache;
80100

81101
constructor(cache: PersistentKeyValueCache, options: VuidManagerOptions, logger: LogHandler) {
82102
this.cache = cache;
83-
this.options = options;
103+
this._vuidEnabled = options.enableVuid;
84104
this.logger = logger;
85105
}
86106

@@ -89,7 +109,7 @@ export class VuidManager implements IVuidManager {
89109
* @returns Promise that resolves when the VuidManager is initialized
90110
*/
91111
async initialize(): Promise<void> {
92-
if (!this.options.enableVuid) {
112+
if (!this.vuidEnabled) {
93113
await this.cache.remove(this._keyForVuid);
94114
}
95115

@@ -142,14 +162,6 @@ export class VuidManager implements IVuidManager {
142162
await cache.set(this._keyForVuid, vuid);
143163
}
144164

145-
/**
146-
* Indicates whether the VUID use is enabled
147-
* @returns *true* if enabled otherwise *false* for disabled
148-
*/
149-
isVuidEnabled(): boolean {
150-
return this.options.enableVuid || false;
151-
}
152-
153165
/**
154166
* Validates the format of a Visitor Unique Identifier (VUID)
155167
* @param vuid VistorId to check

0 commit comments

Comments
 (0)