diff --git a/apps/performance/christmas-web-worker/src/app/app.component.ts b/apps/performance/christmas-web-worker/src/app/app.component.ts index a7e757f..ba55d27 100644 --- a/apps/performance/christmas-web-worker/src/app/app.component.ts +++ b/apps/performance/christmas-web-worker/src/app/app.component.ts @@ -1,12 +1,12 @@ import { CommonModule } from '@angular/common'; import { Component, inject } from '@angular/core'; -import { PrimeService } from './prime-number.service'; +import { HeavyCalculationService } from './heavy-calculation.service'; import { UnknownPersonComponent } from './unknown-person/unknown-person.component'; @Component({ standalone: true, imports: [CommonModule, UnknownPersonComponent], - providers: [PrimeService], + providers: [HeavyCalculationService], selector: 'app-root', template: ` @@ -22,11 +22,11 @@ import { UnknownPersonComponent } from './unknown-person/unknown-person.componen }, }) export class AppComponent { - primeService = inject(PrimeService); + private heavyCalculationService = inject(HeavyCalculationService); - loadingPercentage = this.primeService.loadingPercentage; + readonly loadingPercentage = this.heavyCalculationService.loadingPercentage; discover() { - this.primeService.calculatePrimeLength(); + this.heavyCalculationService.startLoading(); } } diff --git a/apps/performance/christmas-web-worker/src/app/app.config.ts b/apps/performance/christmas-web-worker/src/app/app.config.ts deleted file mode 100644 index 81a6edd..0000000 --- a/apps/performance/christmas-web-worker/src/app/app.config.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { ApplicationConfig } from '@angular/core'; - -export const appConfig: ApplicationConfig = { - providers: [], -}; diff --git a/apps/performance/christmas-web-worker/src/app/heavy-calculation.service.ts b/apps/performance/christmas-web-worker/src/app/heavy-calculation.service.ts new file mode 100644 index 0000000..84ce1e6 --- /dev/null +++ b/apps/performance/christmas-web-worker/src/app/heavy-calculation.service.ts @@ -0,0 +1,30 @@ +import { Injectable, computed, signal } from '@angular/core'; + +@Injectable() +export class HeavyCalculationService { + private finalLength = 664579; + private loadingLength = signal(0); + + loadingPercentage = computed( + () => (this.loadingLength() * 100) / this.finalLength + ); + + startLoading() { + this.randomHeavyCalculalationFunction(); + } + + private randomHeavyCalculalationFunction() { + for (let num = 2; num <= 10000000; num++) { + let randomFlag = true; + for (let i = 2; i <= Math.sqrt(num); i++) { + if (num % i === 0) { + randomFlag = false; + break; + } + } + if (randomFlag) { + this.loadingLength.update((l) => l + 1); + } + } + } +} diff --git a/apps/performance/christmas-web-worker/src/app/prime-number.service.ts b/apps/performance/christmas-web-worker/src/app/prime-number.service.ts deleted file mode 100644 index 9ef52fa..0000000 --- a/apps/performance/christmas-web-worker/src/app/prime-number.service.ts +++ /dev/null @@ -1,30 +0,0 @@ -import { Injectable, computed, signal } from '@angular/core'; - -@Injectable() -export class PrimeService { - private finalResult = 664579; - private primesLength = signal(0); - - loadingPercentage = computed( - () => (this.primesLength() * 100) / this.finalResult - ); - - calculatePrimeLength() { - this.findPrimesUpToLimit(10000000); - } - - private findPrimesUpToLimit(limit: number) { - for (let num = 2; num <= limit; num++) { - let isPrime = true; - for (let i = 2; i <= Math.sqrt(num); i++) { - if (num % i === 0) { - isPrime = false; - break; - } - } - if (isPrime) { - this.primesLength.update((l) => l + 1); - } - } - } -} diff --git a/apps/performance/christmas-web-worker/src/main.ts b/apps/performance/christmas-web-worker/src/main.ts index 514c89a..31c5da4 100644 --- a/apps/performance/christmas-web-worker/src/main.ts +++ b/apps/performance/christmas-web-worker/src/main.ts @@ -1,7 +1,4 @@ import { bootstrapApplication } from '@angular/platform-browser'; -import { appConfig } from './app/app.config'; import { AppComponent } from './app/app.component'; -bootstrapApplication(AppComponent, appConfig).catch((err) => - console.error(err) -); +bootstrapApplication(AppComponent).catch((err) => console.error(err));