Skip to content
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

Improve efficiency of saving and deleting issue models #193

Merged
merged 8 commits into from
Sep 26, 2023
59 changes: 32 additions & 27 deletions src/app/core/services/issue.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export class IssueService {
getLatestIssue(id: number): Observable<Issue> {
return this.githubService.fetchIssueGraphql(id).pipe(
map((response: GithubIssue) => {
this.createAndSaveIssueModel(response);
this.createAndSaveIssueModels([response]);
return this.issues[id];
}),
catchError((err) => {
vigneshsankariyer1234567890 marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -83,23 +83,18 @@ export class IssueService {
);
}

/**
* This function will update the issue's state of the application. This function needs to be called whenever a issue is deleted.
*/
deleteFromLocalStore(issueToDelete: Issue) {
const { [issueToDelete.id]: issueToRemove, ...withoutIssueToRemove } = this.issues;
this.issues = withoutIssueToRemove;
this.issues$.next(Object.values(this.issues));
}

/**
* This function will update the issue's state of the application. This function needs to be called whenever a issue is added/updated.
*
* @params issuesToUpdate - An array of issues to update the state of the application with.
*/
updateLocalStore(issueToUpdate: Issue) {
this.issues = {
...this.issues,
[issueToUpdate.id]: issueToUpdate
};
private updateLocalStore(issuesToUpdate: Issue[]) {
const newIssues = { ...this.issues };
issuesToUpdate.forEach((issue) => {
newIssues[issue.id] = issue;
});
this.issues = newIssues;

this.issues$.next(Object.values(this.issues));
}

Expand Down Expand Up @@ -134,11 +129,10 @@ export class IssueService {
const fetchedIssueIds: number[] = [];

return issuesAPICallsByFilter.pipe(
map((issuesByFilter: []) => {
// Take each issue and put it in next in issues$
for (const issue of issuesByFilter) {
fetchedIssueIds.push(this.createIssueModel(issue).id);
this.createAndSaveIssueModel(issue);
map((githubIssues: GithubIssue[]) => {
const issues = this.createAndSaveIssueModels(githubIssues);
for (const issue of issues) {
fetchedIssueIds.push(issue.id);
}

const outdatedIssueIds: number[] = this.getOutdatedIssueIds(fetchedIssueIds);
Expand All @@ -152,16 +146,27 @@ export class IssueService {
);
}

private createAndSaveIssueModel(githubIssue: GithubIssue): boolean {
const issue = this.createIssueModel(githubIssue);
this.updateLocalStore(issue);
return true;
private createAndSaveIssueModels(githubIssues: GithubIssue[]): Issue[] {
const issues: Issue[] = [];

for (const githubIssue of githubIssues) {
const issue = this.createIssueModel(githubIssue);
issues.push(issue);
}
this.updateLocalStore(issues);

return issues;
}

private deleteIssuesFromLocalStore(ids: number[]): void {
ids.forEach((id: number) => {
this.getIssue(id).subscribe((issue) => this.deleteFromLocalStore(issue));
});
const withoutIssuesToRemove = { ...this.issues };
for (const id of ids) {
delete withoutIssuesToRemove[id];
}
vigneshsankariyer1234567890 marked this conversation as resolved.
Show resolved Hide resolved

this.issues = withoutIssuesToRemove;

this.issues$.next(Object.values(this.issues));
}

/**
Expand Down
Loading