Various improvements #169
2 changed files with 12 additions and 14 deletions
Formatting
commit
fee35a35cd
|
|
@ -1,6 +1,6 @@
|
||||||
export class SyncResetError extends Error {
|
export class SyncResetError extends Error {
|
||||||
public constructor() {
|
public constructor() {
|
||||||
super("Sync was reset");
|
super("SyncClient has been reset, cleaning up");
|
||||||
this.name = "SyncResetError";
|
this.name = "SyncResetError";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -19,11 +19,6 @@ import type { DeleteDocumentVersion } from "./types/DeleteDocumentVersion";
|
||||||
import type { UpdateTextDocumentVersion } from "./types/UpdateTextDocumentVersion";
|
import type { UpdateTextDocumentVersion } from "./types/UpdateTextDocumentVersion";
|
||||||
import { NETWORK_RETRY_INTERVAL_MS } from "../consts";
|
import { NETWORK_RETRY_INTERVAL_MS } from "../consts";
|
||||||
|
|
||||||
export interface CheckConnectionResult {
|
|
||||||
isSuccessful: boolean;
|
|
||||||
message: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export class SyncService {
|
export class SyncService {
|
||||||
private readonly client: typeof globalThis.fetch;
|
private readonly client: typeof globalThis.fetch;
|
||||||
private readonly pingClient: typeof globalThis.fetch;
|
private readonly pingClient: typeof globalThis.fetch;
|
||||||
|
|
@ -65,7 +60,7 @@ export class SyncService {
|
||||||
relativePath: RelativePath;
|
relativePath: RelativePath;
|
||||||
contentBytes: Uint8Array;
|
contentBytes: Uint8Array;
|
||||||
}): Promise<DocumentVersionWithoutContent> {
|
}): Promise<DocumentVersionWithoutContent> {
|
||||||
return this.withRetries(async () => {
|
return this.retryForever(async () => {
|
||||||
const formData = new FormData();
|
const formData = new FormData();
|
||||||
if (documentId !== undefined) {
|
if (documentId !== undefined) {
|
||||||
formData.append("document_id", documentId);
|
formData.append("document_id", documentId);
|
||||||
|
|
@ -114,7 +109,7 @@ export class SyncService {
|
||||||
relativePath: RelativePath;
|
relativePath: RelativePath;
|
||||||
content: (number | string)[];
|
content: (number | string)[];
|
||||||
}): Promise<DocumentUpdateResponse> {
|
}): Promise<DocumentUpdateResponse> {
|
||||||
return this.withRetries(async () => {
|
return this.retryForever(async () => {
|
||||||
this.logger.debug(
|
this.logger.debug(
|
||||||
`Updating text document ${documentId} with parent version ${parentVersionId} and relative path ${relativePath}`
|
`Updating text document ${documentId} with parent version ${parentVersionId} and relative path ${relativePath}`
|
||||||
);
|
);
|
||||||
|
|
@ -166,7 +161,7 @@ export class SyncService {
|
||||||
relativePath: RelativePath;
|
relativePath: RelativePath;
|
||||||
contentBytes: Uint8Array;
|
contentBytes: Uint8Array;
|
||||||
}): Promise<DocumentUpdateResponse> {
|
}): Promise<DocumentUpdateResponse> {
|
||||||
return this.withRetries(async () => {
|
return this.retryForever(async () => {
|
||||||
this.logger.debug(
|
this.logger.debug(
|
||||||
`Updating binary document ${documentId} with parent version ${parentVersionId} and relative path ${relativePath}`
|
`Updating binary document ${documentId} with parent version ${parentVersionId} and relative path ${relativePath}`
|
||||||
);
|
);
|
||||||
|
|
@ -215,7 +210,7 @@ export class SyncService {
|
||||||
documentId: DocumentId;
|
documentId: DocumentId;
|
||||||
relativePath: RelativePath;
|
relativePath: RelativePath;
|
||||||
}): Promise<DocumentVersionWithoutContent> {
|
}): Promise<DocumentVersionWithoutContent> {
|
||||||
return this.withRetries(async () => {
|
return this.retryForever(async () => {
|
||||||
const request: DeleteDocumentVersion = {
|
const request: DeleteDocumentVersion = {
|
||||||
relativePath
|
relativePath
|
||||||
};
|
};
|
||||||
|
|
@ -252,7 +247,7 @@ export class SyncService {
|
||||||
}: {
|
}: {
|
||||||
documentId: DocumentId;
|
documentId: DocumentId;
|
||||||
}): Promise<DocumentVersion> {
|
}): Promise<DocumentVersion> {
|
||||||
return this.withRetries(async () => {
|
return this.retryForever(async () => {
|
||||||
const response = await this.client(
|
const response = await this.client(
|
||||||
this.getUrl(`/documents/${documentId}`),
|
this.getUrl(`/documents/${documentId}`),
|
||||||
{
|
{
|
||||||
|
|
@ -280,7 +275,7 @@ export class SyncService {
|
||||||
public async getAll(
|
public async getAll(
|
||||||
since?: VaultUpdateId
|
since?: VaultUpdateId
|
||||||
): Promise<FetchLatestDocumentsResponse> {
|
): Promise<FetchLatestDocumentsResponse> {
|
||||||
return this.withRetries(async () => {
|
return this.retryForever(async () => {
|
||||||
const url = new URL(this.getUrl("/documents"));
|
const url = new URL(this.getUrl("/documents"));
|
||||||
if (since !== undefined) {
|
if (since !== undefined) {
|
||||||
url.searchParams.append("since", since.toString());
|
url.searchParams.append("since", since.toString());
|
||||||
|
|
@ -308,7 +303,10 @@ export class SyncService {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public async checkConnection(): Promise<CheckConnectionResult> {
|
public async checkConnection(): Promise<{
|
||||||
|
isSuccessful: boolean;
|
||||||
|
message: string;
|
||||||
|
}> {
|
||||||
try {
|
try {
|
||||||
const response = await this.pingClient(this.getUrl("/ping"), {
|
const response = await this.pingClient(this.getUrl("/ping"), {
|
||||||
headers: this.getDefaultHeaders()
|
headers: this.getDefaultHeaders()
|
||||||
|
|
@ -362,7 +360,7 @@ export class SyncService {
|
||||||
return headers;
|
return headers;
|
||||||
}
|
}
|
||||||
|
|
||||||
private async withRetries<T>(fn: () => Promise<T>): Promise<T> {
|
private async retryForever<T>(fn: () => Promise<T>): Promise<T> {
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
||||||
while (true) {
|
while (true) {
|
||||||
try {
|
try {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue