Skip to content

Commit ac7e354

Browse files
committed
Located the cause of high cpu usage, the UiImageLoader/ImageLoader directive
1 parent 6d4f61b commit ac7e354

File tree

9 files changed

+15
-108
lines changed

9 files changed

+15
-108
lines changed

src/app/book/book.module.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ import { ContentSearchDirective } from './directives/content-search.directive';
4141

4242
import { environment } from '../../environments/environment';
4343
import { config } from '../gbr.config';
44-
import { UiImageLoaderDirective } from './directives/ui-image-loader.directive';
4544

4645
@NgModule({
4746
imports: [
@@ -62,7 +61,6 @@ import { UiImageLoaderDirective } from './directives/ui-image-loader.directive';
6261
ElementInputDirective,
6362
ContentSearchDirective,
6463
SafeHtmlPipe,
65-
UiImageLoaderDirective,
6664
],
6765
providers: [
6866
SettingsService,
@@ -90,7 +88,6 @@ import { UiImageLoaderDirective } from './directives/ui-image-loader.directive';
9088
ElementInputDirective,
9189
SafeHtmlPipe,
9290
ContentSearchDirective,
93-
UiImageLoaderDirective,
9491
]
9592
})
9693
export class BookPageModule {

src/app/home/components/book-list.component.html

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@
1313
<!-- 作者和来源 -->
1414
<mat-card-header>
1515
<img mat-card-avatar
16-
uiImageLoader
17-
onErrorSrc="assets/images/avatar.png"
18-
[src]="book.isFromMainstreamPlatform ? book.writer.avatarUrl : 'assets/images/avatar.png'"
19-
/>
16+
[src]="book.isFromMainstreamPlatform ? book.writer.avatarUrl : 'assets/images/avatar.png'" />
2017
<mat-card-title>
2118
{{book.writer.fullName.length > 0 ? book.writer.fullName : book.writer.name}}
2219
</mat-card-title>
@@ -28,13 +25,6 @@
2825

2926
<mat-card-content *ngIf="book.desc.length > 0" class="description">
3027
<mat-label><b></b>{{book.desc.length > 256 ? book.desc.slice(0,256)+'...' : book.desc}}<b></b></mat-label>
31-
<button class="readme-button" mat-button
32-
*ngIf="book.downloaded"
33-
matTooltip="{{'home.bookList.tooltip.readme' | translate}}"
34-
(click)="openReadmeDialog(book)"
35-
size="small">
36-
<ion-icon name="logo-markdown"></ion-icon>README.md
37-
</button>
3828
</mat-card-content>
3929

4030
<mat-card-content fxLayout="row" fxLayoutGap="5px" class="cate-list">

src/app/home/components/book-list.component.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import {
22
Component,
33
OnInit,
44
OnChanges,
5-
ChangeDetectorRef,
65
SimpleChanges,
76
Input
87
} from '@angular/core';
@@ -21,7 +20,6 @@ import { OpMessageService } from '../services/op-message.service';
2120

2221
import { DeleteBookDialog } from './delete-book-dialog.component';
2322
import { EditBookCateListDialog } from './edit-book-cate-list.component';
24-
import { ReadmeDialog } from './readme-dialog.component';
2523
import { ReadingRecordDialog } from './reading-record-dialog.component';
2624

2725
import {
@@ -56,7 +54,6 @@ export class BookListComponent implements OnInit, OnChanges {
5654
private dialog: MatDialog,
5755
private book: BookService,
5856
private i18n: I18nService,
59-
private cdr: ChangeDetectorRef,
6057
private opMessage: OpMessageService
6158
) {}
6259

@@ -122,13 +119,6 @@ export class BookListComponent implements OnInit, OnChanges {
122119
console.log(cate)
123120
}
124121

125-
openReadmeDialog = (book: Book) => {
126-
this.dialog.open(ReadmeDialog, {
127-
width: '640px',
128-
data: book
129-
});
130-
}
131-
132122
openBook = (book: Book) => {
133123
this.book.open(book);
134124
}

src/app/home/components/readme-dialog.component.ts

Lines changed: 0 additions & 53 deletions
This file was deleted.

src/app/home/components/readme.dialog.html

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/app/home/components/readme.dialog.scss

Lines changed: 0 additions & 6 deletions
This file was deleted.

src/app/book/directives/ui-image-loader.directive.ts renamed to src/app/home/directives/image-loader.directive.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,29 @@ import {
33
Attribute,
44
Renderer2,
55
ElementRef,
6-
HostListener } from '@angular/core';
6+
HostListener
7+
} from '@angular/core';
78

89
@Directive({
9-
selector: '[uiImageLoader]'
10+
selector: '[imageLoader]'
1011
})
11-
export class UiImageLoaderDirective {
12+
export class ImageLoaderDirective {
1213
private loader = 'assets/images/loader.svg';
1314
constructor(
1415
@Attribute('onErrorSrc') public onErrorSrc: string,
1516
private renderer: Renderer2,
16-
private el: ElementRef) {
17+
private el: ElementRef
18+
) {
1719
this.renderer.setAttribute(this.el.nativeElement, 'src', this.loader);
1820
}
1921

20-
@HostListener('load') onLoad() {
22+
@HostListener('load')
23+
onLoad() {
2124
this.renderer.setAttribute(this.el.nativeElement, 'src', this.el.nativeElement.src);
2225
}
23-
@HostListener('error') onError() {
26+
27+
@HostListener('error')
28+
onError() {
2429
this.renderer.setAttribute(this.el.nativeElement, 'src', this.onErrorSrc);
2530
}
2631
}

src/app/home/home.module.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,11 @@ import { MaterialModule } from '../material.module';
99
import { MglTimelineModule } from 'angular-mgl-timeline';
1010

1111
import { I18nModule } from '../i18n/i18n.module';
12-
import { BookPageModule } from '../book/book.module';
1312
import { BookListComponent } from './components/book-list.component';
1413
import { NewBookDialog } from './components/new-book-dialog.component';
1514
import { DeleteBookDialog } from './components/delete-book-dialog.component';
1615
import { SnackbarComponent } from './components/snackbar.component';
1716
import { EditBookCateListDialog } from './components/edit-book-cate-list.component';
18-
import { ReadmeDialog } from './components/readme-dialog.component';
1917
import { ReadingRecordDialog } from './components/reading-record-dialog.component';
2018
import { SearchComponent } from './components/search.component';
2119

@@ -32,14 +30,14 @@ import { HomePageRoutingModule } from './home-routing.module';
3230
import { HomePage } from './home.page';
3331
import { ReadableDatePipe } from './pipes/readable-date.pipe';
3432
import { StringToDatePipe } from './pipes/string-to-date.pipe';
33+
import { ImageLoaderDirective } from './directives/image-loader.directive';
3534

3635
@NgModule({
3736
imports: [
3837
CommonModule,
3938
FormsModule,
4039
IonicModule,
4140
I18nModule,
42-
BookPageModule,
4341
MglTimelineModule,
4442
MaterialModule,
4543
HomePageRoutingModule
@@ -49,12 +47,12 @@ import { StringToDatePipe } from './pipes/string-to-date.pipe';
4947
NewBookDialog,
5048
DeleteBookDialog,
5149
EditBookCateListDialog,
52-
ReadmeDialog,
5350
ReadingRecordDialog,
5451
SnackbarComponent,
5552
SearchComponent,
5653
ReadableDatePipe,
57-
StringToDatePipe
54+
StringToDatePipe,
55+
ImageLoaderDirective
5856
],
5957
providers: [
6058
BookService,

src/app/home/home.page.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import {
22
Component,
3-
ChangeDetectorRef,
43
OnInit,
54
AfterViewInit
65
} from '@angular/core';
@@ -108,7 +107,6 @@ export class HomePage implements OnInit, AfterViewInit {
108107
private message: MessageService,
109108
private fetchService: FetchService,
110109
private i18n: I18nService,
111-
private cdr: ChangeDetectorRef,
112110
private book: BookService,
113111
) {
114112
this.bookList = book.list;
@@ -179,8 +177,6 @@ export class HomePage implements OnInit, AfterViewInit {
179177

180178
this.message.getMessage().subscribe((_: IMessage) => {
181179
if(_.event === 'book-list-updated') this.bookList = (_.data as Book[]).slice();
182-
183-
this.cdr.detectChanges();
184180
});
185181

186182
this.crud.ipcRenderer.on('error-occured', (ev, book: Book) => {
@@ -202,8 +198,6 @@ export class HomePage implements OnInit, AfterViewInit {
202198
if(index < 0){
203199
this.downloadingList.push(msg.book.id);
204200
}
205-
206-
this.cdr.detectChanges();
207201
});
208202

209203
this.crud.ipcRenderer.on('book-downloaded', (ev, msg: IProgressMessage) => {

0 commit comments

Comments
 (0)