@@ -26,8 +26,6 @@ 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
- const MODULE_NAME = 'PROJECT_CONFIG_MANAGER' ;
30
-
31
29
interface ProjectConfigManagerConfig {
32
30
// TODO: Don't use object type
33
31
// eslint-disable-next-line @typescript-eslint/ban-types
@@ -79,6 +77,7 @@ export class ProjectConfigManagerImpl extends BaseService implements ProjectConf
79
77
80
78
this . state = ServiceState . Starting ;
81
79
if ( ! this . datafile && ! this . datafileManager ) {
80
+ // TODO: replace message with imported constants
82
81
this . handleInitError ( new Error ( 'You must provide at least one of sdkKey or datafile' ) ) ;
83
82
return ;
84
83
}
@@ -91,7 +90,7 @@ export class ProjectConfigManagerImpl extends BaseService implements ProjectConf
91
90
this . datafileManager ?. onUpdate ( this . handleNewDatafile . bind ( this ) ) ;
92
91
93
92
// If the datafile manager runs successfully, it will emit a onUpdate event. We can
94
- // handle the success case in the onUpdate handler. Hanlding the error case in the.
93
+ // handle the success case in the onUpdate handler. Hanlding the error case in the
95
94
// catch callback
96
95
this . datafileManager ?. onRunning ( ) . catch ( ( err ) => {
97
96
this . handleDatafileManagerError ( err ) ;
@@ -114,7 +113,13 @@ export class ProjectConfigManagerImpl extends BaseService implements ProjectConf
114
113
* is fulfilled with an unsuccessful result.
115
114
*/
116
115
private handleDatafileManagerError ( err : Error ) : void {
117
- if ( this . isNew ( ) ) {
116
+ // TODO: replace message with imported constants
117
+ this . logger ?. error ( 'datafile manager failed to start' , err ) ;
118
+
119
+ // If datafile manager onRunning() promise is rejected, and the project config manager
120
+ // is still in starting state, that means a datafile was not provided or was invalid.
121
+ // In this case, we cannot recover and must reject the start promise.
122
+ if ( this . isStarting ( ) ) {
118
123
this . handleInitError ( err ) ;
119
124
}
120
125
}
@@ -126,40 +131,36 @@ export class ProjectConfigManagerImpl extends BaseService implements ProjectConf
126
131
* the project config and optimizely config objects will not be updated, and the error is returned.
127
132
*/
128
133
private handleNewDatafile ( newDatafile : string | object ) : void {
129
- const configOrError = tryCreatingProjectConfig ( {
130
- datafile : newDatafile ,
131
- jsonSchemaValidator : this . jsonSchemaValidator ,
132
- logger : this . logger ,
133
- } ) ;
134
+ try {
135
+ const config = tryCreatingProjectConfig ( {
136
+ datafile : newDatafile ,
137
+ jsonSchemaValidator : this . jsonSchemaValidator ,
138
+ logger : this . logger ,
139
+ } ) ;
134
140
135
- if ( configOrError instanceof Error ) {
136
- this . logger ?. error ( configOrError ) ;
141
+ if ( this . isStarting ( ) ) {
142
+ this . state = ServiceState . Running ;
143
+ this . startPromise . resolve ( ) ;
144
+ }
145
+
146
+ if ( this . projectConfig ?. revision !== config . revision ) {
147
+ this . projectConfig = config ;
148
+ this . optimizelyConfig = undefined ;
149
+ scheduleMicrotask ( ( ) => {
150
+ this . eventEmitter . emit ( 'update' , config ) ;
151
+ } )
152
+ }
153
+ } catch ( err ) {
154
+ this . logger ?. error ( err ) ;
137
155
// the provided datafile is invalid
138
156
// and no datafile manager is provided
139
157
// so there is no way to recover
140
- if ( this . isNew ( ) && ! this . datafileManager ) {
141
- this . handleInitError ( configOrError ) ;
158
+ if ( this . isStarting ( ) && ! this . datafileManager ) {
159
+ this . handleInitError ( err ) ;
142
160
}
143
- return ;
144
- }
145
-
146
- const config = configOrError ;
147
-
148
- if ( this . isNew ( ) ) {
149
- this . state = ServiceState . Running ;
150
- this . startPromise . resolve ( ) ;
151
- }
152
-
153
- if ( this . projectConfig ?. revision !== config . revision ) {
154
- this . projectConfig = config ;
155
- this . optimizelyConfig = undefined ;
156
- scheduleMicrotask ( ( ) => {
157
- this . eventEmitter . emit ( 'update' , config ) ;
158
- } )
159
161
}
160
162
}
161
163
162
-
163
164
getConfig ( ) : ProjectConfig | undefined {
164
165
return this . projectConfig ;
165
166
}
@@ -186,7 +187,6 @@ export class ProjectConfigManagerImpl extends BaseService implements ProjectConf
186
187
* Stop the internal datafile manager and remove all update listeners
187
188
*/
188
189
stop ( ) : void {
189
- this . state = ServiceState . Stopping ;
190
190
this . state = ServiceState . Stopping ;
191
191
this . eventEmitter . removeAllListeners ( ) ;
192
192
0 commit comments