mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-21 20:16:26 +08:00
refactor(infra): migrate to new infra (#5565)
This commit is contained in:
@@ -11,10 +11,6 @@ export const AwarenessProvider =
|
||||
export class AwarenessEngine {
|
||||
constructor(public readonly providers: AwarenessProvider[]) {}
|
||||
|
||||
static get EMPTY() {
|
||||
return new AwarenessEngine([]);
|
||||
}
|
||||
|
||||
connect() {
|
||||
this.providers.forEach(provider => provider.connect());
|
||||
}
|
||||
|
||||
@@ -54,10 +54,6 @@ export class BlobEngine {
|
||||
private readonly remotes: BlobStorage[]
|
||||
) {}
|
||||
|
||||
static get EMPTY() {
|
||||
return new BlobEngine(createEmptyBlobStorage(), []);
|
||||
}
|
||||
|
||||
start() {
|
||||
if (this.abort || this._status.isStorageOverCapacity) {
|
||||
return;
|
||||
@@ -222,21 +218,19 @@ export class BlobEngine {
|
||||
}
|
||||
}
|
||||
|
||||
export function createEmptyBlobStorage() {
|
||||
return {
|
||||
name: 'empty',
|
||||
readonly: true,
|
||||
async get(_key: string) {
|
||||
return null;
|
||||
},
|
||||
async set(_key: string, _value: Blob) {
|
||||
throw new Error('not supported');
|
||||
},
|
||||
async delete(_key: string) {
|
||||
throw new Error('not supported');
|
||||
},
|
||||
async list() {
|
||||
return [];
|
||||
},
|
||||
} satisfies BlobStorage;
|
||||
}
|
||||
export const EmptyBlobStorage: BlobStorage = {
|
||||
name: 'empty',
|
||||
readonly: true,
|
||||
async get(_key: string) {
|
||||
return null;
|
||||
},
|
||||
async set(_key: string, _value: Blob) {
|
||||
throw new Error('not supported');
|
||||
},
|
||||
async delete(_key: string) {
|
||||
throw new Error('not supported');
|
||||
},
|
||||
async list() {
|
||||
return [];
|
||||
},
|
||||
};
|
||||
|
||||
@@ -23,3 +23,22 @@ export interface SyncStorage {
|
||||
disconnect: (reason: string) => void
|
||||
): Promise<() => void>;
|
||||
}
|
||||
|
||||
export const EmptySyncStorage: SyncStorage = {
|
||||
name: 'empty',
|
||||
pull: async () => null,
|
||||
push: async () => {},
|
||||
subscribe: async () => () => {},
|
||||
};
|
||||
|
||||
export const ReadonlyMappingSyncStorage = (map: {
|
||||
[key: string]: Uint8Array;
|
||||
}): SyncStorage => ({
|
||||
name: 'map',
|
||||
pull: async (id: string) => {
|
||||
const data = map[id];
|
||||
return data ? { data } : null;
|
||||
},
|
||||
push: async () => {},
|
||||
subscribe: async () => () => {},
|
||||
});
|
||||
|
||||
@@ -74,8 +74,14 @@ export function configureWorkspaceServices(services: ServiceCollection) {
|
||||
|
||||
export function configureTestingWorkspaceServices(services: ServiceCollection) {
|
||||
services
|
||||
.addImpl(WorkspaceListProvider, TestingLocalWorkspaceListProvider, [
|
||||
.override(WorkspaceListProvider('affine-cloud'), null)
|
||||
.override(WorkspaceFactory('affine-cloud'), null)
|
||||
.override(
|
||||
WorkspaceListProvider('local'),
|
||||
TestingLocalWorkspaceListProvider,
|
||||
[GlobalState]
|
||||
)
|
||||
.override(WorkspaceFactory('local'), TestingLocalWorkspaceFactory, [
|
||||
GlobalState,
|
||||
])
|
||||
.addImpl(WorkspaceFactory, TestingLocalWorkspaceFactory, [GlobalState]);
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ import type { Workspace as BlockSuiteWorkspace } from '@blocksuite/store';
|
||||
import { applyUpdate, encodeStateAsUpdate } from 'yjs';
|
||||
|
||||
import { fixWorkspaceVersion } from '../blocksuite';
|
||||
import type { ServiceProvider } from '../di';
|
||||
import type { ServiceCollection, ServiceProvider } from '../di';
|
||||
import { ObjectPool } from '../utils/object-pool';
|
||||
import { configureWorkspaceContext } from './context';
|
||||
import type { BlobStorage } from './engine';
|
||||
@@ -90,6 +90,9 @@ export class WorkspaceManager {
|
||||
}
|
||||
|
||||
const workspace = this.instantiate(metadata);
|
||||
// sync information with workspace list, when workspace's avatar and name changed, information will be updated
|
||||
this.list.getInformation(metadata).syncWithWorkspace(workspace);
|
||||
|
||||
const ref = this.pool.put(workspace.meta.id, workspace);
|
||||
|
||||
return {
|
||||
@@ -164,14 +167,21 @@ export class WorkspaceManager {
|
||||
return factory.getWorkspaceBlob(metadata.id, blobKey);
|
||||
}
|
||||
|
||||
private instantiate(metadata: WorkspaceMetadata) {
|
||||
instantiate(
|
||||
metadata: WorkspaceMetadata,
|
||||
configureWorkspace?: (serviceCollection: ServiceCollection) => void
|
||||
) {
|
||||
logger.info(`open workspace [${metadata.flavour}] ${metadata.id} `);
|
||||
const factory = this.factories.find(x => x.name === metadata.flavour);
|
||||
if (!factory) {
|
||||
throw new Error(`Unknown workspace flavour: ${metadata.flavour}`);
|
||||
}
|
||||
const serviceCollection = this.serviceProvider.collection.clone();
|
||||
factory.configureWorkspace(serviceCollection);
|
||||
if (configureWorkspace) {
|
||||
configureWorkspace(serviceCollection);
|
||||
} else {
|
||||
const factory = this.factories.find(x => x.name === metadata.flavour);
|
||||
if (!factory) {
|
||||
throw new Error(`Unknown workspace flavour: ${metadata.flavour}`);
|
||||
}
|
||||
factory.configureWorkspace(serviceCollection);
|
||||
}
|
||||
configureWorkspaceContext(serviceCollection, metadata);
|
||||
const provider = serviceCollection.provider(
|
||||
WorkspaceScope,
|
||||
@@ -179,9 +189,6 @@ export class WorkspaceManager {
|
||||
);
|
||||
const workspace = provider.get(Workspace);
|
||||
|
||||
// sync information with workspace list, when workspace's avatar and name changed, information will be updated
|
||||
this.list.getInformation(metadata).syncWithWorkspace(workspace);
|
||||
|
||||
// apply compatibility fix
|
||||
fixWorkspaceVersion(workspace.blockSuiteWorkspace.doc);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user