Skip to content

Commit ac00afc

Browse files
authored
[FSSDK-9362] chore: odp cleanup + adjust odpmanager initpromise implementation (#827)
1 parent b5e6d99 commit ac00afc

File tree

19 files changed

+158
-97
lines changed

19 files changed

+158
-97
lines changed

packages/optimizely-sdk/lib/core/notification_center/notification_registry.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,7 @@ export class NotificationRegistry {
3131
* @param logger Logger to be used for the corresponding notification center
3232
* @returns {NotificationCenter | undefined} a notification center instance for ODP Manager if a valid SDK Key is provided, otherwise undefined
3333
*/
34-
public static getNotificationCenter(
35-
sdkKey?: string,
36-
logger: LogHandler = getLogger()
37-
): NotificationCenter | undefined {
34+
static getNotificationCenter(sdkKey?: string, logger: LogHandler = getLogger()): NotificationCenter | undefined {
3835
if (!sdkKey) {
3936
logger.log(LogLevel.ERROR, 'No SDK key provided to getNotificationCenter.');
4037
return undefined;
@@ -54,7 +51,7 @@ export class NotificationRegistry {
5451
return notificationCenter;
5552
}
5653

57-
public static removeNotificationCenter(sdkKey?: string): void {
54+
static removeNotificationCenter(sdkKey?: string): void {
5855
if (!sdkKey) {
5956
return;
6057
}

packages/optimizely-sdk/lib/core/odp/index.ts

Lines changed: 0 additions & 15 deletions
This file was deleted.

packages/optimizely-sdk/lib/core/odp/odp_config.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export class OdpConfig {
2727
* Getter to retrieve the ODP server host
2828
* @public
2929
*/
30-
public get apiHost(): string {
30+
get apiHost(): string {
3131
return this._apiHost;
3232
}
3333

@@ -41,7 +41,7 @@ export class OdpConfig {
4141
* Getter to retrieve the ODP API key
4242
* @public
4343
*/
44-
public get apiKey(): string {
44+
get apiKey(): string {
4545
return this._apiKey;
4646
}
4747

@@ -55,7 +55,7 @@ export class OdpConfig {
5555
* Getter for ODP segments to check
5656
* @public
5757
*/
58-
public get segmentsToCheck(): string[] {
58+
get segmentsToCheck(): string[] {
5959
return this._segmentsToCheck;
6060
}
6161

@@ -70,7 +70,7 @@ export class OdpConfig {
7070
* @param {OdpConfig} config New ODP Config to potentially update self with
7171
* @returns true if configuration was updated successfully
7272
*/
73-
public update(config: OdpConfig): boolean {
73+
update(config: OdpConfig): boolean {
7474
if (this.equals(config)) {
7575
return false;
7676
} else {
@@ -85,7 +85,7 @@ export class OdpConfig {
8585
/**
8686
* Determines if ODP configuration has the minimum amount of information
8787
*/
88-
public isReady(): boolean {
88+
isReady(): boolean {
8989
return !!this._apiKey && !!this._apiHost;
9090
}
9191

@@ -94,7 +94,7 @@ export class OdpConfig {
9494
* @param configToCompare ODP Configuration to check self against for equality
9595
* @returns Boolean based on if the current ODP Config is equivalent to the incoming ODP Config
9696
*/
97-
public equals(configToCompare: OdpConfig): boolean {
97+
equals(configToCompare: OdpConfig): boolean {
9898
return (
9999
this._apiHost === configToCompare._apiHost &&
100100
this._apiKey === configToCompare._apiKey &&

packages/optimizely-sdk/lib/core/odp/odp_event.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2022, Optimizely
2+
* Copyright 2022-2023, Optimizely
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,22 +18,22 @@ export class OdpEvent {
1818
/**
1919
* Type of event (typically "fullstack")
2020
*/
21-
public type: string;
21+
type: string;
2222

2323
/**
2424
* Subcategory of the event type
2525
*/
26-
public action: string;
26+
action: string;
2727

2828
/**
2929
* Key-value map of user identifiers
3030
*/
31-
public identifiers: Map<string, string>;
31+
identifiers: Map<string, string>;
3232

3333
/**
3434
* Event data in a key-value map
3535
*/
36-
public data: Map<string, unknown>;
36+
data: Map<string, unknown>;
3737

3838
/**
3939
* Event to be sent and stored in the Optimizely Data Platform

packages/optimizely-sdk/lib/core/odp/odp_event_api_manager.ts

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,16 @@ export interface IOdpEventApiManager {
3131
* Concrete implementation for accessing the ODP REST API
3232
*/
3333
export abstract class OdpEventApiManager implements IOdpEventApiManager {
34+
/**
35+
* Handler for recording execution logs
36+
* @private
37+
*/
3438
private readonly logger: LogHandler;
39+
40+
/**
41+
* Handler for making external HTTP/S requests
42+
* @private
43+
*/
3544
private readonly requestHandler: RequestHandler;
3645

3746
/**
@@ -44,7 +53,7 @@ export abstract class OdpEventApiManager implements IOdpEventApiManager {
4453
this.logger = logger;
4554
}
4655

47-
public getLogger(): LogHandler {
56+
getLogger(): LogHandler {
4857
return this.logger;
4958
}
5059

@@ -55,7 +64,7 @@ export abstract class OdpEventApiManager implements IOdpEventApiManager {
5564
* @param events ODP events to send
5665
* @returns Retry is true - if network or server error (5xx), otherwise false
5766
*/
58-
public async sendEvents(apiKey: string, apiHost: string, events: OdpEvent[]): Promise<boolean> {
67+
async sendEvents(apiKey: string, apiHost: string, events: OdpEvent[]): Promise<boolean> {
5968
let shouldRetry = false;
6069

6170
if (!apiKey || !apiHost) {
@@ -101,10 +110,14 @@ export abstract class OdpEventApiManager implements IOdpEventApiManager {
101110

102111
protected abstract shouldSendEvents(events: OdpEvent[]): boolean;
103112

104-
protected abstract generateRequestData(apiHost: string, apiKey: string, events: OdpEvent[]): {
105-
method: string,
106-
endpoint: string,
107-
headers: {[key: string]: string},
108-
data: string,
109-
}
113+
protected abstract generateRequestData(
114+
apiHost: string,
115+
apiKey: string,
116+
events: OdpEvent[]
117+
): {
118+
method: string;
119+
endpoint: string;
120+
headers: { [key: string]: string };
121+
data: string;
122+
};
110123
}

packages/optimizely-sdk/lib/core/odp/odp_event_manager.ts

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,59 +61,69 @@ export abstract class OdpEventManager implements IOdpEventManager {
6161
/**
6262
* Current state of the event processor
6363
*/
64-
public state: STATE = STATE.STOPPED;
64+
state: STATE = STATE.STOPPED;
65+
6566
/**
6667
* Queue for holding all events to be eventually dispatched
6768
* @protected
6869
*/
6970
protected queue = new Array<OdpEvent>();
71+
7072
/**
7173
* Identifier of the currently running timeout so clearCurrentTimeout() can be called
7274
* @private
7375
*/
7476
private timeoutId?: NodeJS.Timeout | number;
77+
7578
/**
76-
* ODP configuration settings in used
79+
* ODP configuration settings for identifying the target API and segments
7780
* @private
7881
*/
7982
private odpConfig: OdpConfig;
83+
8084
/**
8185
* REST API Manager used to send the events
8286
* @private
8387
*/
8488
private readonly apiManager: IOdpEventApiManager;
89+
8590
/**
8691
* Handler for recording execution logs
8792
* @private
8893
*/
8994
private readonly logger: LogHandler;
95+
9096
/**
9197
* Maximum queue size
9298
* @protected
9399
*/
94100
protected queueSize!: number;
101+
95102
/**
96103
* Maximum number of events to process at once. Ignored in browser context
97104
* @protected
98105
*/
99106
protected batchSize!: number;
107+
100108
/**
101109
* Milliseconds between setTimeout() to process new batches. Ignored in browser context
102110
* @protected
103111
*/
104112
protected flushInterval!: number;
113+
105114
/**
106115
* Type of execution context eg node, js, react
107116
* @private
108117
*/
109118
private readonly clientEngine: string;
119+
110120
/**
111121
* Version of the client being used
112122
* @private
113123
*/
114124
private readonly clientVersion: string;
115125

116-
public constructor({
126+
constructor({
117127
odpConfig,
118128
apiManager,
119129
logger,
@@ -151,21 +161,21 @@ export abstract class OdpEventManager implements IOdpEventManager {
151161
* Update ODP configuration settings.
152162
* @param newConfig New configuration to apply
153163
*/
154-
public updateSettings(newConfig: OdpConfig): void {
164+
updateSettings(newConfig: OdpConfig): void {
155165
this.odpConfig = newConfig;
156166
}
157167

158168
/**
159169
* Cleans up all pending events; occurs every time the ODP Config is updated.
160170
*/
161-
public flush(): void {
171+
flush(): void {
162172
this.processQueue(true);
163173
}
164174

165175
/**
166176
* Start processing events in the queue
167177
*/
168-
public start(): void {
178+
start(): void {
169179
this.state = STATE.RUNNING;
170180

171181
this.setNewTimeout();
@@ -174,7 +184,7 @@ export abstract class OdpEventManager implements IOdpEventManager {
174184
/**
175185
* Drain the queue sending all remaining events in batches then stop processing
176186
*/
177-
public async stop(): Promise<void> {
187+
async stop(): Promise<void> {
178188
this.logger.log(LogLevel.DEBUG, 'Stop requested.');
179189

180190
await this.processQueue(true);
@@ -187,7 +197,7 @@ export abstract class OdpEventManager implements IOdpEventManager {
187197
* Register a new visitor user id (VUID) in ODP
188198
* @param vuid Visitor User ID to send
189199
*/
190-
public registerVuid(vuid: string): void {
200+
registerVuid(vuid: string): void {
191201
const identifiers = new Map<string, string>();
192202
identifiers.set(ODP_USER_KEY.VUID, vuid);
193203

@@ -200,7 +210,7 @@ export abstract class OdpEventManager implements IOdpEventManager {
200210
* @param {string} userId (Optional) Full-stack User ID
201211
* @param {string} vuid (Optional) Visitor User ID
202212
*/
203-
public identifyUser(userId?: string, vuid?: string): void {
213+
identifyUser(userId?: string, vuid?: string): void {
204214
const identifiers = new Map<string, string>();
205215
if (!userId && !vuid) {
206216
this.logger.log(LogLevel.ERROR, ERROR_MESSAGES.ODP_SEND_EVENT_FAILED_UID_MISSING);
@@ -223,7 +233,7 @@ export abstract class OdpEventManager implements IOdpEventManager {
223233
* Send an event to ODP via dispatch queue
224234
* @param event ODP Event to forward
225235
*/
226-
public sendEvent(event: OdpEvent): void {
236+
sendEvent(event: OdpEvent): void {
227237
if (invalidOdpDataFound(event.data)) {
228238
this.logger.log(LogLevel.ERROR, 'Event data found to be invalid.');
229239
} else {

packages/optimizely-sdk/lib/core/odp/odp_manager.ts

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,27 +27,47 @@ import { OptimizelySegmentOption } from './optimizely_segment_option';
2727
import { invalidOdpDataFound } from './odp_utils';
2828
import { OdpEvent } from './odp_event';
2929

30+
/**
31+
* Manager for handling internal all business logic related to
32+
* Optimizely Data Platform (ODP) / Advanced Audience Targeting (AAT)
33+
*/
3034
export interface IOdpManager {
35+
initPromise?: Promise<void>;
36+
3137
enabled: boolean;
38+
3239
segmentManager: IOdpSegmentManager | undefined;
40+
3341
eventManager: IOdpEventManager | undefined;
42+
3443
updateSettings({ apiKey, apiHost, segmentsToCheck }: OdpConfig): boolean;
44+
3545
close(): void;
46+
3647
fetchQualifiedSegments(userId: string, options?: Array<OptimizelySegmentOption>): Promise<string[] | null>;
48+
3749
identifyUser(userId?: string, vuid?: string): void;
50+
3851
sendEvent({ type, action, identifiers, data }: OdpEvent): void;
52+
3953
isVuidEnabled(): boolean;
54+
4055
getVuid(): string | undefined;
4156
}
4257

4358
/**
4459
* Orchestrates segments manager, event manager, and ODP configuration
4560
*/
4661
export abstract class OdpManager implements IOdpManager {
62+
/**
63+
* Promise that returns when the OdpManager is finished initializing
64+
*/
4765
initPromise?: Promise<void>;
66+
67+
/**
68+
* Switch to enable/disable ODP Manager functionality
69+
*/
4870
enabled = true;
49-
logger: LogHandler = getLogger();
50-
odpConfig: OdpConfig = new OdpConfig();
5171

5272
/**
5373
* ODP Segment Manager which provides an interface to the remote ODP server (GraphQL API) for audience segments mapping.
@@ -61,7 +81,18 @@ export abstract class OdpManager implements IOdpManager {
6181
*/
6282
eventManager: IOdpEventManager | undefined;
6383

64-
constructor() {}
84+
/**
85+
* Handler for recording execution logs
86+
* @protected
87+
*/
88+
protected logger: LogHandler = getLogger(); // TODO: Consider making private and moving instantiation to constructor
89+
90+
/**
91+
* ODP configuration settings for identifying the target API and segments
92+
*/
93+
odpConfig: OdpConfig = new OdpConfig(); // TODO: Consider making private and adding public accessors
94+
95+
constructor() {} // TODO: Consider accepting logger as a parameter and initializing it in constructor instead
6596

6697
/**
6798
* Provides a method to update ODP Manager's ODP Config API Key, API Host, and Audience Segments
@@ -195,7 +226,13 @@ export abstract class OdpManager implements IOdpManager {
195226
this.eventManager.sendEvent(new OdpEvent(mType, action, identifiers, data));
196227
}
197228

229+
/**
230+
* Identifies if the VUID feature is enabled
231+
*/
198232
abstract isVuidEnabled(): boolean;
199233

234+
/**
235+
* Returns VUID value if it exists
236+
*/
200237
abstract getVuid(): string | undefined;
201238
}

0 commit comments

Comments
 (0)