Merge pull request #442 from toeverything/chore/db-tweak
Chore/db tweak
This commit is contained in:
@@ -8,21 +8,12 @@ import {
|
|||||||
BlockMatcher,
|
BlockMatcher,
|
||||||
Connectivity,
|
Connectivity,
|
||||||
} from '@toeverything/datasource/jwt';
|
} from '@toeverything/datasource/jwt';
|
||||||
import { sleep } from '@toeverything/utils';
|
|
||||||
|
|
||||||
import type { ObserveCallback, ReturnUnobserve } from './observer';
|
import type { ObserveCallback, ReturnUnobserve } from './observer';
|
||||||
import { getObserverName, ObserverManager } from './observer';
|
import { getObserverName, ObserverManager } from './observer';
|
||||||
export type { ObserveCallback, ReturnUnobserve } from './observer';
|
export type { ObserveCallback, ReturnUnobserve } from './observer';
|
||||||
|
|
||||||
const workspaces: Record<string, BlockClientInstance> = {};
|
const workspaces: Record<string, Promise<BlockClientInstance>> = {};
|
||||||
|
|
||||||
const loading = new Set();
|
|
||||||
|
|
||||||
const waitLoading = async (key: string) => {
|
|
||||||
while (loading.has(key)) {
|
|
||||||
await sleep(50);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
async function _getCurrentToken() {
|
async function _getCurrentToken() {
|
||||||
if (!process.env['NX_LOCAL']) {
|
if (!process.env['NX_LOCAL']) {
|
||||||
@@ -48,20 +39,15 @@ async function _getBlockDatabase(
|
|||||||
workspace: string,
|
workspace: string,
|
||||||
options?: BlockInitOptions
|
options?: BlockInitOptions
|
||||||
) {
|
) {
|
||||||
if (loading.has(workspace)) {
|
|
||||||
await waitLoading(workspace);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!workspaces[workspace]) {
|
if (!workspaces[workspace]) {
|
||||||
loading.add(workspace);
|
workspaces[workspace] = _getCurrentToken().then(token =>
|
||||||
workspaces[workspace] = await BlockClient.init(workspace, {
|
BlockClient.init(workspace, {
|
||||||
enabled: _enabled[workspace] || ['idb'],
|
enabled: _enabled[workspace] || ['idb'],
|
||||||
...options,
|
...options,
|
||||||
token: await _getCurrentToken(),
|
token,
|
||||||
});
|
})
|
||||||
|
);
|
||||||
(window as any).client = workspaces[workspace];
|
(window as any).client = workspaces[workspace];
|
||||||
await workspaces[workspace].buildIndex();
|
|
||||||
loading.delete(workspace);
|
|
||||||
}
|
}
|
||||||
return workspaces[workspace];
|
return workspaces[workspace];
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -641,7 +641,7 @@ export class BlockClient<
|
|||||||
const { importData, exportData, hasExporter, installExporter } =
|
const { importData, exportData, hasExporter, installExporter } =
|
||||||
getDataExporter();
|
getDataExporter();
|
||||||
|
|
||||||
const instance = await YjsAdapter.init(workspace, {
|
const yjsAdapter = await YjsAdapter.init(workspace, {
|
||||||
provider: getYjsProviders({
|
provider: getYjsProviders({
|
||||||
enabled: [],
|
enabled: [],
|
||||||
backend: BucketBackend.YjsWebSocketAffine,
|
backend: BucketBackend.YjsWebSocketAffine,
|
||||||
@@ -652,10 +652,17 @@ export class BlockClient<
|
|||||||
}),
|
}),
|
||||||
...options,
|
...options,
|
||||||
});
|
});
|
||||||
return new BlockClient(instance, workspace, {
|
|
||||||
|
const blockClient: BlockClientInstance = new BlockClient(
|
||||||
|
yjsAdapter,
|
||||||
|
workspace,
|
||||||
|
{
|
||||||
...options,
|
...options,
|
||||||
installExporter,
|
installExporter,
|
||||||
});
|
}
|
||||||
|
);
|
||||||
|
await blockClient.buildIndex();
|
||||||
|
return blockClient;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -16,9 +16,7 @@ function getLocation() {
|
|||||||
function getCollaborationPoint() {
|
function getCollaborationPoint() {
|
||||||
const { protocol, host } = getLocation();
|
const { protocol, host } = getLocation();
|
||||||
const ws = protocol.startsWith('https') ? 'wss' : 'ws';
|
const ws = protocol.startsWith('https') ? 'wss' : 'ws';
|
||||||
const isOnline = host.endsWith('affine.pro');
|
return `${ws}://${host}/collaboration/`;
|
||||||
const site = isOnline ? host : 'localhost:3000';
|
|
||||||
return `${ws}://${site}/collaboration/`;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const BucketBackend = {
|
export const BucketBackend = {
|
||||||
|
|||||||
Reference in New Issue
Block a user