14
14
* limitations under the License.
15
15
*/
16
16
import { LoggerFacade } from '../modules/logging' ;
17
- import { sprintf } from '../utils/fns' ;
18
-
19
- import { ERROR_MESSAGES } from '../utils/enums' ;
20
17
import { createOptimizelyConfig } from '../core/optimizely_config' ;
21
18
import { OptimizelyConfig } from '../shared_types' ;
22
19
import { DatafileManager } from './datafile_manager' ;
@@ -44,8 +41,8 @@ export interface ProjectConfigManager extends Service {
44
41
45
42
/**
46
43
* ProjectConfigManager provides project config objects via its methods
47
- * getConfig and onUpdate. It uses a DatafileManager to fetch datafiles. It is
48
- * responsible for parsing and validating datafiles, and converting datafile
44
+ * getConfig and onUpdate. It uses a DatafileManager to fetch datafile if provided.
45
+ * It is responsible for parsing and validating datafiles, and converting datafile
49
46
* string into project config objects.
50
47
* @param {ProjectConfigManagerConfig } config
51
48
*/
@@ -87,6 +84,10 @@ export class ProjectConfigManagerImpl extends BaseService implements ProjectConf
87
84
}
88
85
89
86
this . datafileManager ?. start ( ) ;
87
+
88
+ // This handles the case where the datafile manager starts successfully. The
89
+ // datafile manager will only start successfully when it has downloaded a datafile,
90
+ // an will fire an onUpdate event.
90
91
this . datafileManager ?. onUpdate ( this . handleNewDatafile . bind ( this ) ) ;
91
92
92
93
// If the datafile manager runs successfully, it will emit a onUpdate event. We can
@@ -105,20 +106,14 @@ export class ProjectConfigManagerImpl extends BaseService implements ProjectConf
105
106
this . stopPromise . reject ( error ) ;
106
107
}
107
108
108
- /**
109
- * Respond to datafile manager's onRunning promise becoming rejected.
110
- * When DatafileManager's onReady promise is rejected, if a datafile was not provided and therefore
111
- * the projectConfigManager is still in New state, there is no possibility
112
- * of obtaining a datafile. In this case, ProjectConfigManager's ready promise
113
- * is fulfilled with an unsuccessful result.
114
- */
115
109
private handleDatafileManagerError ( err : Error ) : void {
116
110
// TODO: replace message with imported constants
117
111
this . logger ?. error ( 'datafile manager failed to start' , err ) ;
118
112
119
113
// 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.
114
+ // is still in starting state, that means a datafile was not provided in cofig or was invalid,
115
+ // otherwise the state would have already been set to running synchronously.
116
+ // In this case, we cannot recover.
122
117
if ( this . isStarting ( ) ) {
123
118
this . handleInitError ( err ) ;
124
119
}
@@ -127,8 +122,9 @@ export class ProjectConfigManagerImpl extends BaseService implements ProjectConf
127
122
/**
128
123
* Handle new datafile by attemping to create a new Project Config object. If successful and
129
124
* the new config object's revision is newer than the current one, sets/updates the project config
130
- * and optimizely config object instance variables and returns null for the error. If unsuccessful,
131
- * the project config and optimizely config objects will not be updated, and the error is returned.
125
+ * and emits onUpdate event. If unsuccessful,
126
+ * the project config and optimizely config objects will not be updated. If the error
127
+ * is fatal, handleInitError will be called.
132
128
*/
133
129
private handleNewDatafile ( newDatafile : string | object , fromConfig = false ) : void {
134
130
if ( this . isDone ( ) ) {
@@ -193,9 +189,6 @@ export class ProjectConfigManagerImpl extends BaseService implements ProjectConf
193
189
return this . eventEmitter . on ( 'update' , listener ) ;
194
190
}
195
191
196
- /**
197
- * Stop the internal datafile manager and remove all update listeners
198
- */
199
192
stop ( ) : void {
200
193
if ( this . isDone ( ) ) {
201
194
return ;
0 commit comments