feat(infra): framework

This commit is contained in:
EYHN
2024-04-17 14:12:29 +08:00
parent ab17a05df3
commit 06fda3b62c
467 changed files with 9996 additions and 8697 deletions

View File

@@ -0,0 +1,65 @@
import { DebugLogger } from '@affine/debug';
import { WorkspaceFlavour } from '@affine/env/workspace';
import type { WorkspaceService } from '@toeverything/infra';
import {
backoffRetry,
catchErrorInto,
effect,
Entity,
fromPromise,
LiveData,
mapInto,
onComplete,
onStart,
} from '@toeverything/infra';
import { exhaustMap } from 'rxjs';
import { isBackendError, isNetworkError } from '../../cloud';
import type { WorkspacePermissionStore } from '../stores/permission';
const logger = new DebugLogger('affine:workspace-permission');
export class WorkspacePermission extends Entity {
isOwner$ = new LiveData<boolean | null>(null);
isLoading$ = new LiveData(false);
error$ = new LiveData<any>(null);
constructor(
private readonly workspaceService: WorkspaceService,
private readonly store: WorkspacePermissionStore
) {
super();
}
revalidate = effect(
exhaustMap(() => {
return fromPromise(async signal => {
if (
this.workspaceService.workspace.flavour ===
WorkspaceFlavour.AFFINE_CLOUD
) {
return await this.store.fetchIsOwner(
this.workspaceService.workspace.id,
signal
);
} else {
return true;
}
}).pipe(
backoffRetry({
when: isNetworkError,
count: Infinity,
}),
backoffRetry({
when: isBackendError,
}),
mapInto(this.isOwner$),
catchErrorInto(this.error$, error => {
logger.error('Failed to fetch isOwner', error);
}),
onStart(() => this.isLoading$.setValue(true)),
onComplete(() => this.isLoading$.setValue(false))
);
})
);
}

View File

@@ -0,0 +1,20 @@
export { WorkspacePermissionService } from './services/permission';
import { GraphQLService } from '@affine/core/modules/cloud';
import {
type Framework,
WorkspaceScope,
WorkspaceService,
} from '@toeverything/infra';
import { WorkspacePermission } from './entities/permission';
import { WorkspacePermissionService } from './services/permission';
import { WorkspacePermissionStore } from './stores/permission';
export function configurePermissionsModule(framework: Framework) {
framework
.scope(WorkspaceScope)
.service(WorkspacePermissionService)
.store(WorkspacePermissionStore, [GraphQLService])
.entity(WorkspacePermission, [WorkspaceService, WorkspacePermissionStore]);
}

View File

@@ -0,0 +1,7 @@
import { Service } from '@toeverything/infra';
import { WorkspacePermission } from '../entities/permission';
export class WorkspacePermissionService extends Service {
permission = this.framework.createEntity(WorkspacePermission);
}

View File

@@ -0,0 +1,21 @@
import type { GraphQLService } from '@affine/core/modules/cloud';
import { getIsOwnerQuery } from '@affine/graphql';
import { Store } from '@toeverything/infra';
export class WorkspacePermissionStore extends Store {
constructor(private readonly graphqlService: GraphQLService) {
super();
}
async fetchIsOwner(workspaceId: string, signal?: AbortSignal) {
const isOwner = await this.graphqlService.gql({
query: getIsOwnerQuery,
variables: {
workspaceId,
},
context: { signal },
});
return isOwner.isOwner;
}
}