|
1 | 1 | 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, |
11 | 11 | } from '@angular/core';
|
12 | 12 | import { isPlatformServer } from '@angular/common';
|
13 |
| -import { from, mergeMap, Subject, Subscription, takeUntil } from 'rxjs'; |
14 | 13 | import { tsParticles } from '@tsparticles/engine';
|
15 |
| -import type { Container, Engine } from '@tsparticles/engine'; |
| 14 | +import type { Container } from '@tsparticles/engine'; |
16 | 15 | import { IParticlesProps } from './ng-particles.module';
|
17 | 16 | import { NgParticlesService } from './ng-particles.service';
|
18 | 17 |
|
19 | 18 | @Component({
|
20 |
| - selector: 'ngx-particles', |
21 |
| - template: '<div [id]="id"></div>', |
| 19 | + selector: 'ngx-particles', |
| 20 | + template: '<div [id]="id"></div>', |
22 | 21 | })
|
23 | 22 | 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>(); |
29 | 27 |
|
30 |
| - private subscription?: Subscription; |
31 |
| - private destroy$ = new Subject<void>(); |
32 |
| - private container?: Container; |
| 28 | + private container?: Container; |
33 | 29 |
|
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 | + } |
40 | 36 |
|
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(); |
46 | 41 | }
|
| 42 | + } |
47 | 43 |
|
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; |
54 | 47 | }
|
55 | 48 |
|
56 |
| - public ngOnDestroy(): void { |
57 |
| - this.container?.destroy(); |
58 |
| - this.subscription?.unsubscribe(); |
59 |
| - this.destroy$.next(); |
60 |
| - } |
| 49 | + this.loadParticles(); |
| 50 | + } |
61 | 51 |
|
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 | + } |
67 | 55 |
|
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 | + } |
77 | 64 | }
|
0 commit comments