Skip to content

Commit 8aee289

Browse files
fix: code to pass tests
1 parent 005936b commit 8aee289

File tree

3 files changed

+23
-27
lines changed

3 files changed

+23
-27
lines changed

lib/core/odp/odp_manager.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import { OptimizelySegmentOption } from './optimizely_segment_option';
2727
import { invalidOdpDataFound } from './odp_utils';
2828
import { OdpEvent } from './odp_event';
2929
import { resolvablePromise, ResolvablePromise } from '../../utils/promise/resolvablePromise';
30+
import { OdpOptions } from '../../shared_types';
3031

3132
/**
3233
* Manager for handling internal all business logic related to
@@ -97,22 +98,29 @@ export abstract class OdpManager implements IOdpManager {
9798
*/
9899
odpIntegrationConfig?: OdpIntegrationConfig;
99100

101+
/**
102+
* ODP initialization options
103+
*/
104+
protected odpOptions?: OdpOptions;
105+
100106
// TODO: Consider accepting logger as a parameter and initializing it in constructor instead
101107
constructor({
102108
odpIntegrationConfig,
103109
segmentManager,
104110
eventManager,
105111
logger,
112+
odpOptions,
106113
}: {
107114
odpIntegrationConfig?: OdpIntegrationConfig;
108115
segmentManager: IOdpSegmentManager;
109116
eventManager: IOdpEventManager;
110117
logger: LogHandler;
118+
odpOptions?: OdpOptions;
111119
}) {
112120
this.segmentManager = segmentManager;
113121
this.eventManager = eventManager;
114122
this.logger = logger;
115-
123+
this.odpOptions = odpOptions;
116124
this.configPromise = resolvablePromise();
117125

118126
const readinessDependencies: PromiseLike<unknown>[] = [this.configPromise];

lib/plugins/odp_manager/index.browser.ts

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,14 @@ interface BrowserOdpManagerConfig {
5151
// Client-side Browser Plugin for ODP Manager
5252
export class BrowserOdpManager extends OdpManager {
5353
static cache = new BrowserAsyncStorageCache();
54-
vuid?: string;
55-
private static shouldUseVuid = false;
54+
vuid?: string = "";
5655

5756
constructor(options: {
5857
odpIntegrationConfig?: OdpIntegrationConfig;
5958
segmentManager: IOdpSegmentManager;
6059
eventManager: IOdpEventManager;
6160
logger: LogHandler;
61+
odpOptions?: OdpOptions;
6262
}) {
6363
super(options);
6464
}
@@ -129,13 +129,12 @@ export class BrowserOdpManager extends OdpManager {
129129
});
130130
}
131131

132-
this.shouldUseVuid = odpOptions?.enableVuid || false;
133-
134132
return new BrowserOdpManager({
135133
odpIntegrationConfig,
136134
segmentManager,
137135
eventManager,
138136
logger,
137+
odpOptions,
139138
});
140139
}
141140

@@ -144,14 +143,7 @@ export class BrowserOdpManager extends OdpManager {
144143
* accesses or creates new VUID from Browser cache
145144
*/
146145
protected async initializeVuid(): Promise<void> {
147-
const vuidManager = await VuidManager.instance(BrowserOdpManager.cache);
148-
149-
if (!this.isVuidEnabled()) {
150-
await vuidManager.remove(BrowserOdpManager.cache);
151-
// assign default empty string VUID from VuidManager instead of
152-
// early return here.
153-
}
154-
146+
const vuidManager = await VuidManager.instance(BrowserOdpManager.cache, this.odpOptions);
155147
this.vuid = vuidManager.vuid;
156148
}
157149

@@ -197,7 +189,7 @@ export class BrowserOdpManager extends OdpManager {
197189
}
198190

199191
isVuidEnabled(): boolean {
200-
return BrowserOdpManager.shouldUseVuid;
192+
return this.odpOptions?.enableVuid || false;
201193
}
202194

203195
getVuid(): string | undefined {

lib/plugins/vuid_manager/index.ts

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17+
import { OdpOptions } from '../../shared_types';
1718
import { uuid } from '../../utils/fns';
1819
import PersistentKeyValueCache from '../key_value_cache/persistentKeyValueCache';
1920

@@ -43,7 +44,7 @@ export class VuidManager implements IVuidManager {
4344
* Current VUID value being used
4445
* @private
4546
*/
46-
private _vuid: string;
47+
private _vuid = '';
4748

4849
/**
4950
* Get the current VUID value being used
@@ -52,9 +53,7 @@ export class VuidManager implements IVuidManager {
5253
return this._vuid;
5354
}
5455

55-
private constructor() {
56-
this._vuid = '';
57-
}
56+
private constructor() { }
5857

5958
/**
6059
* Instance of the VUID Manager
@@ -67,11 +66,16 @@ export class VuidManager implements IVuidManager {
6766
* @param cache Caching mechanism to use for persisting the VUID outside working memory *
6867
* @returns An instance of VuidManager
6968
*/
70-
static async instance(cache: PersistentKeyValueCache): Promise<VuidManager> {
69+
static async instance(cache: PersistentKeyValueCache, options?: OdpOptions): Promise<VuidManager> {
7170
if (!this._instance) {
7271
this._instance = new VuidManager();
7372
}
7473

74+
if (!options?.enableVuid) {
75+
cache.remove(this._instance._keyForVuid);
76+
return this._instance;
77+
}
78+
7579
if (!this._instance._vuid) {
7680
await this._instance.load(cache);
7781
}
@@ -123,14 +127,6 @@ export class VuidManager implements IVuidManager {
123127
await cache.set(this._keyForVuid, vuid);
124128
}
125129

126-
/**
127-
* Removes the VUID from the cache
128-
* @param cache Caching mechanism to use for persisting the VUID outside working memory
129-
*/
130-
async remove(cache: PersistentKeyValueCache): Promise<void> {
131-
await cache.remove(this._keyForVuid);
132-
}
133-
134130
/**
135131
* Validates the format of a Visitor Unique Identifier
136132
* @param vuid VistorId to check

0 commit comments

Comments
 (0)