Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitca10625

Browse files
committed
refactor(toaster): move to takeUntilDestroyed(), cleanup
1 parent416c4d9 commitca10625

File tree

1 file changed

+23
-26
lines changed

1 file changed

+23
-26
lines changed

‎projects/coreui-angular/src/lib/toast/toaster/toaster.component.ts‎

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,20 @@ import {
33
Component,
44
ComponentRef,
55
ContentChildren,
6+
DestroyRef,
67
ElementRef,
78
HostBinding,
9+
inject,
810
Injector,
911
Input,
1012
NgModuleRef,
11-
OnDestroy,
1213
OnInit,
1314
QueryList,
1415
Renderer2,
1516
ViewChild,
1617
ViewContainerRef
1718
}from'@angular/core';
18-
import{Subscription}from'rxjs';
19+
import{takeUntilDestroyed}from'@angular/core/rxjs-interop';
1920

2021
import{IToasterAction,ToasterService}from'./toaster.service';
2122
import{ToasterHostDirective}from'./toaster-host.directive';
@@ -54,9 +55,16 @@ export type TToasterPlacement =
5455
standalone:true,
5556
imports:[ToasterHostDirective]
5657
})
57-
exportclassToasterComponentimplementsOnDestroy,OnInit,AfterContentChecked{
58+
exportclassToasterComponentimplementsOnInit,AfterContentChecked{
59+
60+
readonly #destroyRef=inject(DestroyRef);
61+
62+
constructor(
63+
privatehostElement:ElementRef,
64+
privaterenderer:Renderer2,
65+
privatetoasterService:ToasterService
66+
){}
5867

59-
stateToasterSubscription!:Subscription;
6068
placements=Object.values(ToasterPlacement);
6169
toasts!:QueryList<ViewContainerRef>;
6270
toastsDynamic:any[]=[];
@@ -76,12 +84,6 @@ export class ToasterComponent implements OnDestroy, OnInit, AfterContentChecked
7684
@ViewChild(ToasterHostDirective,{static:true})toasterHost!:ToasterHostDirective;
7785
@ContentChildren(ToastComponent,{read:ViewContainerRef})contentToasts!:QueryList<ViewContainerRef>;
7886

79-
constructor(
80-
privatehostElement:ElementRef,
81-
privaterenderer:Renderer2,
82-
privatetoasterService:ToasterService
83-
){}
84-
8587
@HostBinding('class')
8688
gethostClasses():any{
8789
return{
@@ -101,18 +103,14 @@ export class ToasterComponent implements OnDestroy, OnInit, AfterContentChecked
101103
}
102104

103105
ngOnInit():void{
104-
this.stateToasterSubscribe(true);
105-
}
106-
107-
ngOnDestroy():void{
108-
this.stateToasterSubscribe(false);
106+
this.stateToasterSubscribe();
109107
}
110108

111109
ngAfterContentChecked():void{
112110
this.toasts=this.contentToasts;
113111
}
114112

115-
addToast(toast:any,props:any,options?:{
113+
publicaddToast(toast:any,props:any,options?:{
116114
index?:number;
117115
injector?:Injector;
118116
ngModuleRef?:NgModuleRef<unknown>;
@@ -133,7 +131,7 @@ export class ToasterComponent implements OnDestroy, OnInit, AfterContentChecked
133131
returncomponentRef;
134132
}
135133

136-
removeToast(state:IToasterAction):void{
134+
publicremoveToast(state:IToasterAction):void{
137135
this.toastsDynamic?.forEach(item=>{
138136
if(state.toast?.dynamic&&(item.instance===state.toast)){
139137
item.instance.visible=false;
@@ -143,26 +141,25 @@ export class ToasterComponent implements OnDestroy, OnInit, AfterContentChecked
143141
});
144142

145143
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){
149146
state.toast.visible=false;
150147
}
151148
}
152149
});
153150
}
154151

155-
privatestateToasterSubscribe(subscribe:boolean=true):void{
156-
if(subscribe){
157-
this.stateToasterSubscription=this.toasterService.toasterState$.subscribe((state)=>{
152+
privatestateToasterSubscribe():void{
153+
this.toasterService.toasterState$
154+
.pipe(
155+
takeUntilDestroyed(this.#destroyRef)
156+
)
157+
.subscribe((state)=>{
158158
if(state.show===false){
159159
this.removeToast(state);
160160
}
161161
if(state.show===true&&state.toast?.dynamic===undefined){
162162
}
163163
});
164-
}else{
165-
this.stateToasterSubscription?.unsubscribe();
166-
}
167164
}
168165
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp