Skip to content

Commit 0098fc2

Browse files
committed
update
1 parent b41f28f commit 0098fc2

File tree

3 files changed

+38
-48
lines changed

3 files changed

+38
-48
lines changed

lib/project_config/project_config.ts

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -796,32 +796,23 @@ export const toDatafile = function(projectConfig: ProjectConfig): string {
796796
/**
797797
* Try to create a project config object from the given datafile and
798798
* configuration properties.
799-
* Returns an object with configObj and error properties.
800-
* If successful, configObj is the project config object, and error is null.
801-
* Otherwise, configObj is null and error is an error with more information.
799+
* Returns a ProjectConfig if successful.
800+
* Otherwise, throws an error.
802801
* @param {Object} config
803802
* @param {Object|string} config.datafile
804803
* @param {Object} config.jsonSchemaValidator
805804
* @param {Object} config.logger
806-
* @returns {Object} Object containing configObj and error properties
805+
* @returns {Object} ProjectConfig
806+
* @throws {Error}
807807
*/
808808
export const tryCreatingProjectConfig = function(
809809
config: TryCreatingProjectConfigConfig
810-
): ProjectConfig | Error {
811-
let newDatafileObj;
812-
try {
813-
newDatafileObj = configValidator.validateDatafile(config.datafile);
814-
} catch (error) {
815-
return error;
816-
}
810+
): ProjectConfig {
811+
let newDatafileObj = configValidator.validateDatafile(config.datafile);
817812

818813
if (config.jsonSchemaValidator) {
819-
try {
820814
config.jsonSchemaValidator(newDatafileObj);
821815
config.logger?.log(LOG_LEVEL.INFO, LOG_MESSAGES.VALID_DATAFILE, MODULE_NAME);
822-
} catch (error) {
823-
return error;
824-
}
825816
} else {
826817
config.logger?.log(LOG_LEVEL.INFO, LOG_MESSAGES.SKIPPING_JSON_VALIDATION, MODULE_NAME);
827818
}
@@ -833,7 +824,6 @@ export const tryCreatingProjectConfig = function(
833824
}
834825

835826
const newConfigObj = createProjectConfig(...createProjectConfigArgs);
836-
837827
return newConfigObj;
838828
};
839829

lib/project_config/project_config_manager.ts

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ import { Service, ServiceState, BaseService } from '../service';
2626
import { Consumer, Fn, Transformer } from '../utils/type';
2727
import { EventEmitter } from '../utils/event_emitter/eventEmitter';
2828

29-
const MODULE_NAME = 'PROJECT_CONFIG_MANAGER';
30-
3129
interface ProjectConfigManagerConfig {
3230
// TODO: Don't use object type
3331
// eslint-disable-next-line @typescript-eslint/ban-types
@@ -79,6 +77,7 @@ export class ProjectConfigManagerImpl extends BaseService implements ProjectConf
7977

8078
this.state = ServiceState.Starting;
8179
if (!this.datafile && !this.datafileManager) {
80+
// TODO: replace message with imported constants
8281
this.handleInitError(new Error('You must provide at least one of sdkKey or datafile'));
8382
return;
8483
}
@@ -91,7 +90,7 @@ export class ProjectConfigManagerImpl extends BaseService implements ProjectConf
9190
this.datafileManager?.onUpdate(this.handleNewDatafile.bind(this));
9291

9392
// 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
9594
// catch callback
9695
this.datafileManager?.onRunning().catch((err) => {
9796
this.handleDatafileManagerError(err);
@@ -114,7 +113,13 @@ export class ProjectConfigManagerImpl extends BaseService implements ProjectConf
114113
* is fulfilled with an unsuccessful result.
115114
*/
116115
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()) {
118123
this.handleInitError(err);
119124
}
120125
}
@@ -126,40 +131,36 @@ export class ProjectConfigManagerImpl extends BaseService implements ProjectConf
126131
* the project config and optimizely config objects will not be updated, and the error is returned.
127132
*/
128133
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+
});
134140

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);
137155
// the provided datafile is invalid
138156
// and no datafile manager is provided
139157
// 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);
142160
}
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-
})
159161
}
160162
}
161163

162-
163164
getConfig(): ProjectConfig | undefined {
164165
return this.projectConfig;
165166
}
@@ -186,7 +187,6 @@ export class ProjectConfigManagerImpl extends BaseService implements ProjectConf
186187
* Stop the internal datafile manager and remove all update listeners
187188
*/
188189
stop(): void {
189-
this.state = ServiceState.Stopping;
190190
this.state = ServiceState.Stopping;
191191
this.eventEmitter.removeAllListeners();
192192

vitest.config.mts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export default defineConfig({
44
test: {
55
onConsoleLog: () => true,
66
environment: 'happy-dom',
7-
include: ["**/polling_datafile_manager.spec.ts"],
7+
include: ["**/project_config_manager.spec.ts"],
88
typecheck: {
99
tsconfig: 'tsconfig.spec.json',
1010
exclude: ['**/index.react_native.spec.ts'],

0 commit comments

Comments
 (0)