Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: avoid memeory leak in effects #515

Merged
merged 1 commit into from
Oct 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 19 additions & 20 deletions libs/ngu/carousel/src/lib/ngu-carousel/ngu-carousel.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
afterNextRender,
AfterRenderPhase
} from '@angular/core';
import { EMPTY, Subject, Subscription, fromEvent, interval, merge, timer } from 'rxjs';
import { EMPTY, Subject, fromEvent, interval, merge, timer } from 'rxjs';
import { debounceTime, filter, map, startWith, switchMap, takeUntil } from 'rxjs/operators';

import { IS_BROWSER } from '../symbols';
Expand Down Expand Up @@ -80,24 +80,24 @@ export class NguCarousel<T, U extends NgIterable<T> = NgIterable<T>>
readonly activePoint = signal(0);
readonly pointNumbers = signal<number[]>([]);

inputs = input.required<NguCarouselConfig>();
carouselLoad = output<number>();
onMove = output<this>();
readonly inputs = input.required<NguCarouselConfig>();
readonly carouselLoad = output<number>();
readonly onMove = output<this>();

private _defDirectives = contentChildren(NguCarouselDefDirective);

private _nodeOutlet = viewChild.required(NguCarouselOutlet);

nextButton = contentChild(NguCarouselNextDirective, { read: ElementRef });
prevButton = contentChild(NguCarouselPrevDirective, { read: ElementRef });
readonly nextButton = contentChild(NguCarouselNextDirective, { read: ElementRef });
readonly prevButton = contentChild(NguCarouselPrevDirective, { read: ElementRef });

carouselMain1 = viewChild.required('ngucarousel', { read: ElementRef });
_nguItemsContainer = viewChild.required('nguItemsContainer', { read: ElementRef });
_touchContainer = viewChild.required('touchContainer', { read: ElementRef });
readonly carouselMain1 = viewChild.required('ngucarousel', { read: ElementRef });
readonly _nguItemsContainer = viewChild.required('nguItemsContainer', { read: ElementRef });
readonly _touchContainer = viewChild.required('touchContainer', { read: ElementRef });

private _arrayChanges: IterableChanges<T> | null = null;

dataSource = input.required({
readonly dataSource = input.required({
transform: (v: NguCarouselDataSource<T, U>) => v || ([] as never)
});

Expand Down Expand Up @@ -125,8 +125,8 @@ export class NguCarousel<T, U extends NgIterable<T> = NgIterable<T>>
* relative to the function to know if a Items should be added/removed/moved.
* Accepts a function that takes two parameters, `index` and `item`.
*/
trackBy = input<TrackByFunction<T>>();
_trackByFn = computed(() => {
readonly trackBy = input<TrackByFunction<T>>();
readonly _trackByFn = computed(() => {
const fn = this.trackBy();
if (NG_DEV_MODE && fn != null && typeof fn !== 'function' && console?.warn) {
console.warn(`trackBy must be a function, but received ${JSON.stringify(fn)}.`);
Expand Down Expand Up @@ -165,27 +165,26 @@ export class NguCarousel<T, U extends NgIterable<T> = NgIterable<T>>
{ allowSignalWrites: true }
);

let preSub: Subscription;
effect(() => {
preSub?.unsubscribe();
effect(cleanup => {
const prevButton = this.prevButton();
untracked(() => {
if (prevButton) {
preSub = fromEvent(prevButton.nativeElement, 'click')
const preSub = fromEvent(prevButton.nativeElement, 'click')
.pipe(takeUntil(this._destroy$))
.subscribe(() => this._carouselScrollOne(0));
cleanup(() => preSub.unsubscribe());
}
});
});
let nextSub: Subscription;
effect(() => {
nextSub?.unsubscribe();

effect(cleanup => {
const nextButton = this.nextButton();
untracked(() => {
if (nextButton) {
nextSub = fromEvent(nextButton.nativeElement, 'click')
const nextSub = fromEvent(nextButton.nativeElement, 'click')
.pipe(takeUntil(this._destroy$))
.subscribe(() => this._carouselScrollOne(1));
cleanup(() => nextSub.unsubscribe());
}
});
});
Expand Down