* 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 * . * . * .
51 lines
1.4 KiB
TypeScript
51 lines
1.4 KiB
TypeScript
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();
|
|
}
|
|
}
|