Skip to content

feat: Convert project config manager module to TS #684

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Jul 27, 2021

Conversation

yavorona
Copy link
Contributor

@yavorona yavorona commented Jun 22, 2021

Summary

  • Convert project_config_manager.js to TS

Test plan

Existing unit tests

@yavorona yavorona requested a review from a team as a code owner June 22, 2021 22:05
@yavorona yavorona added the WIP label Jun 23, 2021
@yavorona yavorona force-pushed the pnguen/convert-project-config-manager branch from f5f6ccf to c201a8f Compare June 23, 2021 22:19
@coveralls
Copy link

coveralls commented Jun 23, 2021

Coverage Status

Coverage increased (+0.0006%) to 96.978% when pulling 34e0816 on pnguen/convert-project-config-manager into 1b0c036 on master.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.05%) to 96.934% when pulling 55dc6be on pnguen/convert-project-config-manager into a4dcc3e on master.

@yavorona yavorona force-pushed the pnguen/convert-project-config-manager branch from 55dc6be to b986680 Compare July 22, 2021 20:09
@yavorona yavorona removed the WIP label Jul 22, 2021
@yavorona yavorona removed their assignment Jul 26, 2021
import { toDatafile, tryCreatingProjectConfig } from '../../core/project_config';
import { createOptimizelyConfig } from '../optimizely_config';
import { OptimizelyConfig, DatafileOptions, DatafileManagerConfig } from '../../shared_types';
import { ProjectConfig } from '../project_config';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This and line 22 can be imported in one import because i think they are referring to the same package using different paths

Comment on lines 71 to 75
this.configObj = null;
this.optimizelyConfigObj = null;
this.updateListeners = [];
this.jsonSchemaValidator = config.jsonSchemaValidator;
this.datafileManager = null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a specific reason to define these separately in constructors. Can these initialization values go with instance variable declarations above the constructor?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is a great idea :)

Comment on lines 119 to 127
logger.error(ex);
this.updateListeners = [];
this.configObj = null;
this.optimizelyConfigObj = null;
this.datafileManager = null;
this.readyPromise = Promise.resolve({
success: false,
reason: getErrorMessage(ex, 'Error in initialize'),
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some of these might not be needed here if you initialize default values with instance variable declarations at the top of the class.

public datafileManager: HttpPollingDatafileManager | null;

constructor(config: ProjectConfigManagerConfig) {
try {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you see a need to add a try block here when it wasnt there in the JS version?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

* successful result.
*/
private onDatafileManagerReadyFulfill(): { success: boolean; reason?: string } {
if (this.datafileManager) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you add this extra check in the typescript version. datafile manager is not expected to be null here because promise can only be fulfilled after its initialization

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True but TS compiler is not smart enough to know it. It requires this check prior to calling this.datafileManager.get() since the initial value of it is null.

private updateListeners: Array<(config: ProjectConfig) => void>;
private configObj: ProjectConfig | null;
private optimizelyConfigObj: OptimizelyConfig | null;
private readyPromise: Promise<{ success: boolean; reason?: string }>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion (Non binding): I see you using { success: boolean; reason?: string } type at many places in the file. What do you think about declaring a type at the top and reusing it for simplicity. Totally up to your preference.

* update listeners if successful
*/
private onDatafileManagerUpdate(): void {
if (this.datafileManager) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, why do you need a check here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above.

Comment on lines 222 to 224
this.updateListeners.forEach((listener) => {
listener(configObj);
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
this.updateListeners.forEach((listener) => {
listener(configObj);
});
this.updateListeners.forEach((listener) => listener(configObj));

Comment on lines 1 to 18
declare module '@optimizely/js-sdk-datafile-manager' {
interface DatafileManagerConfig {
sdkKey: string;
}
interface DatafileUpdate {
datafile: string;
}
type Disposer = () => void;

export class HttpPollingDatafileManager {
constructor(config: DatafileManagerConfig);
start(): void;
onReady(): Promise<void>;
on(eventName: string, listener: (datafileUpdate: DatafileUpdate) => void): Disposer;
get(): string;
stop(): Promise<void>;
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The indentation looks off. it should be 2 spaces per tab.

@yavorona yavorona requested a review from zashraf1985 July 26, 2021 22:29
Copy link
Contributor

@zashraf1985 zashraf1985 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great Work! LGTM!

@yavorona yavorona merged commit 3d8a5b8 into master Jul 27, 2021
@yavorona yavorona deleted the pnguen/convert-project-config-manager branch July 27, 2021 20:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants