@@ -3,19 +3,20 @@ import {
3
3
Component ,
4
4
ComponentRef ,
5
5
ContentChildren ,
6
+ DestroyRef ,
6
7
ElementRef ,
7
8
HostBinding ,
9
+ inject ,
8
10
Injector ,
9
11
Input ,
10
12
NgModuleRef ,
11
- OnDestroy ,
12
13
OnInit ,
13
14
QueryList ,
14
15
Renderer2 ,
15
16
ViewChild ,
16
17
ViewContainerRef
17
18
} from '@angular/core' ;
18
- import { Subscription } from 'rxjs' ;
19
+ import { takeUntilDestroyed } from '@angular/core/ rxjs-interop ' ;
19
20
20
21
import { IToasterAction , ToasterService } from './toaster.service' ;
21
22
import { ToasterHostDirective } from './toaster-host.directive' ;
@@ -54,9 +55,16 @@ export type TToasterPlacement =
54
55
standalone :true ,
55
56
imports :[ ToasterHostDirective ]
56
57
} )
57
- export class ToasterComponent implements OnDestroy , OnInit , AfterContentChecked {
58
+ export class ToasterComponent implements OnInit , AfterContentChecked {
59
+
60
+ readonly #destroyRef= inject ( DestroyRef ) ;
61
+
62
+ constructor (
63
+ private hostElement :ElementRef ,
64
+ private renderer :Renderer2 ,
65
+ private toasterService :ToasterService
66
+ ) { }
58
67
59
- stateToasterSubscription ! :Subscription ;
60
68
placements = Object . values ( ToasterPlacement ) ;
61
69
toasts ! :QueryList < ViewContainerRef > ;
62
70
toastsDynamic :any [ ] = [ ] ;
@@ -76,12 +84,6 @@ export class ToasterComponent implements OnDestroy, OnInit, AfterContentChecked
76
84
@ViewChild ( ToasterHostDirective , { static :true } ) toasterHost ! :ToasterHostDirective ;
77
85
@ContentChildren ( ToastComponent , { read :ViewContainerRef } ) contentToasts ! :QueryList < ViewContainerRef > ;
78
86
79
- constructor (
80
- private hostElement :ElementRef ,
81
- private renderer :Renderer2 ,
82
- private toasterService :ToasterService
83
- ) { }
84
-
85
87
@HostBinding ( 'class' )
86
88
get hostClasses ( ) :any {
87
89
return {
@@ -101,18 +103,14 @@ export class ToasterComponent implements OnDestroy, OnInit, AfterContentChecked
101
103
}
102
104
103
105
ngOnInit ( ) :void {
104
- this . stateToasterSubscribe ( true ) ;
105
- }
106
-
107
- ngOnDestroy ( ) :void {
108
- this . stateToasterSubscribe ( false ) ;
106
+ this . stateToasterSubscribe ( ) ;
109
107
}
110
108
111
109
ngAfterContentChecked ( ) :void {
112
110
this . toasts = this . contentToasts ;
113
111
}
114
112
115
- addToast ( toast :any , props :any , options ?:{
113
+ public addToast ( toast :any , props :any , options ?:{
116
114
index ?:number ;
117
115
injector ?:Injector ;
118
116
ngModuleRef ?:NgModuleRef < unknown > ;
@@ -133,7 +131,7 @@ export class ToasterComponent implements OnDestroy, OnInit, AfterContentChecked
133
131
return componentRef ;
134
132
}
135
133
136
- removeToast ( state :IToasterAction ) :void {
134
+ public removeToast ( state :IToasterAction ) :void {
137
135
this . toastsDynamic ?. forEach ( item => {
138
136
if ( state . toast ?. dynamic && ( item . instance === state . toast ) ) {
139
137
item . instance . visible = false ;
@@ -143,26 +141,25 @@ export class ToasterComponent implements OnDestroy, OnInit, AfterContentChecked
143
141
} ) ;
144
142
145
143
this . toasts ?. forEach ( item => {
146
- if ( item . element . nativeElement === state . toast ?. hostElement . nativeElement ) {
147
- if ( ! state . toast ?. dynamic ) {
148
- //@ts -ignore
144
+ if ( state . toast && ( item . element . nativeElement === state . toast . hostElement . nativeElement ) ) {
145
+ if ( ! state . toast . dynamic ) {
149
146
state . toast . visible = false ;
150
147
}
151
148
}
152
149
} ) ;
153
150
}
154
151
155
- private stateToasterSubscribe ( subscribe :boolean = true ) :void {
156
- if ( subscribe ) {
157
- this . stateToasterSubscription = this . toasterService . toasterState$ . subscribe ( ( state ) => {
152
+ private stateToasterSubscribe ( ) :void {
153
+ this . toasterService . toasterState$
154
+ . pipe (
155
+ takeUntilDestroyed ( this . #destroyRef)
156
+ )
157
+ . subscribe ( ( state ) => {
158
158
if ( state . show === false ) {
159
159
this . removeToast ( state ) ;
160
160
}
161
161
if ( state . show === true && state . toast ?. dynamic === undefined ) {
162
162
}
163
163
} ) ;
164
- } else {
165
- this . stateToasterSubscription ?. unsubscribe ( ) ;
166
- }
167
164
}
168
165
}