From 8ab3a049117af1fb4b6c937361b5bbf326b8fb0b Mon Sep 17 00:00:00 2001 From: crupest Date: Sat, 8 Aug 2020 20:46:57 +0800 Subject: ... --- Timeline/ClientApp/src/app/data/user.ts | 71 ++++++++++----------------------- 1 file changed, 22 insertions(+), 49 deletions(-) (limited to 'Timeline/ClientApp/src') diff --git a/Timeline/ClientApp/src/app/data/user.ts b/Timeline/ClientApp/src/app/data/user.ts index f98d2119..5b96e2b6 100644 --- a/Timeline/ClientApp/src/app/data/user.ts +++ b/Timeline/ClientApp/src/app/data/user.ts @@ -1,6 +1,6 @@ import React, { useState, useEffect } from 'react'; import { BehaviorSubject, Observable, from } from 'rxjs'; -import { map, filter, switchMap, take } from 'rxjs/operators'; +import { map, filter } from 'rxjs/operators'; import { UiLogicError } from '../common'; import { convertError } from '../utilities/rxjs'; @@ -237,18 +237,12 @@ export class UserInfoService { this._userHub.getLine(user.username)?.next({ user, type: 'synced' }); } - private async getCachedUser(username: string): Promise { - const uniqueId = await dataStorage.getItem( - `user.${username}` - ); - if (uniqueId == null) return null; - const user = await dataStorage.getItem(`user.${uniqueId}`); - return user; + private getCachedUser(username: string): Promise { + return dataStorage.getItem(`user.${username}`); } private async doSaveUser(user: HttpUser): Promise { - await dataStorage.setItem(`user.${user.username}`, user.uniqueId); - await dataStorage.setItem(`user.${user.uniqueId}`, user); + await dataStorage.setItem(`user.${user.username}`, user); } private async syncUser(username: string): Promise { @@ -302,60 +296,54 @@ export class UserInfoService { ); } - private getCachedAvatar(uniqueId: string): Promise { + private getCachedAvatar(username: string): Promise { return dataStorage - .getItem(`user.${uniqueId}.avatar`) + .getItem(`user.${username}.avatar`) .then((data) => data?.data ?? null); } - private async syncAvatar(user: { - username: string; - uniqueId: string; - }): Promise { - const syncStatusKey = `user.avatar.${user.uniqueId}`; + private async syncAvatar(username: string): Promise { + const syncStatusKey = `user.avatar.${username}`; if (syncStatusHub.get(syncStatusKey)) return; syncStatusHub.begin(syncStatusKey); - const dataKey = `user.${user.uniqueId}.avatar`; + const dataKey = `user.${username}.avatar`; const cache = await dataStorage.getItem(dataKey); if (cache == null) { try { - const avatar = await getHttpUserClient().getAvatar(user.username); + const avatar = await getHttpUserClient().getAvatar(username); await dataStorage.setItem(dataKey, avatar); syncStatusHub.end(syncStatusKey); this._avatarHub - .getLine(user) + .getLine(username) ?.next({ data: avatar.data, type: 'synced' }); } catch (e) { syncStatusHub.end(syncStatusKey); - this._avatarHub.getLine(user)?.next({ type: 'offline' }); + this._avatarHub.getLine(username)?.next({ type: 'offline' }); if (!(e instanceof HttpNetworkError)) { throw e; } } } else { try { - const res = await getHttpUserClient().getAvatar( - user.username, - cache.etag - ); + const res = await getHttpUserClient().getAvatar(username, cache.etag); if (res instanceof NotModified) { syncStatusHub.end(syncStatusKey); this._avatarHub - .getLine(user) + .getLine(username) ?.next({ data: cache.data, type: 'synced' }); } else { const avatar = res; await dataStorage.setItem(dataKey, avatar); syncStatusHub.end(syncStatusKey); this._avatarHub - .getLine(user) + .getLine(username) ?.next({ data: avatar.data, type: 'synced' }); } } catch (e) { syncStatusHub.end(syncStatusKey); this._avatarHub - .getLine(user) + .getLine(username) ?.next({ data: cache.data, type: 'offline' }); if (!(e instanceof HttpNetworkError)) { throw e; @@ -365,13 +353,12 @@ export class UserInfoService { } private _avatarHub = new SubscriptionHub< - { username: string; uniqueId: string }, + string, | { data: Blob; type: 'cache' | 'synced' | 'offline' } | { data?: undefined; type: 'notexist' | 'offline' } >({ - keyToString: (key) => `${key.username}.${key.uniqueId}`, setup: (key, line) => { - void this.getCachedAvatar(key.uniqueId).then((avatar) => { + void this.getCachedAvatar(key).then((avatar) => { if (avatar != null) { line.next({ data: avatar, type: 'cache' }); } @@ -381,19 +368,9 @@ export class UserInfoService { }); getAvatar$(username: string): Observable { - return this._userHub.getObservable(username).pipe( - switchMap((state) => { - if (state.user == null) return []; - if (state.type === 'synced') - return this._avatarHub.getObservable(state.user).pipe( - map((state) => state?.data), - filter((data): data is Blob => data != null) - ); - else - return from(this.getCachedAvatar(state.user.uniqueId)).pipe( - filter((data): data is Blob => data != null) - ); - }) + return this._avatarHub.getObservable(username).pipe( + map((state) => state.data), + filter((blob): blob is Blob => blob != null) ); } @@ -406,11 +383,7 @@ export class UserInfoService { async setAvatar(username: string, blob: Blob): Promise { const user = checkLogin(); await getHttpUserClient().putAvatar(username, blob, user.token); - this.getUser$(username) - .pipe(take(1)) - .subscribe((user) => { - this._avatarHub.getLine(user)?.next({ data: blob, type: 'synced' }); - }); + this._avatarHub.getLine(username)?.next({ data: blob, type: 'synced' }); } } -- cgit v1.2.3