feat(core): reduce profile loading time (#6616)

This commit is contained in:
EYHN
2024-04-18 15:23:12 +00:00
parent c3438fde21
commit 6a23fe37a7
3 changed files with 28 additions and 20 deletions
@@ -1,8 +1,14 @@
import { DebugLogger } from '@affine/debug'; import { DebugLogger } from '@affine/debug';
import { catchError, EMPTY, from, mergeMap, switchMap } from 'rxjs'; import { catchError, EMPTY, mergeMap, switchMap } from 'rxjs';
import { Entity } from '../../../framework'; import { Entity } from '../../../framework';
import { effect, LiveData, onComplete, onStart } from '../../../livedata'; import {
effect,
fromPromise,
LiveData,
onComplete,
onStart,
} from '../../../livedata';
import type { WorkspaceMetadata } from '../metadata'; import type { WorkspaceMetadata } from '../metadata';
import type { WorkspaceFlavourProvider } from '../providers/flavour'; import type { WorkspaceFlavourProvider } from '../providers/flavour';
import type { WorkspaceProfileCacheStore } from '../stores/profile-cache'; import type { WorkspaceProfileCacheStore } from '../stores/profile-cache';
@@ -54,11 +60,12 @@ export class WorkspaceProfile extends Entity<{ metadata: WorkspaceMetadata }> {
revalidate = effect( revalidate = effect(
switchMap(() => { switchMap(() => {
if (!this.provider) { const provider = this.provider;
if (!provider) {
return EMPTY; return EMPTY;
} }
return from( return fromPromise(signal =>
this.provider.getWorkspaceProfile(this.props.metadata.id) provider.getWorkspaceProfile(this.props.metadata.id, signal)
).pipe( ).pipe(
mergeMap(info => { mergeMap(info => {
if (info) { if (info) {
@@ -47,7 +47,10 @@ export interface WorkspaceFlavourProvider {
*/ */
revalidate?: () => void; revalidate?: () => void;
getWorkspaceProfile(id: string): Promise<WorkspaceProfileInfo | undefined>; getWorkspaceProfile(
id: string,
signal?: AbortSignal
): Promise<WorkspaceProfileInfo | undefined>;
getWorkspaceBlob(id: string, blob: string): Promise<Blob | null>; getWorkspaceBlob(id: string, blob: string): Promise<Blob | null>;
@@ -26,7 +26,7 @@ import {
} from '@toeverything/infra'; } from '@toeverything/infra';
import { effect, globalBlockSuiteSchema, Service } from '@toeverything/infra'; import { effect, globalBlockSuiteSchema, Service } from '@toeverything/infra';
import { nanoid } from 'nanoid'; import { nanoid } from 'nanoid';
import { EMPTY, lastValueFrom, map, mergeMap, timeout } from 'rxjs'; import { EMPTY, map, mergeMap } from 'rxjs';
import { applyUpdate, encodeStateAsUpdate } from 'yjs'; import { applyUpdate, encodeStateAsUpdate } from 'yjs';
import type { import type {
@@ -179,7 +179,8 @@ export class CloudWorkspaceFlavourProviderService
isLoading$ = new LiveData(false); isLoading$ = new LiveData(false);
workspaces$ = new LiveData<WorkspaceMetadata[]>([]); workspaces$ = new LiveData<WorkspaceMetadata[]>([]);
async getWorkspaceProfile( async getWorkspaceProfile(
id: string id: string,
signal?: AbortSignal
): Promise<WorkspaceProfileInfo | undefined> { ): Promise<WorkspaceProfileInfo | undefined> {
// get information from both cloud and local storage // get information from both cloud and local storage
@@ -190,7 +191,7 @@ export class CloudWorkspaceFlavourProviderService
const localData = await docStorage.doc.get(id); const localData = await docStorage.doc.get(id);
const cloudData = await cloudStorage.pull(id); const cloudData = await cloudStorage.pull(id);
const isOwner = await this.getIsOwner(id); const isOwner = await this.getIsOwner(id, signal);
if (!cloudData && !localData) { if (!cloudData && !localData) {
return { return {
@@ -255,18 +256,15 @@ export class CloudWorkspaceFlavourProviderService
}; };
} }
private async getIsOwner(workspaceId: string) { private async getIsOwner(workspaceId: string, signal?: AbortSignal) {
return ( return (
await lastValueFrom( await this.graphqlService.gql({
this.graphqlService query: getIsOwnerQuery,
.rxGql({ variables: {
query: getIsOwnerQuery, workspaceId,
variables: { },
workspaceId, context: { signal },
}, })
})
.pipe(timeout(3000))
)
).isOwner; ).isOwner;
} }