Skip to content

Commit

Permalink
feat(core): reduce profile loading time
Browse files Browse the repository at this point in the history
  • Loading branch information
EYHN committed Apr 18, 2024
1 parent 10653ec commit 4d383f3
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 20 deletions.
17 changes: 12 additions & 5 deletions packages/common/infra/src/modules/workspace/entities/profile.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
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 { effect, LiveData, onComplete, onStart } from '../../../livedata';
import {
effect,
fromPromise,
LiveData,
onComplete,
onStart,
} from '../../../livedata';
import type { WorkspaceMetadata } from '../metadata';
import type { WorkspaceFlavourProvider } from '../providers/flavour';
import type { WorkspaceProfileCacheStore } from '../stores/profile-cache';
Expand Down Expand Up @@ -54,11 +60,12 @@ export class WorkspaceProfile extends Entity<{ metadata: WorkspaceMetadata }> {

revalidate = effect(
switchMap(() => {
if (!this.provider) {
const provider = this.provider;
if (!provider) {
return EMPTY;
}
return from(
this.provider.getWorkspaceProfile(this.props.metadata.id)
return fromPromise(signal =>
provider.getWorkspaceProfile(this.props.metadata.id, signal)
).pipe(
mergeMap(info => {
if (info) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ export interface WorkspaceFlavourProvider {
*/
revalidate?: () => void;

getWorkspaceProfile(id: string): Promise<WorkspaceProfileInfo | undefined>;
getWorkspaceProfile(
id: string,
signal?: AbortSignal
): Promise<WorkspaceProfileInfo | undefined>;

getWorkspaceBlob(id: string, blob: string): Promise<Blob | null>;

Expand Down
26 changes: 12 additions & 14 deletions packages/frontend/core/src/modules/workspace-engine/impls/cloud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
} from '@toeverything/infra';
import { effect, globalBlockSuiteSchema, Service } from '@toeverything/infra';
import { nanoid } from 'nanoid';
import { EMPTY, lastValueFrom, map, mergeMap, timeout } from 'rxjs';
import { EMPTY, map, mergeMap } from 'rxjs';
import { applyUpdate, encodeStateAsUpdate } from 'yjs';

import type {
Expand Down Expand Up @@ -179,7 +179,8 @@ export class CloudWorkspaceFlavourProviderService
isLoading$ = new LiveData(false);
workspaces$ = new LiveData<WorkspaceMetadata[]>([]);
async getWorkspaceProfile(
id: string
id: string,
signal?: AbortSignal
): Promise<WorkspaceProfileInfo | undefined> {
// get information from both cloud and local storage

Expand All @@ -190,7 +191,7 @@ export class CloudWorkspaceFlavourProviderService
const localData = await docStorage.doc.get(id);
const cloudData = await cloudStorage.pull(id);

const isOwner = await this.getIsOwner(id);
const isOwner = await this.getIsOwner(id, signal);

if (!cloudData && !localData) {
return {
Expand Down Expand Up @@ -255,18 +256,15 @@ export class CloudWorkspaceFlavourProviderService
};
}

private async getIsOwner(workspaceId: string) {
private async getIsOwner(workspaceId: string, signal?: AbortSignal) {
return (
await lastValueFrom(
this.graphqlService
.rxGql({
query: getIsOwnerQuery,
variables: {
workspaceId,
},
})
.pipe(timeout(3000))
)
await this.graphqlService.gql({
query: getIsOwnerQuery,
variables: {
workspaceId,
},
context: { signal },
})
).isOwner;
}

Expand Down

0 comments on commit 4d383f3

Please sign in to comment.