Skip to content

Optimize ProjectDataService.getProjectData() #5398

Closed
@lambourn

Description

@lambourn

Is your feature request related to a problem? Please describe.

While developing a before-prepare hook that should adjust some CI aspects (use different bundle IDs, setup dynamic code signing. etc.) I wanted to modify some content on the injected $projectData object from inside the hook.

However, my changes applied in the hook were all gone after the hook was done. In the end, the CLI worked on the same ProjectData object instance but all modified data was wiped.

I debugged the CLI and found that ProjectDataService.getProjectData() is called ridicously often by the CLI. While that still might ok, the internal caching mechanism itself is just not used at all. This leads to unnecessary re-initialization of the projectData over and over again and the cache has no effect at all.

Relevant code snippet

public getProjectData(projectDir: string): IProjectData {
projectDir = projectDir || this.defaultProjectDir;
this.projectDataCache[projectDir] =
this.projectDataCache[projectDir] ||
this.$injector.resolve<IProjectData>(ProjectData);
this.projectDataCache[projectDir].initializeProjectData(projectDir);
return this.projectDataCache[projectDir];
}

Describe the solution you'd like

The getProjectData is missing a simple check if the project data has been initialized already. If not, initialize it, otherwise return the cached value.

I monkey-patched this locally and it works for me. Not sure of any side-effects, though. It goes like this:

public getProjectData(projectDir: string): IProjectData {
    projectDir = projectDir || this.defaultProjectDir;
    this.projectDataCache[projectDir] =
        this.projectDataCache[projectDir] ||
        this.$injector.resolve<IProjectData>(ProjectData);
    if (!this.projectDataCache[projectDir].initialized) {
        this.projectDataCache[projectDir].initializeProjectData(projectDir);
        // either have initializeProjectData taking care of setting the flag or set it here
        // (or use any of the existing properties that get initialized in initializeProjectData...)
        this.projectDataCache[projectDir].initialized = true;
    }
    return this.projectDataCache[projectDir];
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions