Skip to content
Open
Show file tree
Hide file tree
Changes from 7 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
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ examples.forEach((activeConversation) => {
let examplePost: Post;
const course = { id: 1 } as Course;

beforeAll(() => {
(window as any).ResizeObserver = class {
observe() {}
unobserve() {}
disconnect() {}
};
});

beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
imports: [FormsModule, ReactiveFormsModule, FaIconComponent],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,18 @@ export class ConversationMessagesComponent implements OnInit, AfterViewInit, OnD
childList: true,
subtree: true,
});

const resizeObserver = new ResizeObserver((event, observer) => {
const entry = event.first();
if (entry && entry.contentRect.height) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see you're using a ResizeObserver here to wait for the component to have a height before trying to restore the scroll position. That's a clever and robust way to handle this kind of race condition where layout isn't ready on ngAfterViewInit. I'll keep this pattern in mind for similar issues in the future.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see you're using a ResizeObserver here to wait for the component to have a height before trying to restore the scroll position. That's a clever and robust way to handle this kind of race condition where layout isn't ready on ngAfterViewInit. I'll keep this pattern in mind for similar issues in the future.

// Stop observing as soon as height is non-zero
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see you're using a ResizeObserver here to wait until the component has a non-zero height before trying to restore the scroll position. This is a neat way to solve the race condition where the scroll action might fire before the content is fully rendered and sized. I'll make a note of this pattern for handling layout-dependent actions after a view initializes.

observer.unobserve(el);
}
if (!this.createdNewMessage && this.posts.length > 0) {
this.scrollToStoredId();
}
});
resizeObserver.observe(el);
}

ngOnDestroy(): void {
Expand All @@ -264,6 +276,8 @@ export class ConversationMessagesComponent implements OnInit, AfterViewInit, OnD
}
if (savedScrollId) {
requestAnimationFrame(() => this.goToLastSelectedElement(savedScrollId, this.isOpenThreadOnFocus));
} else {
this.scrollToBottomOfMessages();
}
}

Expand Down
Loading