feat: local command

This commit is contained in:
DarkSky
2022-07-22 18:08:30 +08:00
parent 0871fbda07
commit 9361ad8e61
9 changed files with 43 additions and 23 deletions

View File

@@ -24,16 +24,16 @@ const waitLoading = async (key: string) => {
};
async function _getCurrentToken() {
if (process.env['NX_FREE_LOGIN']) {
return 'NX_FREE_LOGIN';
}
const token = await getAuth().currentUser?.getIdToken();
if (token) return token;
return new Promise<string>(resolve => {
getAuth().onIdTokenChanged((user: User | null) => {
if (user) resolve(user.getIdToken());
if (!process.env['NX_LOCAL']) {
const token = await getAuth().currentUser?.getIdToken();
if (token) return token;
return new Promise<string>(resolve => {
getAuth().onIdTokenChanged((user: User | null) => {
if (user) resolve(user.getIdToken());
});
});
});
}
return undefined;
}
async function _getBlockDatabase(

View File

@@ -16,6 +16,7 @@ import {
transact,
encodeStateAsUpdate,
applyUpdate,
snapshot,
} from 'yjs';
import { WebsocketProvider } from '@toeverything/datasource/jwt-rpc';
@@ -62,7 +63,7 @@ async function _initWebsocketProvider(
params?: YjsInitOptions['params']
): Promise<[Awareness, WebsocketProvider | undefined]> {
const awareness = new Awareness(doc);
if (token && !process.env['NX_FREE_LOGIN']) {
if (token) {
const ws = new WebsocketProvider(token, url, room, doc, {
awareness,
params,
@@ -129,10 +130,13 @@ async function _initYjsDatabase(
binaries
).whenSynced;
const gateKeeperData = doc.getMap<YMap<string>>('gatekeeper');
const gatekeeper = new GateKeeper(
userId,
doc.getMap('creators'),
doc.getMap('common')
gateKeeperData.get('creators') ||
gateKeeperData.set('creators', new YMap()),
gateKeeperData.get('common') || gateKeeperData.set('common', new YMap())
);
_yjsDatabaseInstance.set(workspace, {
@@ -206,8 +210,11 @@ export class YjsAdapter implements AsyncDatabaseAdapter<YjsContentOperation> {
this.#awareness = providers.awareness;
this.#gatekeeper = providers.gatekeeper;
this.#blocks = this.#doc.getMap('blocks');
this.#block_updated = this.#doc.getMap('block_updated');
const blocks = this.#doc.getMap<YMap<any>>('blocks');
this.#blocks =
blocks.get('content') || blocks.set('content', new YMap());
this.#block_updated =
blocks.get('updated') || blocks.set('updated', new YMap());
this.#block_caches = new LRUCache({ max: 1024, ttl: 1000 * 60 * 10 });
this.#binaries = new YjsRemoteBinaries(
providers.binariesIdb.doc.getMap(),
@@ -381,6 +388,18 @@ export class YjsAdapter implements AsyncDatabaseAdapter<YjsContentOperation> {
this.#blocks.clear();
this.#block_updated.clear();
this.#gatekeeper.clear();
this.#doc.getMap('blocks').clear();
this.#doc.getMap('gatekeeper').clear();
},
// eslint-disable-next-line @typescript-eslint/naming-convention
clear_old: () => {
this.#doc.getMap('block_updated').clear();
this.#doc.getMap('blocks').clear();
this.#doc.getMap('common').clear();
this.#doc.getMap('creators').clear();
},
snapshot: () => {
return snapshot(this.#doc);
},
};
}

View File

@@ -63,7 +63,7 @@ const _useUserAndSpace = () => {
};
const _useUserAndSpacesForFreeLogin = () => {
const [loading] = useAtom(_loadingAtom);
const [loading, setLoading] = useAtom(_loadingAtom);
useEffect(() => setLoading(false), []);
const BRAND_ID = 'AFFiNE';
@@ -78,6 +78,6 @@ const _useUserAndSpacesForFreeLogin = () => {
loading,
};
};
export const useUserAndSpaces = process.env['NX_FREE_LOGIN']
export const useUserAndSpaces = process.env['NX_LOCAL']
? _useUserAndSpacesForFreeLogin
: _useUserAndSpace;