Fix syncing when network latency is present (#4)
* WIP * Add debug * Dedupe inserts * Add deterministic ordering * Fix whitespaces * Update insta * Add integration test script * Rename * Add test * Working for non-deletes * omg it mostly works for deletes * Isdeleted fix * remove created dates * update api * Take document id * No max attempt * works * Use string uuids * . * working!!!! (hopefully) * Improve bundling * Add module * lint * . * lint * Fix CI * use toolchain * clean up * Add useSlowFileEvents * Delete fuzz * Fix CI * use docker * fix script * clean up * Clean up * change node version * Build docker image on every commit * fix ci * 1 db per vault * Add scritps folder * Bump versions * Lint * . * Fix tests for real * Style * . * try * Consistent ordering * Fix tests * hmm * . * Clean up diff * Fixes * . * Fix version bump * . * . * .
This commit is contained in:
parent
bcf48c428d
commit
8b8f1d91d9
1 changed files with 2216 additions and 1550 deletions
51
frontend/sync-client/src/services/connected-state.ts
Normal file
51
frontend/sync-client/src/services/connected-state.ts
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
import type { Settings } from "../persistence/settings";
|
||||
import type { Logger } from "../tracing/logger";
|
||||
import { createPromise } from "../utils/create-promise";
|
||||
import { retriedFetchFactory } from "../utils/retried-fetch";
|
||||
|
||||
export class ConnectedState {
|
||||
private resolveIsSyncEnabled: (() => void) | undefined;
|
||||
private syncIsEnabled: Promise<void> | undefined;
|
||||
|
||||
public constructor(
|
||||
settings: Settings,
|
||||
private readonly logger: Logger
|
||||
) {
|
||||
settings.addOnSettingsChangeHandlers((newSettings, oldSettings) => {
|
||||
if (!oldSettings.isSyncEnabled && newSettings.isSyncEnabled) {
|
||||
this.handleComingOnline();
|
||||
} else if (
|
||||
oldSettings.isSyncEnabled &&
|
||||
!newSettings.isSyncEnabled
|
||||
) {
|
||||
this.handleGoingOffline();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public getFetchImplementation(
|
||||
fetch: typeof globalThis.fetch,
|
||||
{ doRetries = true }: { doRetries: boolean } = { doRetries: true }
|
||||
): typeof globalThis.fetch {
|
||||
const retriedFetch = doRetries
|
||||
? retriedFetchFactory(this.logger, fetch)
|
||||
: fetch;
|
||||
|
||||
return async (input: RequestInfo | URL): Promise<Response> => {
|
||||
if (this.syncIsEnabled !== undefined) {
|
||||
await this.syncIsEnabled;
|
||||
}
|
||||
return retriedFetch(input);
|
||||
};
|
||||
}
|
||||
|
||||
private handleComingOnline(): void {
|
||||
this.logger.debug("Sync is enabled");
|
||||
this.resolveIsSyncEnabled?.();
|
||||
}
|
||||
|
||||
private handleGoingOffline(): void {
|
||||
this.logger.debug("Sync is disabled");
|
||||
[this.syncIsEnabled, this.resolveIsSyncEnabled] = createPromise();
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue