@@ -29,7 +29,7 @@ import { NzButtonModule } from "ng-zorro-antd/button";
2929import { NzIconModule } from "ng-zorro-antd/icon" ;
3030import { AppSettings } from "../../../common/app-setting" ;
3131import { Subject , Subscription } from "rxjs" ;
32- import { debounceTime , switchMap } from "rxjs/operators" ;
32+ import { debounceTime , finalize , switchMap , takeUntil } from "rxjs/operators" ;
3333
3434export interface HuggingFaceModelOption {
3535 id : string ;
@@ -155,6 +155,7 @@ export class HuggingFaceComponent extends FieldType<FieldTypeConfig> implements
155155 private readonly searchSubject$ = new Subject < string > ( ) ;
156156 private searchSubscription : Subscription | null = null ;
157157
158+ private readonly destroy$ = new Subject < void > ( ) ;
158159 private subscription : Subscription | null = null ;
159160 private taskPollInterval : ReturnType < typeof setInterval > | null = null ;
160161 private modelPollInterval : ReturnType < typeof setInterval > | null = null ;
@@ -183,6 +184,8 @@ export class HuggingFaceComponent extends FieldType<FieldTypeConfig> implements
183184 }
184185
185186 ngOnDestroy ( ) : void {
187+ this . destroy$ . next ( ) ;
188+ this . destroy$ . complete ( ) ;
186189 this . subscription ?. unsubscribe ( ) ;
187190 this . searchSubscription ?. unsubscribe ( ) ;
188191 this . searchSubject$ . complete ( ) ;
@@ -240,6 +243,16 @@ export class HuggingFaceComponent extends FieldType<FieldTypeConfig> implements
240243
241244 tasksFetchSubscription = this . http
242245 . get < HuggingFaceTaskOption [ ] > ( `${ AppSettings . getApiEndpoint ( ) } /huggingface/tasks` )
246+ . pipe (
247+ takeUntil ( this . destroy$ ) ,
248+ finalize ( ( ) => {
249+ // If takeUntil fires before next/error, reset the module-level guard
250+ // so the next component instance can start a fresh fetch.
251+ if ( cachedTaskOptions === null && tasksFetchError === null ) {
252+ tasksFetchSubscription = null ;
253+ }
254+ } )
255+ )
243256 . subscribe ( {
244257 next : tasks => {
245258 tasksFetchSubscription = null ;
@@ -343,9 +356,11 @@ export class HuggingFaceComponent extends FieldType<FieldTypeConfig> implements
343356 this . cdr . detectChanges ( ) ;
344357
345358 this . subscription = this . http
346- . get <
347- HuggingFaceModelOption [ ]
348- > ( `${ AppSettings . getApiEndpoint ( ) } /huggingface/models?task=${ encodeURIComponent ( tag ) } ` , { observe : "response" } )
359+ . get < HuggingFaceModelOption [ ] > (
360+ `${ AppSettings . getApiEndpoint ( ) } /huggingface/models?task=${ encodeURIComponent ( tag ) } ` ,
361+ { observe : "response" }
362+ )
363+ . pipe ( takeUntil ( this . destroy$ ) )
349364 . subscribe ( {
350365 next : resp => {
351366 const models = resp . body ?? [ ] ;
@@ -423,7 +438,8 @@ export class HuggingFaceComponent extends FieldType<FieldTypeConfig> implements
423438 return this . http . get < HuggingFaceModelOption [ ] > (
424439 `${ AppSettings . getApiEndpoint ( ) } /huggingface/models?task=${ encodeURIComponent ( tag ) } &search=${ encodeURIComponent ( query ) } `
425440 ) ;
426- } )
441+ } ) ,
442+ takeUntil ( this . destroy$ )
427443 )
428444 . subscribe ( {
429445 next : models => {
0 commit comments