Skip to content
This repository has been archived by the owner on Apr 26, 2020. It is now read-only.

Commit

Permalink
feat: web channel run in angular zone
Browse files Browse the repository at this point in the history
Change-Id: Ie0ae2aa16fa53e1e2c50ba7b88d9cff004b9e531
  • Loading branch information
myml committed Dec 20, 2018
1 parent c331ff8 commit 3555abc
Show file tree
Hide file tree
Showing 14 changed files with 118 additions and 121 deletions.
3 changes: 0 additions & 3 deletions src/web/src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@ export class AppComponent implements OnInit {
}

switchTheme() {
console.log('test');
this.themeService.getTheme().subscribe(console.log);
this.themeService.getTheme().subscribe(theme => {
console.log('getTheme', theme);
document.body.className = theme;
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
<button routerLink="../local" routerLinkActive="active">
<ng-container i18n>Local App</ng-container>
</button>
<button routerLink="../remote" routerLinkActive="active">
<button
*ngIf="(logged | async)"
routerLink="../remote"
routerLinkActive="active"
>
<ng-container i18n>Remote App</ng-container>
</button>
</dstore-center-title>
Expand All @@ -16,9 +20,7 @@
dstoreResize
(resize)="listHeight$.next($event.height)"
>
<ng-container
*ngIf="(apps$ | async) as installedList; else: loadingContainer"
>
<ng-container *ngIf="(apps$ | async) as installedList; else loadingContainer">
<ng-container *ngFor="let installed of installedList">
<div class="installed-app">
<img
Expand All @@ -41,7 +43,7 @@
>
</div>
</ng-template>
<ng-container *ngIf="select != installed.name; else: confirm">
<ng-container *ngIf="select != installed.name; else confirm">
<div class="time">
<span i18n>Date installed:</span>
{{
Expand All @@ -63,7 +65,7 @@
<ng-container i18n>Confirm</ng-container>
</button>
</ng-template>
<ng-container *ngIf="select != installed.name; else: confirmBtn">
<ng-container *ngIf="select != installed.name; else confirmBtn">
<ng-template #uninstall>
<button (click)="select = installed.name">
<ng-container i18n>Uninstall</ng-container>
Expand All @@ -73,7 +75,7 @@
*ngIf="
removingList.includes(installed.name) ||
removing.includes(installed.name);
else: uninstall
else uninstall
"
disabled
>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import { combineLatest, BehaviorSubject } from 'rxjs';
import { map, tap, share, shareReplay, distinctUntilChanged, debounceTime } from 'rxjs/operators';
import {
map,
tap,
share,
shareReplay,
distinctUntilChanged,
debounceTime,
} from 'rxjs/operators';

import { LocalAppService } from '../../services/local-app.service';
import { App } from 'app/services/app.service';
import { AuthService } from 'app/services/auth.service';

@Component({
selector: 'dstore-local-app',
Expand All @@ -16,8 +24,9 @@ export class LocalAppComponent implements OnInit {
private route: ActivatedRoute,
private router: Router,
private localAppService: LocalAppService,
private authService: AuthService,
) {}

logged = this.authService.logged$;
removing: string[] = [];
select: string;
listHeight$ = new BehaviorSubject<number>(0);
Expand All @@ -26,7 +35,9 @@ export class LocalAppComponent implements OnInit {
map(height => Math.floor(height / 64)),
distinctUntilChanged(),
);
pageIndex$ = this.route.queryParamMap.pipe(map(query => Number(query.get('page') || 1) - 1));
pageIndex$ = this.route.queryParamMap.pipe(
map(query => Number(query.get('page') || 1) - 1),
);
localApps$ = this.localAppService.LocalAppList().pipe(share());
apps$ = combineLatest(this.localApps$, this.pageSize$, this.pageIndex$).pipe(
map(([apps, size, index]) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ export class AutoInstallService {
return Channel.exec('settings.setAutoInstall', auto);
}
getAutoInstall() {
return Channel.exec('settings.getAutoInstall').then(auto =>
this.zone.run(() => auto),
);
return Channel.exec('settings.getAutoInstall');
}
}
3 changes: 1 addition & 2 deletions src/web/src/app/modules/my-donates/donates.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { environment } from 'environments/environment';
providedIn: 'root',
})
export class DonatesService {
apiURL = environment.operationServer + '/api/user/my/donate';
apiURL = environment.metadataServer + '/api/donation/user';
constructor(private http: HttpClient, private appService: AppService) {}
donateList(page: number, count: number) {
return this.http
Expand All @@ -19,7 +19,6 @@ export class DonatesService {
.pipe(
switchMap(
result => {
console.log('switchMap');
const names = result.donations.map(d => d.appName);
return this.appService.getApps(names, false, false);
},
Expand Down
18 changes: 12 additions & 6 deletions src/web/src/app/modules/my-donates/donates/donates.component.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { Payment } from './../../details/services/donate.model';
import { distinctUntilChanged, map, switchMap, share, tap } from 'rxjs/operators';
import {
distinctUntilChanged,
map,
switchMap,
share,
tap,
} from 'rxjs/operators';
import { BehaviorSubject, combineLatest } from 'rxjs';
import { ActivatedRoute, Router } from '@angular/router';
import { DonatesService } from './../donates.service';
Expand All @@ -22,11 +28,13 @@ export class DonatesComponent implements OnInit {
listHeight$ = new BehaviorSubject<number>(0);
// 根据列表高度计算列表行数
pageSize$ = this.listHeight$.pipe(
map(height => Math.floor(height / 160)),
map(height => Math.floor(height / 60)),
distinctUntilChanged(),
);
// 监听当前页
pageIndex$ = this.route.queryParamMap.pipe(map(query => Number(query.get('page') || 1) - 1));
pageIndex$ = this.route.queryParamMap.pipe(
map(query => Number(query.get('page') || 1) - 1),
);
// 根据列表行数和页数的变动,拉取列表数据
result$ = combineLatest(this.pageIndex$, this.pageSize$).pipe(
switchMap(([pageIndex, pageSize]) => {
Expand All @@ -41,9 +49,7 @@ export class DonatesComponent implements OnInit {
donates$ = this.result$.pipe(map(result => result.donations));
length$ = this.result$.pipe(map(result => result.totalCount));

ngOnInit() {
this.result$.subscribe(console.log);
}
ngOnInit() {}
gotoPage(pageIndex: number) {
this.router.navigate([], { queryParams: { page: pageIndex + 1 } });
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,37 +1,30 @@
<div class="titleGroup">
<div class="title">
<ng-content></ng-content>
<span class="number"
*ngIf="count"
i18n>{{count}} applications</span>
<span class="number"
*ngIf="top"
i18n>Top {{top}}</span>
<span class="number" *ngIf="count" i18n>{{ count }} applications</span>
<span class="number" *ngIf="top" i18n>Top {{ top }}</span>
</div>
<div class="dropdown"
tabIndex="-1"
(click)="dropMenu.hidden=!dropMenu.hidden;dorpBtn.focus()"
[hidden]="sortHidden"
(blur)="dropMenu.hidden=true"
#dorpBtn>

<ng-container *ngIf="sortBy===SortOrder.Downloads"
i18n>Downloads</ng-container>
<ng-container *ngIf="sortBy===SortOrder.Score"
i18n>Rating</ng-container>
<div
class="dropdown"
tabIndex="-1"
(click)="dropMenu.hidden = !dropMenu.hidden; dorpBtn.focus()"
[hidden]="sortHidden"
(blur)="dropMenu.hidden = true"
#dorpBtn
>
<ng-container *ngIf="sortBy === SortOrder.Downloads" i18n
>Downloads</ng-container
>
<ng-container *ngIf="sortBy === SortOrder.Score" i18n>Rating</ng-container>
</div>
<ul class="dropdown-menu"
hidden
#dropMenu>
<ul class="dropdown-menu" hidden #dropMenu>
<li (mousedown)="change(SortOrder.Downloads)">
<div class="select"
[class.ok]="sortBy===SortOrder.Downloads"></div>
<div class="select" [class.ok]="sortBy === SortOrder.Downloads"></div>
<ng-container i18n>Downloads</ng-container>
</li>
<li (mousedown)="change(SortOrder.Score)">
<div class="select"
[class.ok]="sortBy===SortOrder.Score"></div>
<div class="select" [class.ok]="sortBy === SortOrder.Score"></div>
<ng-container i18n>Rating</ng-container>
</li>
</ul>
</div>
</div>
14 changes: 3 additions & 11 deletions src/web/src/app/services/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,11 @@ import { Channel } from 'app/modules/client/utils/channel';
export class AuthService {
constructor(private zone: NgZone) {
Channel.exec<UserInfo>('account.getUserInfo').then(info => {
this.zone.run(() => {
this.userInfo$.next(info);
});
this.userInfo$.next(info);
});
Channel.connect<UserInfo>('account.userInfoChanged').subscribe(info => {
console.log('userinfo', info);
this.zone.run(() => {
this.userInfo$.next(info);
});
this.userInfo$.next(info);
});
}
private userInfo$ = new BehaviorSubject<UserInfo>(null);
Expand All @@ -34,11 +30,7 @@ export class AuthService {
logged$ = this.info$.pipe(map(info => info && info.IsLoggedIn));
// get token
getToken() {
return Channel.exec<string>('account.getToken').then(token => {
return this.zone.run(() => {
return token;
});
});
return Channel.exec<string>('account.getToken');
}
// 登录方法
login() {
Expand Down
65 changes: 33 additions & 32 deletions src/web/src/app/services/job.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,39 +24,40 @@ export class JobService {
this.StoreServer.jobListChange().subscribe(list => this.update(list));
}
private update(list: string[]) {
this.zone.run(() => {
this.jobList$.next(list);
if (this.interval) {
this.interval.unsubscribe();
}
const defer = Array.from(this.cache.values())
.filter(job => !list.includes(job.job))
.map(job => job.id);
if (defer.length > 0) {
setTimeout(() => {
defer.forEach(id => this.cache.delete(id));
this.jobInfoList$.next(Array.from(this.cache.values()));
}, 500);
}
if (list.length > 0) {
this.interval = timer(0, 1000)
.pipe(switchMap(() => this.StoreServer.getJobsInfo(list)))
.subscribe(infoList => {
infoList = infoList.filter(job => {
if (job.type === StoreJobType.uninstall && job.status === StoreJobStatus.failed) {
return false;
}
return true;
});
infoList.forEach(job => {
this.cache.set(job.id, job);
});
this.jobInfoList$.next(Array.from(this.cache.values()));
});
} else {
this.jobList$.next(list);
if (this.interval) {
this.interval.unsubscribe();
}
const defer = Array.from(this.cache.values())
.filter(job => !list.includes(job.job))
.map(job => job.id);
if (defer.length > 0) {
setTimeout(() => {
defer.forEach(id => this.cache.delete(id));
this.jobInfoList$.next(Array.from(this.cache.values()));
}
});
}, 500);
}
if (list.length > 0) {
this.interval = timer(0, 1000)
.pipe(switchMap(() => this.StoreServer.getJobsInfo(list)))
.subscribe(infoList => {
infoList = infoList.filter(job => {
if (
job.type === StoreJobType.uninstall &&
job.status === StoreJobStatus.failed
) {
return false;
}
return true;
});
infoList.forEach(job => {
this.cache.set(job.id, job);
});
this.jobInfoList$.next(Array.from(this.cache.values()));
});
} else {
this.jobInfoList$.next(Array.from(this.cache.values()));
}
}
jobList(): Observable<string[]> {
return this.jobList$.asObservable();
Expand Down
6 changes: 2 additions & 4 deletions src/web/src/app/services/menu.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ export class MenuService {
// menu user info
this.auth.info$.subscribe(async userInfo => {
if (!userInfo) {
Channel.exec('menu.setUserInfo', {});
return;
}
console.log('setUserInfo');
const dInfo = await this.dService.getDeepinUserInfo(userInfo.UserID);
const avatar = await fetch(dInfo.profile_image).then(resp => resp.blob());
const data = await new Promise<string>((resolve, reject) => {
Expand All @@ -43,9 +43,7 @@ export class MenuService {

connectToRouter(signal: string, url: string) {
Channel.connect(signal).subscribe(() => {
this.zone.run(() => {
this.router.navigateByUrl(url);
});
this.router.navigateByUrl(url);
});
}
}
13 changes: 7 additions & 6 deletions src/web/src/app/services/notify.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,20 @@ export class NotifyService {
this.getBulletin();
if (BaseService.isNative) {
DstoreObject.clearArchives().subscribe(() => {
this.zone.run(() => {
this.success(NotifyType.Clear);
});
this.success(NotifyType.Clear);
});
}
}

private getBulletin() {
this.http
.get(BaseService.serverHosts.operationServer + '/api/bulletin', { responseType: 'text' })
.get(BaseService.serverHosts.operationServer + '/api/bulletin', {
responseType: 'text',
})
.subscribe(body => {
const { bulletin }: { bulletin: Bulletin } = JSON.parse(body, (k: string, v) =>
k.includes('Time') ? new Date(v) : v,
const { bulletin }: { bulletin: Bulletin } = JSON.parse(
body,
(k: string, v) => (k.includes('Time') ? new Date(v) : v),
);
const t = new Date();
if (bulletin.startTime <= t && bulletin.endTime > t) {
Expand Down
4 changes: 1 addition & 3 deletions src/web/src/app/services/recommend.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ import { BaseService } from '../dstore/services/base.service';
export class RecommendService {
constructor(private zone: NgZone, private http: HttpClient) {
Channel.connect<void>('menu.recommendAppRequested').subscribe(() => {
zone.run(() => {
this.openRecommend();
});
this.openRecommend();
});
}

Expand Down
Loading

0 comments on commit 3555abc

Please sign in to comment.