14
14
* limitations under the License.
15
15
*/
16
16
17
+ import { LogHandler , LogLevel } from '../../modules/logging' ;
17
18
import { VuidManagerOptions } from '../../shared_types' ;
18
19
import { uuid } from '../../utils/fns' ;
19
20
import PersistentKeyValueCache from '../key_value_cache/persistentKeyValueCache' ;
20
21
21
22
export interface IVuidManager {
22
- readonly vuid : string ;
23
+ readonly vuid : string | undefined ;
23
24
}
24
25
25
26
/**
26
27
* Manager for creating, persisting, and retrieving a Visitor Unique Identifier
27
28
*/
28
29
export class VuidManager implements IVuidManager {
30
+ /**
31
+ * Handler for recording execution logs
32
+ * @private
33
+ */
34
+ private readonly logger : LogHandler ;
35
+
29
36
/**
30
37
* Prefix used as part of the VUID format
31
38
* @public
@@ -44,48 +51,51 @@ export class VuidManager implements IVuidManager {
44
51
* Current VUID value being used
45
52
* @private
46
53
*/
47
- private _vuid : string ;
54
+ private _vuid : string | undefined ;
48
55
49
56
/**
50
57
* Get the current VUID value being used
51
58
*/
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
+ }
57
63
58
- private constructor ( options : VuidManagerOptions ) {
59
- this . _vuid = '' ;
60
- this . options = options ;
64
+ return this . _vuid ;
61
65
}
62
66
63
67
/**
64
- * Instance of the VUID Manager
68
+ * The cache used to store the VUID
65
69
* @private
70
+ * @readonly
66
71
*/
67
- private static _instance : VuidManager ;
72
+ private readonly cache : PersistentKeyValueCache ;
68
73
69
74
/**
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
74
78
*/
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 ;
78
80
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
+ }
83
86
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 ) ;
86
94
}
87
95
88
- return this . _instance ;
96
+ if ( ! this . _vuid ) {
97
+ await this . load ( this . cache ) ;
98
+ }
89
99
}
90
100
91
101
/**
@@ -114,7 +124,7 @@ export class VuidManager implements IVuidManager {
114
124
private makeVuid ( ) : string {
115
125
const maxLength = 32 ; // required by ODP server
116
126
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.
118
128
const uuidV4 = uuid ( ) ;
119
129
const formatted = uuidV4 . replace ( / - / g, '' ) . toLowerCase ( ) ;
120
130
const vuidFull = `${ VuidManager . vuid_prefix } ${ formatted } ` ;
@@ -132,12 +142,16 @@ export class VuidManager implements IVuidManager {
132
142
await cache . set ( this . _keyForVuid , vuid ) ;
133
143
}
134
144
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 ;
137
151
}
138
152
139
153
/**
140
- * Validates the format of a Visitor Unique Identifier
154
+ * Validates the format of a Visitor Unique Identifier (VUID)
141
155
* @param vuid VistorId to check
142
156
* @returns *true* if the VisitorId is valid otherwise *false* for invalid
143
157
*/
@@ -148,7 +162,7 @@ export class VuidManager implements IVuidManager {
148
162
* **Important**: This should not to be used in production code
149
163
* @private
150
164
*/
151
- private static _reset ( ) : void {
152
- this . _instance . _vuid = '' ;
165
+ private _reset ( ) : void {
166
+ this . _vuid = '' ;
153
167
}
154
168
}
0 commit comments