Skip to content

Commit

Permalink
Optimized project service
Browse files Browse the repository at this point in the history
  • Loading branch information
miladsoft committed Sep 6, 2024
1 parent 1142865 commit aeffce3
Showing 1 changed file with 24 additions and 66 deletions.
90 changes: 24 additions & 66 deletions src/app/services/projects.service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Injectable } from '@angular/core';
import { HttpClient, HttpResponse } from '@angular/common/http';
import { IndexerService } from './indexer.service';
import { catchError } from 'rxjs/operators';
import { of, Observable } from 'rxjs';
import { IndexerService } from './indexer.service';

export interface Project {
founderKey: string;
Expand Down Expand Up @@ -36,71 +36,38 @@ export class ProjectsService {
constructor(
private http: HttpClient,
private indexerService: IndexerService

) {}

private async ensureTotalProjectsFetched(): Promise<void> {
if (!this.totalProjectsFetched) {
await this.getTotalProjectsCount();
this.totalProjectsFetched = true;
this.offset = Math.max(this.totalProjects - this.limit, 0);
async fetchProjects(): Promise<Project[]> {
if (this.loading || this.noMoreProjects) {
return [];
}
}

private async getTotalProjectsCount(): Promise<void> {
this.loading = true;
const indexerUrl = this.indexerService.getPrimaryIndexer('testnet');
if (!indexerUrl) {
throw new Error('No primary indexer found for testnet');
}
const url = this.totalProjectsFetched
? `${indexerUrl}api/query/Angor/projects?offset=${this.offset}&limit=${this.limit}`
: `${indexerUrl}api/query/Angor/projects?limit=${this.limit}`;

const url = `${indexerUrl}api/query/Angor/projects?offset=0&limit=1`;
console.log(`Fetching projects from URL: ${url}`);

try {
const response = await this.http
.get<Project[]>(url, { observe: 'response' })
.toPromise();
if (response && response.headers) {

if (!this.totalProjectsFetched && response && response.headers) {
const paginationTotal = response.headers.get('pagination-total');
this.totalProjects = paginationTotal ? +paginationTotal : 0;
console.log(`Total projects: ${this.totalProjects}`);
}
} catch (error) {
console.error('Error fetching total project count:', error);
}
}

async fetchProjects(): Promise<Project[]> {
await this.ensureTotalProjectsFetched();

if (this.loading || this.noMoreProjects) {
return [];
}
this.totalProjectsFetched = true;

this.loading = true;
const indexerUrl = this.indexerService.getPrimaryIndexer('testnet');
if (!indexerUrl) {
this.loading = false;
throw new Error('No primary indexer found for testnet');
}

if (this.projects.length >= this.totalProjects || this.offset < 0) {
this.noMoreProjects = true;
this.loading = false;
return [];
}

const url = `${indexerUrl}api/query/Angor/projects?offset=${this.offset}&limit=${this.limit}`;
console.log(`Fetching projects from URL: ${url}`);
this.offset = Math.max(this.totalProjects - this.limit, 0);
}

try {
const newProjects = await this.http
.get<Project[]>(url)
.pipe(
catchError((error) => {
console.error('Error fetching projects:', error);
return of([]);
})
)
.toPromise();
const newProjects = response?.body || [];
console.log('New projects received:', newProjects);

if (!newProjects || newProjects.length === 0) {
this.noMoreProjects = true;
Expand All @@ -110,13 +77,14 @@ export class ProjectsService {
(newProject) =>
!this.projects.some(
(existingProject) =>
existingProject.projectIdentifier ===
newProject.projectIdentifier
existingProject.projectIdentifier === newProject.projectIdentifier
)
);

if (uniqueNewProjects.length > 0) {
this.projects = [...this.projects, ...uniqueNewProjects];
console.log(`${uniqueNewProjects.length} new projects added`);

this.offset = Math.max(this.offset - this.limit, 0);
return uniqueNewProjects;
} else {
Expand All @@ -132,13 +100,9 @@ export class ProjectsService {
}
}


fetchProjectStats(projectIdentifier: string): Observable<ProjectStats> {
const indexerUrl = this.indexerService.getPrimaryIndexer('testnet');
if (!indexerUrl) {
console.error('No primary indexer found for testnet');
return of({} as ProjectStats);
}

const url = `${indexerUrl}api/query/Angor/projects/${projectIdentifier}/stats`;
console.log(`Fetching project stats from URL: ${url}`);

Expand All @@ -155,11 +119,6 @@ export class ProjectsService {

fetchProjectDetails(projectIdentifier: string): Observable<Project> {
const indexerUrl = this.indexerService.getPrimaryIndexer('testnet');
if (!indexerUrl) {
console.error('No primary indexer found for testnet');
return of({} as Project);
}

const url = `${indexerUrl}api/query/Angor/projects/${projectIdentifier}`;
console.log(`Fetching project details from URL: ${url}`);

Expand All @@ -178,11 +137,10 @@ export class ProjectsService {
return this.projects;
}

async resetProjects(): Promise<void> {
await this.ensureTotalProjectsFetched();

resetProjects(): void {
this.projects = [];
this.noMoreProjects = false;
this.offset = Math.max(this.totalProjects - this.limit, 0);
this.offset = 0;
this.totalProjectsFetched = false;
}
}

0 comments on commit aeffce3

Please sign in to comment.