Skip to content

Commit 8cfb58a

Browse files
refactor: VuidManager to a standard class from singleton
1 parent 059db4c commit 8cfb58a

File tree

1 file changed

+46
-32
lines changed

1 file changed

+46
-32
lines changed

lib/plugins/vuid_manager/index.ts

Lines changed: 46 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,25 @@
1414
* limitations under the License.
1515
*/
1616

17+
import { LogHandler, LogLevel } from '../../modules/logging';
1718
import { VuidManagerOptions } from '../../shared_types';
1819
import { uuid } from '../../utils/fns';
1920
import PersistentKeyValueCache from '../key_value_cache/persistentKeyValueCache';
2021

2122
export interface IVuidManager {
22-
readonly vuid: string;
23+
readonly vuid: string | undefined;
2324
}
2425

2526
/**
2627
* Manager for creating, persisting, and retrieving a Visitor Unique Identifier
2728
*/
2829
export class VuidManager implements IVuidManager {
30+
/**
31+
* Handler for recording execution logs
32+
* @private
33+
*/
34+
private readonly logger: LogHandler;
35+
2936
/**
3037
* Prefix used as part of the VUID format
3138
* @public
@@ -44,48 +51,51 @@ export class VuidManager implements IVuidManager {
4451
* Current VUID value being used
4552
* @private
4653
*/
47-
private _vuid: string;
54+
private _vuid: string | undefined;
4855

4956
/**
5057
* Get the current VUID value being used
5158
*/
52-
get vuid(): string {
53-
return this._vuid;
54-
}
55-
56-
private readonly options: VuidManagerOptions;
59+
get vuid(): string | undefined {
60+
if (!this._vuid) {
61+
this.logger.log(LogLevel.ERROR, 'VUID is not initialized. Please call initialize() before accessing the VUID.');
62+
}
5763

58-
private constructor(options: VuidManagerOptions) {
59-
this._vuid = '';
60-
this.options = options;
64+
return this._vuid;
6165
}
6266

6367
/**
64-
* Instance of the VUID Manager
68+
* The cache used to store the VUID
6569
* @private
70+
* @readonly
6671
*/
67-
private static _instance: VuidManager;
72+
private readonly cache: PersistentKeyValueCache;
6873

6974
/**
70-
* Gets the current instance of the VUID Manager, initializing if needed
71-
* @param cache Caching mechanism to use for persisting the VUID outside working memory
72-
* @param options Options for the VUID Manager
73-
* @returns An instance of VuidManager
75+
* The initalization options for the VuidManager
76+
* @private
77+
* @readonly
7478
*/
75-
static async instance(cache: PersistentKeyValueCache, options: VuidManagerOptions): Promise<VuidManager> {
76-
if (!this._instance) {
77-
this._instance = new VuidManager(options);
79+
private readonly options: VuidManagerOptions;
7880

79-
if (!this._instance.options.enableVuid) {
80-
await cache.remove(this._instance._keyForVuid);
81-
}
82-
}
81+
constructor(cache: PersistentKeyValueCache, options: VuidManagerOptions, logger: LogHandler) {
82+
this.cache = cache;
83+
this.options = options;
84+
this.logger = logger;
85+
}
8386

84-
if (!this._instance._vuid) {
85-
await this._instance.load(cache);
87+
/**
88+
* Initialize the VuidManager
89+
* @returns Promise that resolves when the VuidManager is initialized
90+
*/
91+
async initialize(): Promise<void> {
92+
if (!this.options.enableVuid) {
93+
await this.cache.remove(this._keyForVuid);
8694
}
8795

88-
return this._instance;
96+
if (!this._vuid) {
97+
await this.load(this.cache);
98+
}
8999
}
90100

91101
/**
@@ -114,7 +124,7 @@ export class VuidManager implements IVuidManager {
114124
private makeVuid(): string {
115125
const maxLength = 32; // required by ODP server
116126

117-
// make sure UUIDv4 is used (not UUIDv1 or UUIDv6) since the trailing 5 chars will be truncated. See TDD for details.
127+
// make sure UUIDv4 is used (not UUIDv1 or UUIDv6) since the trailing 5 chars will be truncated.
118128
const uuidV4 = uuid();
119129
const formatted = uuidV4.replace(/-/g, '').toLowerCase();
120130
const vuidFull = `${VuidManager.vuid_prefix}${formatted}`;
@@ -132,12 +142,16 @@ export class VuidManager implements IVuidManager {
132142
await cache.set(this._keyForVuid, vuid);
133143
}
134144

135-
static isVuidEnabled(): boolean {
136-
return this._instance.options.enableVuid || false;
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;
137151
}
138152

139153
/**
140-
* Validates the format of a Visitor Unique Identifier
154+
* Validates the format of a Visitor Unique Identifier (VUID)
141155
* @param vuid VistorId to check
142156
* @returns *true* if the VisitorId is valid otherwise *false* for invalid
143157
*/
@@ -148,7 +162,7 @@ export class VuidManager implements IVuidManager {
148162
* **Important**: This should not to be used in production code
149163
* @private
150164
*/
151-
private static _reset(): void {
152-
this._instance._vuid = '';
165+
private _reset(): void {
166+
this._vuid = '';
153167
}
154168
}

0 commit comments

Comments
 (0)