chore: trick logic for temp feature

This commit is contained in:
DarkSky 2022-09-05 18:37:10 +08:00
parent 0a79e30c54
commit 8387fa4cf7
2 changed files with 28 additions and 11 deletions

View File

@ -56,6 +56,7 @@ type YjsProviders = {
connListener: { listeners?: ConnectivityListener }; connListener: { listeners?: ConnectivityListener };
userId: string; userId: string;
remoteToken: string | undefined; // remote storage token remoteToken: string | undefined; // remote storage token
providers: unknown[];
}; };
const _yjsDatabaseInstance = new Map<string, YjsProviders>(); const _yjsDatabaseInstance = new Map<string, YjsProviders>();
@ -107,19 +108,25 @@ async function _initYjsDatabase(
); );
const connListener: { listeners?: ConnectivityListener } = {}; const connListener: { listeners?: ConnectivityListener } = {};
let providers: unknown[] = [];
if (options.provider) { if (options.provider) {
const emitState = (c: Connectivity) => const emitState = (c: Connectivity) =>
connListener.listeners?.(workspace, c); connListener.listeners?.(workspace, c);
await Promise.all( providers = await Promise.all(
Object.entries(options.provider).flatMap(([, p]) => [ Object.entries(options.provider).flatMap(([name, p]) => [
p({ awareness, doc, token, workspace, emitState }), p({ awareness, doc, token, workspace, emitState }).then(p => {
console.log(p);
return {
[name]: p,
};
}),
p({ p({
awareness, awareness,
doc: binaries, doc: binaries,
token, token,
workspace: `${workspace}_binaries`, workspace: `${workspace}_binaries`,
emitState, emitState,
}), }).then(p => ({ [`${name}_binaries`]: p })),
]) ])
); );
} }
@ -128,7 +135,7 @@ async function _initYjsDatabase(
binaries, binaries,
doc, doc,
gatekeeper, gatekeeper,
providers,
connListener, connListener,
userId, userId,
remoteToken: token, remoteToken: token,
@ -342,7 +349,11 @@ export class YjsAdapter implements AsyncDatabaseAdapter<YjsContentOperation> {
}); });
const [file] = (await fromEvent(handles)) as File[]; const [file] = (await fromEvent(handles)) as File[];
const binary = await file?.arrayBuffer(); const binary = await file?.arrayBuffer();
// await this._provider.idb.clearData(); console.log(this._provider.providers);
let { indexeddb } = (
this._provider.providers as any[]
).find(p => p.indexeddb);
await indexeddb?.idb?.clearData();
const doc = new Doc({ autoLoad: true, shouldLoad: true }); const doc = new Doc({ autoLoad: true, shouldLoad: true });
let updated = 0; let updated = 0;
let isUpdated = false; let isUpdated = false;
@ -365,8 +376,8 @@ export class YjsAdapter implements AsyncDatabaseAdapter<YjsContentOperation> {
}; };
check(); check();
}); });
// await new IndexedDBProvider(this._provider.idb.name, doc) await new indexeddb.ctor(indexeddb.idb.name, doc)
// .whenSynced; .whenSynced;
if (binary) { if (binary) {
applyUpdate(doc, new Uint8Array(binary)); applyUpdate(doc, new Uint8Array(binary));
await update_check; await update_check;
@ -374,6 +385,7 @@ export class YjsAdapter implements AsyncDatabaseAdapter<YjsContentOperation> {
return true; return true;
} catch (err) { } catch (err) {
console.log(err);
return false; return false;
} }
}, },

View File

@ -18,7 +18,9 @@ type YjsDefaultInstances = {
emitState: (connectivity: Connectivity) => void; emitState: (connectivity: Connectivity) => void;
}; };
export type YjsProvider = (instances: YjsDefaultInstances) => Promise<void>; export type YjsProvider = (
instances: YjsDefaultInstances
) => Promise<unknown | undefined>;
type ProviderType = 'idb' | 'sqlite' | 'ws'; type ProviderType = 'idb' | 'sqlite' | 'ws';
@ -38,9 +40,12 @@ export const getYjsProviders = (
return { return {
indexeddb: async (instances: YjsDefaultInstances) => { indexeddb: async (instances: YjsDefaultInstances) => {
if (options.enabled.includes('idb')) { if (options.enabled.includes('idb')) {
await new IndexedDBProvider(instances.workspace, instances.doc) return new IndexedDBProvider(
.whenSynced; instances.workspace,
instances.doc
).whenSynced.then(idb => ({ idb, ctor: IndexedDBProvider }));
} }
return undefined;
}, },
sqlite: async (instances: YjsDefaultInstances) => { sqlite: async (instances: YjsDefaultInstances) => {
if (options.enabled.includes('sqlite')) { if (options.enabled.includes('sqlite')) {