1
- import { AfterViewInit , Component , EventEmitter , Inject , Input , OnDestroy , Output , PLATFORM_ID } from '@angular/core' ;
1
+ import {
2
+ AfterViewInit ,
3
+ Component ,
4
+ EventEmitter ,
5
+ Inject ,
6
+ Input ,
7
+ OnDestroy ,
8
+ OnInit ,
9
+ Output ,
10
+ PLATFORM_ID ,
11
+ } from '@angular/core' ;
2
12
import { isPlatformServer } from '@angular/common' ;
3
- import { from , mergeMap , Subject , takeUntil } from 'rxjs' ;
13
+ import { from , mergeMap , Subject , Subscription , takeUntil } from 'rxjs' ;
4
14
import { tsParticles } from '@tsparticles/engine' ;
5
15
import type { Container , Engine } from '@tsparticles/engine' ;
6
16
import { IParticlesProps } from './ng-particles.module' ;
17
+ import { NgParticlesService } from './ng-particles.service' ;
7
18
8
19
@Component ( {
9
20
selector : 'ngx-particles' ,
10
21
template : '<div [id]="id"></div>' ,
11
22
} )
12
- export class NgxParticlesComponent implements AfterViewInit , OnDestroy {
23
+ export class NgxParticlesComponent implements OnInit , AfterViewInit , OnDestroy {
13
24
@Input ( ) options ?: IParticlesProps ;
14
25
@Input ( ) url ?: string ;
15
26
@Input ( ) id : string ;
16
27
@Input ( ) particlesInit ?: ( engine : Engine ) => Promise < void > ;
17
28
@Output ( ) particlesLoaded : EventEmitter < Container > = new EventEmitter < Container > ( ) ;
18
29
30
+ private subscription ?: Subscription ;
19
31
private destroy$ = new Subject < void > ( ) ;
20
32
private container ?: Container ;
21
33
22
- constructor ( @Inject ( PLATFORM_ID ) protected platformId : string ) {
34
+ constructor (
35
+ @Inject ( PLATFORM_ID ) protected platformId : string ,
36
+ private readonly particlesService : NgParticlesService ,
37
+ ) {
23
38
this . id = 'tsparticles' ;
24
39
}
25
40
41
+ public ngOnInit ( ) {
42
+ this . subscription = this . particlesService . getInstallationStatus ( ) . subscribe ( ( ) => {
43
+ this . container ?. destroy ( ) ;
44
+ this . loadParticles ( ) ;
45
+ } ) ;
46
+ }
47
+
26
48
public ngAfterViewInit ( ) : void {
27
49
if ( isPlatformServer ( this . platformId ) ) {
28
50
return ;
29
51
}
30
52
53
+ this . loadParticles ( ) ;
54
+ }
55
+
56
+ public ngOnDestroy ( ) : void {
57
+ this . container ?. destroy ( ) ;
58
+ this . subscription ?. unsubscribe ( ) ;
59
+ this . destroy$ . next ( ) ;
60
+ }
61
+
62
+ private loadParticles ( ) : void {
31
63
const cb = ( container ?: Container ) => {
32
64
this . container = container ;
33
65
this . particlesLoaded . emit ( container ) ;
@@ -42,9 +74,4 @@ export class NgxParticlesComponent implements AfterViewInit, OnDestroy {
42
74
)
43
75
. subscribe ( cb ) ;
44
76
}
45
-
46
- public ngOnDestroy ( ) : void {
47
- this . container ?. destroy ( ) ;
48
- this . destroy$ . next ( ) ;
49
- }
50
77
}
0 commit comments