No More Collab. Fixes #449
* Fully removes real-time collaboration * URL scheme rewrite
This commit is contained in:
+7
-129
@@ -17,7 +17,7 @@ const syncStartTimeKey = "syncStartTime";
|
||||
// Keeps the last time an activity was registered, used to detect if a sync is still alive and whether a new one should be started already
|
||||
const syncLastActivityKey = "syncLastActivity";
|
||||
|
||||
const syncExcludePrefix = "syncExclude:";
|
||||
const syncInitialFullSyncCompletedKey = "syncInitialFullSyncCompleted";
|
||||
|
||||
// maximum time between two activities before we consider a sync crashed
|
||||
const syncMaxIdleTimeout = 1000 * 20; // 20s
|
||||
@@ -56,17 +56,9 @@ export class SyncService {
|
||||
await this.syncFile(`${name}.md`);
|
||||
});
|
||||
|
||||
eventHook.addLocalListener("editor:pageSaved", async (name, meta) => {
|
||||
eventHook.addLocalListener("editor:pageSaved", async (name) => {
|
||||
const path = `${name}.md`;
|
||||
await this.syncFile(path);
|
||||
if (await this.isExcludedFromSync(path)) {
|
||||
// So we're editing a page and just saved it, but it's excluded from sync
|
||||
// Assumption: we're in collab mode for this file, so we're going to constantly update our local hash
|
||||
// console.log(
|
||||
// "Locally updating last modified in snapshot because we're in collab mode",
|
||||
// );
|
||||
await this.updateLocalLastModified(path, meta.lastModified);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -86,10 +78,9 @@ export class SyncService {
|
||||
return true;
|
||||
}
|
||||
|
||||
async hasInitialSyncCompleted(): Promise<boolean> {
|
||||
hasInitialSyncCompleted(): Promise<boolean> {
|
||||
// Initial sync has happened when sync progress has been reported at least once, but the syncStartTime has been reset (which happens after sync finishes)
|
||||
return !(await this.kvStore.has(syncStartTimeKey)) &&
|
||||
(await this.kvStore.has(syncLastActivityKey));
|
||||
return this.kvStore.has(syncInitialFullSyncCompletedKey);
|
||||
}
|
||||
|
||||
async registerSyncStart(): Promise<void> {
|
||||
@@ -116,40 +107,7 @@ export class SyncService {
|
||||
async registerSyncStop(): Promise<void> {
|
||||
await this.registerSyncProgress();
|
||||
await this.kvStore.del(syncStartTimeKey);
|
||||
}
|
||||
|
||||
// Temporarily exclude a specific file from sync (e.g. when in collab mode)
|
||||
excludeFromSync(path: string): Promise<void> {
|
||||
return this.kvStore.set(syncExcludePrefix + path, Date.now());
|
||||
}
|
||||
|
||||
unExcludeFromSync(path: string): Promise<void> {
|
||||
return this.kvStore.del(syncExcludePrefix + path);
|
||||
}
|
||||
|
||||
async isExcludedFromSync(path: string): Promise<boolean> {
|
||||
const lastExcluded = await this.kvStore.get(syncExcludePrefix + path);
|
||||
return lastExcluded && Date.now() - lastExcluded < syncMaxIdleTimeout;
|
||||
}
|
||||
|
||||
async fetchAllExcludedFromSync(): Promise<string[]> {
|
||||
const entries = await this.kvStore.queryPrefix(syncExcludePrefix);
|
||||
const expiredPaths: string[] = [];
|
||||
const now = Date.now();
|
||||
const result = entries.filter(({ key, value }) => {
|
||||
if (now - value > syncMaxIdleTimeout) {
|
||||
expiredPaths.push(key);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}).map(({ key }) => key.slice(syncExcludePrefix.length));
|
||||
|
||||
if (expiredPaths.length > 0) {
|
||||
console.log("Purging expired sync exclusions: ", expiredPaths);
|
||||
await this.kvStore.batchDelete(expiredPaths);
|
||||
}
|
||||
|
||||
return result;
|
||||
await this.kvStore.set(syncInitialFullSyncCompletedKey, true);
|
||||
}
|
||||
|
||||
async getSnapshot(): Promise<Map<string, SyncStatusItem>> {
|
||||
@@ -167,83 +125,6 @@ export class SyncService {
|
||||
}
|
||||
}
|
||||
|
||||
// When in collab mode, we delegate the sync to the CDRT engine, to avoid conflicts, we try to keep the lastModified time in sync with the remote
|
||||
async updateRemoteLastModified(path: string, lastModified: number) {
|
||||
await this.noOngoingSync();
|
||||
await this.registerSyncStart();
|
||||
const snapshot = await this.getSnapshot();
|
||||
const entry = snapshot.get(path);
|
||||
if (entry) {
|
||||
snapshot.set(path, [entry[0], lastModified]);
|
||||
} else {
|
||||
// In the unlikely scenario that a space first openen on a collab page before every being synced
|
||||
try {
|
||||
console.log(
|
||||
"Received lastModified time for file not in snapshot",
|
||||
path,
|
||||
lastModified,
|
||||
);
|
||||
snapshot.set(path, [
|
||||
(await this.localSpacePrimitives.getFileMeta(path)).lastModified,
|
||||
lastModified,
|
||||
]);
|
||||
} catch (e) {
|
||||
console.warn(
|
||||
"Received lastModified time for non-existing file not in snapshot",
|
||||
path,
|
||||
lastModified,
|
||||
e,
|
||||
);
|
||||
}
|
||||
}
|
||||
await this.saveSnapshot(snapshot);
|
||||
await this.registerSyncStop();
|
||||
}
|
||||
|
||||
// Reach out out to remote space, fetch the latest lastModified time and update the local snapshot
|
||||
// This is used when exiting collab mode
|
||||
async fetchAndPersistRemoteLastModified(path: string) {
|
||||
const meta = await this.remoteSpace.getFileMeta(path);
|
||||
await this.updateRemoteLastModified(
|
||||
path,
|
||||
meta.lastModified,
|
||||
);
|
||||
}
|
||||
|
||||
// When in collab mode, we delegate the sync to the CDRT engine, to avoid conflicts, we try to keep the lastModified time in sync when local changes happen
|
||||
async updateLocalLastModified(path: string, lastModified: number) {
|
||||
await this.noOngoingSync();
|
||||
await this.registerSyncStart();
|
||||
const snapshot = await this.getSnapshot();
|
||||
const entry = snapshot.get(path);
|
||||
if (entry) {
|
||||
snapshot.set(path, [lastModified, entry[1]]);
|
||||
} else {
|
||||
// In the unlikely scenario that a space first openen on a collab page before every being synced
|
||||
try {
|
||||
console.log(
|
||||
"Setting lastModified time for file not in snapshot",
|
||||
path,
|
||||
lastModified,
|
||||
);
|
||||
snapshot.set(path, [
|
||||
lastModified,
|
||||
(await this.localSpacePrimitives.getFileMeta(path)).lastModified,
|
||||
]);
|
||||
} catch (e) {
|
||||
console.warn(
|
||||
"Received lastModified time for non-existing file not in snapshot",
|
||||
path,
|
||||
lastModified,
|
||||
e,
|
||||
);
|
||||
}
|
||||
}
|
||||
await this.saveSnapshot(snapshot);
|
||||
await this.registerSyncStop();
|
||||
// console.log("All done!");
|
||||
}
|
||||
|
||||
start() {
|
||||
this.syncSpace().catch(
|
||||
console.error,
|
||||
@@ -271,14 +152,11 @@ export class SyncService {
|
||||
await this.registerSyncStart();
|
||||
let operations = 0;
|
||||
const snapshot = await this.getSnapshot();
|
||||
// Fetch the list of files that are excluded from sync (e.g. because they're in collab mode)
|
||||
const excludedFromSync = await this.fetchAllExcludedFromSync();
|
||||
// console.log("Excluded from sync", excludedFromSync);
|
||||
try {
|
||||
operations = await this.spaceSync!.syncFiles(
|
||||
snapshot,
|
||||
(path) =>
|
||||
this.isSyncCandidate(path) && !excludedFromSync.includes(path),
|
||||
(path) => this.isSyncCandidate(path),
|
||||
);
|
||||
this.eventHook.dispatchEvent("sync:success", operations);
|
||||
} catch (e: any) {
|
||||
@@ -295,7 +173,7 @@ export class SyncService {
|
||||
// console.log("Already syncing");
|
||||
return;
|
||||
}
|
||||
if (!this.isSyncCandidate(name) || (await this.isExcludedFromSync(name))) {
|
||||
if (!this.isSyncCandidate(name)) {
|
||||
return;
|
||||
}
|
||||
await this.registerSyncStart();
|
||||
|
||||
Reference in New Issue
Block a user