Skip to content

Commit

Permalink
perf: run change detection locally when emoji is mouse left (#366)
Browse files Browse the repository at this point in the history
  • Loading branch information
arturovt authored Jan 31, 2021
1 parent 7fb737f commit b57f51b
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions src/lib/picker/picker.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export class PickerComponent implements OnInit, OnDestroy {
firstRender = true;
recent?: string[];
previewEmoji: any;
leaveTimeout: any;
animationFrameRequestId: number | null = null;
NAMESPACE = 'emoji-mart';
measureScrollbar = 0;
RECENT_CATEGORY: EmojiCategory = {
Expand Down Expand Up @@ -262,8 +262,8 @@ export class PickerComponent implements OnInit, OnDestroy {
this.categories[categoriesToLoadFirst - 1].emojis = lastActiveCategoryEmojis;
this.setActiveCategories(this.categories);
// The `setTimeout` will trigger the change detection, but since we're inside
// the OnPush component we can change detection locally starting from this one and
// down to bottom.
// the OnPush component we can run change detection locally starting from this
// component and going down to the children.
this.ref.detectChanges();

this.ngZone.runOutsideAngular(() => {
Expand All @@ -289,6 +289,11 @@ export class PickerComponent implements OnInit, OnDestroy {

ngOnDestroy(): void {
this.scrollListener();
// This is called here because the component might be destroyed
// but there will still be a `requestAnimationFrame` callback in the queue
// that calls `detectChanges()` on the `ViewRef`. This will lead to a runtime
// exception if the `detectChanges()` is called after the `ViewRef` is destroyed.
this.cancelAnimationFrame();
}

setActiveCategories(categoriesToMakeActive: Array<EmojiCategory>) {
Expand Down Expand Up @@ -433,17 +438,19 @@ export class PickerComponent implements OnInit, OnDestroy {
}

this.previewEmoji = $event.emoji;
clearTimeout(this.leaveTimeout);
this.cancelAnimationFrame();
}
handleEmojiLeave() {
if (!this.showPreview || !this.previewRef) {
return;
}

this.leaveTimeout = setTimeout(() => {
this.previewEmoji = null;
this.previewRef!.ref.markForCheck();
}, 16);
this.ngZone.runOutsideAngular(() => {
this.animationFrameRequestId = requestAnimationFrame(() => {
this.previewEmoji = null;
this.ref.detectChanges();
});
});
}
handleEmojiClick($event: EmojiEvent) {
this.emojiClick.emit($event);
Expand All @@ -461,4 +468,11 @@ export class PickerComponent implements OnInit, OnDestroy {
}
return this.perLine * (this.emojiSize + 12) + 12 + 2 + this.measureScrollbar + 'px';
}

private cancelAnimationFrame(): void {
if (this.animationFrameRequestId !== null) {
cancelAnimationFrame(this.animationFrameRequestId);
this.animationFrameRequestId = null;
}
}
}

0 comments on commit b57f51b

Please sign in to comment.