Skip to content

Commit 3a91eaf

Browse files
committed
build: removed rxjs usage from component, it was useless
1 parent f922ba1 commit 3a91eaf

File tree

1 file changed

+44
-57
lines changed

1 file changed

+44
-57
lines changed
Lines changed: 44 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,64 @@
11
import {
2-
AfterViewInit,
3-
Component,
4-
EventEmitter,
5-
Inject,
6-
Input,
7-
OnDestroy,
8-
OnInit,
9-
Output,
10-
PLATFORM_ID,
2+
AfterViewInit,
3+
Component,
4+
EventEmitter,
5+
Inject,
6+
Input,
7+
OnDestroy,
8+
OnInit,
9+
Output,
10+
PLATFORM_ID,
1111
} from '@angular/core';
1212
import { isPlatformServer } from '@angular/common';
13-
import { from, mergeMap, Subject, Subscription, takeUntil } from 'rxjs';
1413
import { tsParticles } from '@tsparticles/engine';
15-
import type { Container, Engine } from '@tsparticles/engine';
14+
import type { Container } from '@tsparticles/engine';
1615
import { IParticlesProps } from './ng-particles.module';
1716
import { NgParticlesService } from './ng-particles.service';
1817

1918
@Component({
20-
selector: 'ngx-particles',
21-
template: '<div [id]="id"></div>',
19+
selector: 'ngx-particles',
20+
template: '<div [id]="id"></div>',
2221
})
2322
export class NgxParticlesComponent implements OnInit, AfterViewInit, OnDestroy {
24-
@Input() options?: IParticlesProps;
25-
@Input() url?: string;
26-
@Input() id: string;
27-
@Input() particlesInit?: (engine: Engine) => Promise<void>;
28-
@Output() particlesLoaded: EventEmitter<Container> = new EventEmitter<Container>();
23+
@Input() options?: IParticlesProps;
24+
@Input() url?: string;
25+
@Input() id: string;
26+
@Output() particlesLoaded: EventEmitter<Container> = new EventEmitter<Container>();
2927

30-
private subscription?: Subscription;
31-
private destroy$ = new Subject<void>();
32-
private container?: Container;
28+
private container?: Container;
3329

34-
constructor(
35-
@Inject(PLATFORM_ID) protected platformId: string,
36-
private readonly particlesService: NgParticlesService,
37-
) {
38-
this.id = 'tsparticles';
39-
}
30+
constructor(
31+
@Inject(PLATFORM_ID) protected platformId: string,
32+
private readonly particlesService: NgParticlesService,
33+
) {
34+
this.id = 'tsparticles';
35+
}
4036

41-
public ngOnInit() {
42-
this.subscription = this.particlesService.getInstallationStatus().subscribe(() => {
43-
this.container?.destroy();
44-
this.loadParticles();
45-
});
37+
public async ngOnInit() {
38+
if (await this.particlesService.getInstallationStatus()) {
39+
this.container?.destroy();
40+
this.loadParticles();
4641
}
42+
}
4743

48-
public ngAfterViewInit(): void {
49-
if (isPlatformServer(this.platformId)) {
50-
return;
51-
}
52-
53-
this.loadParticles();
44+
public ngAfterViewInit(): void {
45+
if (isPlatformServer(this.platformId)) {
46+
return;
5447
}
5548

56-
public ngOnDestroy(): void {
57-
this.container?.destroy();
58-
this.subscription?.unsubscribe();
59-
this.destroy$.next();
60-
}
49+
this.loadParticles();
50+
}
6151

62-
private loadParticles(): void {
63-
const cb = (container?: Container) => {
64-
this.container = container;
65-
this.particlesLoaded.emit(container);
66-
};
52+
public ngOnDestroy(): void {
53+
this.container?.destroy();
54+
}
6755

68-
from(this.particlesInit ? this.particlesInit(tsParticles) : Promise.resolve())
69-
.pipe(
70-
mergeMap(() => {
71-
return tsParticles.load({ id: this.id, url: this.url, options: this.options });
72-
}),
73-
takeUntil(this.destroy$),
74-
)
75-
.subscribe(cb);
76-
}
56+
private loadParticles(): void {
57+
tsParticles.load({ id: this.id, url: this.url, options: this.options })
58+
.then(container => {
59+
this.container = container;
60+
this.particlesLoaded.emit(container);
61+
})
62+
.catch(error => console.error(error));
63+
}
7764
}

0 commit comments

Comments
 (0)