13
13
* See the License for the specific language governing permissions and
14
14
* limitations under the License.
15
15
*/
16
- import { getLogger , LoggerFacade } from '../modules/logging' ;
16
+ import { LoggerFacade } from '../modules/logging' ;
17
17
import { sprintf } from '../utils/fns' ;
18
18
19
19
import { ERROR_MESSAGES } from '../utils/enums' ;
20
20
import { createOptimizelyConfig } from '../core/optimizely_config' ;
21
21
import { OptimizelyConfig } from '../shared_types' ;
22
- import { DatafileManager } from './datafileManager ' ;
22
+ import { DatafileManager } from './datafile_manager ' ;
23
23
import { ProjectConfig , toDatafile , tryCreatingProjectConfig } from '../project_config' ;
24
24
import { scheduleMicrotask } from '../utils/microtask' ;
25
25
import { Service , ServiceState , BaseService } from '../service' ;
26
26
import { Consumer , Fn , Transformer } from '../utils/type' ;
27
27
import { EventEmitter } from '../utils/event_emitter/eventEmitter' ;
28
28
29
-
30
- const logger = getLogger ( ) ;
31
29
const MODULE_NAME = 'PROJECT_CONFIG_MANAGER' ;
32
30
33
31
interface ProjectConfigManagerConfig {
34
- // TODO[OASIS-6649] : Don't use object type
32
+ // TODO: Don't use object type
35
33
// eslint-disable-next-line @typescript-eslint/ban-types
36
34
datafile ?: string | object ;
37
35
jsonSchemaValidator ?: Transformer < unknown , boolean > ,
38
36
datafileManager ?: DatafileManager ;
37
+ logger ?: LoggerFacade ;
39
38
}
40
39
41
40
export interface ProjectConfigManager extends Service {
@@ -63,7 +62,7 @@ export class ProjectConfigManagerImpl extends BaseService implements ProjectConf
63
62
64
63
constructor ( config : ProjectConfigManagerConfig ) {
65
64
super ( ) ;
66
-
65
+ this . logger = config . logger ;
67
66
this . jsonSchemaValidator = config . jsonSchemaValidator ;
68
67
this . datafile = config . datafile ;
69
68
this . datafileManager = config . datafileManager ;
@@ -73,18 +72,6 @@ export class ProjectConfigManagerImpl extends BaseService implements ProjectConf
73
72
this . logger = logger ;
74
73
}
75
74
76
- getState ( ) : ServiceState {
77
- return this . state ;
78
- }
79
-
80
- onRunning ( ) : Promise < void > {
81
- return this . startPromise . promise ;
82
- }
83
-
84
- onTerminated ( ) : Promise < void > {
85
- return this . stopPromise . promise ;
86
- }
87
-
88
75
start ( ) {
89
76
if ( ! this . datafile && ! this . datafileManager ) {
90
77
this . handleInitError ( new Error ( 'You must provide at least one of sdkKey or datafile' ) ) ;
@@ -96,99 +83,30 @@ export class ProjectConfigManagerImpl extends BaseService implements ProjectConf
96
83
}
97
84
98
85
this . datafileManager ?. start ( ) ;
99
- this . datafileManager ?. onUpdate ( ( this . handleNewDatafile . bind ( this ) ) ) ;
86
+ this . datafileManager ?. onUpdate ( this . handleNewDatafile . bind ( this ) ) ;
87
+
88
+ // If the datafile manager runs successfully, it will emit a onUpdate event. We can
89
+ // handle the success case in the onUpdate handler. Hanlding the error case in the.
90
+ // catch callback
100
91
this . datafileManager ?. onRunning ( ) . catch ( ( err ) => {
101
92
this . handleDatafileManagerError ( err ) ;
102
93
} ) ;
103
94
}
104
95
105
- // try {
106
- // if (!config.datafile && !config.sdkKey) {
107
- // const datafileAndSdkKeyMissingError = new Error(
108
- // sprintf(ERROR_MESSAGES.DATAFILE_AND_SDK_KEY_MISSING, MODULE_NAME)
109
- // );
110
- // this.readyPromise = Promise.resolve({
111
- // success: false,
112
- // reason: getErrorMessage(datafileAndSdkKeyMissingError),
113
- // });
114
- // logger.error(datafileAndSdkKeyMissingError);
115
- // return;
116
- // }
117
-
118
- // let handleNewDatafileException = null;
119
- // if (config.datafile) {
120
- // handleNewDatafileException = this.handleNewDatafile(config.datafile);
121
- // }
122
-
123
- // if (config.sdkKey && config.datafileManager) {
124
- // this.datafileManager = config.datafileManager;
125
- // this.datafileManager.start();
126
-
127
- // this.readyPromise = this.datafileManager
128
- // .onReady()
129
- // .then(this.onDatafileManagerReadyFulfill.bind(this), this.onDatafileManagerReadyReject.bind(this));
130
- // this.datafileManager.on('update', this.onDatafileManagerUpdate.bind(this));
131
- // } else if (this.projectConfig) {
132
- // this.readyPromise = Promise.resolve({
133
- // success: true,
134
- // });
135
- // } else {
136
- // this.readyPromise = Promise.resolve({
137
- // success: false,
138
- // reason: getErrorMessage(handleNewDatafileException, 'Invalid datafile'),
139
- // });
140
- // }
141
- // } catch (ex) {
142
- // logger.error(ex);
143
- // this.readyPromise = Promise.resolve({
144
- // success: false,
145
- // reason: getErrorMessage(ex, 'Error in initialize'),
146
- // });
147
- // }
148
- // }
149
-
150
96
private handleInitError ( error : Error ) : void {
151
- logger . error ( error ) ;
97
+ this . logger ? .error ( error ) ;
152
98
this . state = ServiceState . Failed ;
153
99
this . datafileManager ?. stop ( ) ;
154
100
this . startPromise . reject ( error ) ;
155
101
this . stopPromise . reject ( error ) ;
156
102
}
157
103
158
- /**
159
- * Respond to datafile manager's onReady promise becoming fulfilled.
160
- * If there are validation or parse failures using the datafile provided by
161
- * DatafileManager, ProjectConfigManager's ready promise is resolved with an
162
- * unsuccessful result. Otherwise, ProjectConfigManager updates its own project
163
- * config object from the new datafile, and its ready promise is resolved with a
164
- * successful result.
165
- */
166
- // private onDatafileManagerReadyFulfill(): OnReadyResult {
167
- // if (this.datafileManager) {
168
- // const newDatafileError = this.handleNewDatafile(this.datafileManager.get());
169
- // if (newDatafileError) {
170
- // return {
171
- // success: false,
172
- // reason: getErrorMessage(newDatafileError),
173
- // };
174
- // }
175
- // return { success: true };
176
- // }
177
-
178
- // return {
179
- // success: false,
180
- // reason: getErrorMessage(null, 'Datafile manager is not provided'),
181
- // };
182
- // }
183
-
184
104
/**
185
105
* Respond to datafile manager's onRunning promise becoming rejected.
186
106
* When DatafileManager's onReady promise is rejected, if a datafile was not provided and therefore
187
107
* the projectConfigManager is still in New state, there is no possibility
188
108
* of obtaining a datafile. In this case, ProjectConfigManager's ready promise
189
109
* is fulfilled with an unsuccessful result.
190
- * @param {Error } err
191
- * @returns {Object }
192
110
*/
193
111
private handleDatafileManagerError ( err : Error ) : void {
194
112
if ( this . isNew ( ) ) {
@@ -201,18 +119,16 @@ export class ProjectConfigManagerImpl extends BaseService implements ProjectConf
201
119
* the new config object's revision is newer than the current one, sets/updates the project config
202
120
* and optimizely config object instance variables and returns null for the error. If unsuccessful,
203
121
* the project config and optimizely config objects will not be updated, and the error is returned.
204
- * @param {string | object } newDatafile
205
- * @returns {Error|null } error or null
206
122
*/
207
123
private handleNewDatafile ( newDatafile : string | object ) : void {
208
124
const configOrError = tryCreatingProjectConfig ( {
209
125
datafile : newDatafile ,
210
126
jsonSchemaValidator : this . jsonSchemaValidator ,
211
- logger : logger ,
127
+ logger : this . logger ,
212
128
} ) ;
213
129
214
130
if ( configOrError instanceof Error ) {
215
- logger . error ( configOrError ) ;
131
+ this . logger ? .error ( configOrError ) ;
216
132
// the provided datafile is invalid
217
133
// and no datafile manager is provided
218
134
// so there is no way to recover
@@ -238,22 +154,14 @@ export class ProjectConfigManagerImpl extends BaseService implements ProjectConf
238
154
}
239
155
}
240
156
241
- /**
242
- * Returns the current project config object, or null if no project config object
243
- * is available
244
- * @return {ProjectConfig|null }
245
- */
157
+
246
158
getConfig ( ) : ProjectConfig | undefined {
247
159
return this . projectConfig ;
248
160
}
249
161
250
- /**
251
- * Returns the optimizely config object or null
252
- * @return {OptimizelyConfig|null }
253
- */
254
162
getOptimizelyConfig ( ) : OptimizelyConfig | undefined {
255
163
if ( ! this . optimizelyConfig && this . projectConfig ) {
256
- this . optimizelyConfig = createOptimizelyConfig ( this . projectConfig , toDatafile ( this . projectConfig ) , logger ) ;
164
+ this . optimizelyConfig = createOptimizelyConfig ( this . projectConfig , toDatafile ( this . projectConfig ) , this . logger ) ;
257
165
}
258
166
return this . optimizelyConfig ;
259
167
}
0 commit comments