Skip to content

Commit e2c23f3

Browse files
committed
timeout selection on database sync
1 parent 8a84f0e commit e2c23f3

File tree

3 files changed

+11
-9
lines changed

3 files changed

+11
-9
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@bitxenia/astradb",
3-
"version": "0.1.0",
3+
"version": "0.1.1",
44
"description": "Database for astrawiki & astrachat ipfs using orbitdb & libp2p",
55
"author": "Bitxenia",
66
"license": "MIT",

src/database.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ export class Database {
4141
await this.updateDatabase();
4242
}
4343

44-
public async initExisting(): Promise<boolean> {
44+
public async initExisting(msTimeout: number): Promise<boolean> {
4545
await this.createDatabase();
46-
const synced = await this.syncDb();
46+
const synced = await this.syncDb(msTimeout);
4747
await this.setupDbEvents();
4848
await this.updateDatabase();
4949
return synced;
@@ -79,7 +79,7 @@ export class Database {
7979
this.openDb = db;
8080
}
8181

82-
private async syncDb(): Promise<boolean> {
82+
private async syncDb(msTimeout: number): Promise<boolean> {
8383
// We wait for the database to be synced with at least one provider.
8484
// This is because the database is empty until it is synced.
8585
let synced = false;
@@ -98,7 +98,9 @@ export class Database {
9898
try {
9999
await this.waitFor(
100100
async () => synced,
101-
async () => true
101+
async () => true,
102+
100,
103+
msTimeout
102104
);
103105
} catch (error) {
104106
if (error instanceof SyncTimeoutError) {
@@ -116,8 +118,8 @@ export class Database {
116118
private async waitFor(
117119
valueA: any,
118120
toBeValueB: any,
119-
pollInterval = 100,
120-
timeout = 20000 // 20 seconds | TODO: see if this is enough
121+
pollInterval: number,
122+
timeout: number
121123
): Promise<void> {
122124
// TODO: We use this slight modifided busy wait found in the OrbitDB codebase:
123125
// https://github.com/orbitdb/orbitdb/blob/main/test/utils/wait-for.js

src/keyRepository.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export class KeyRepository {
3838

3939
if (!this.isCollaborator && !offlineMode) {
4040
// If we are not a collaborator and we are not in offline mode, we open an existing database which sincronizes with the providers.
41-
const synced = await this.keyDb.initExisting();
41+
const synced = await this.keyDb.initExisting(60000);
4242
if (!synced) {
4343
// If we are not a collaborator and the db did not sync with the providers,
4444
// We throw an error because a non collaborator node cannot create a new astradb.
@@ -135,7 +135,7 @@ export class KeyRepository {
135135
valueDb = new Database(valueDbName, this.orbitdb, this.events);
136136
if (existing) {
137137
// If the database already exists, we open it and sync it.
138-
await valueDb.initExisting();
138+
await valueDb.initExisting(20000);
139139
} else {
140140
await valueDb.initNew();
141141
}

0 commit comments

Comments
 (0)