Skip to content

Commit

Permalink
fix: include object stores without keyPath in the fetch loop
Browse files Browse the repository at this point in the history
  • Loading branch information
darrachequesne committed Oct 8, 2024
1 parent 05bdbb5 commit 66c927c
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 36 deletions.
46 changes: 21 additions & 25 deletions src/sync-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,42 +300,38 @@ abstract class Loop {
}
}

function sleep(duration: number) {
return new Promise((resolve) => setTimeout(resolve, duration));
}

class FetchLoop extends Loop {
async run() {
const storeNames = this.db.objectStoreNames;
for (let storeName of storeNames) {
if (
!IGNORED_STORES.includes(storeName) &&
!this.opts.withoutKeyPath[storeName]
) {
await this.tryFetchUpdates(storeName);
}
}
for (const storeName in this.opts.withoutKeyPath) {
for (const key of this.opts.withoutKeyPath[storeName]) {
await this.fetchUpdatesForKey(storeName, key);
}
}
}

private async tryFetchUpdates(storeName: string) {
if (!this.isRunning) {
return;
}
const storeNames = this.db.objectStoreNames;

let hasMore = false;
try {
hasMore = await this.fetchUpdates(storeName);
for (let storeName of storeNames) {
if (
!IGNORED_STORES.includes(storeName) &&
!this.opts.withoutKeyPath[storeName]
) {
while (await this.fetchUpdates(storeName)) {
await sleep(MIN_DELAY_BETWEEN_REQUESTS);
}
}
}
for (const storeName in this.opts.withoutKeyPath) {
for (const key of this.opts.withoutKeyPath[storeName]) {
await this.fetchUpdatesForKey(storeName, key);
}
}
} catch (e) {
this.manager.onfetcherror(e);
}

setTimeout(
() => {
this.tryFetchUpdates(storeName);
},
hasMore ? MIN_DELAY_BETWEEN_REQUESTS : this.opts.fetchInterval,
);
setTimeout(() => this.run(), this.opts.fetchInterval);
}

private async fetchUpdates(storeName: string): Promise<boolean> {
Expand Down
61 changes: 50 additions & 11 deletions test/sync-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ suite.only('SyncManager', () => {
test('no conflict', async () => {
const schemaDB = await openDBWithSchema();
db = schemaDB as IDBPDatabase;
manager = new SyncManager(db, BASE_URL);
manager = new SyncManager(db, BASE_URL, {
withoutKeyPath: {
'key-val-store': [],
},
});

manager.start();
await waitForFetchSuccess(manager);
Expand Down Expand Up @@ -78,7 +82,11 @@ suite.only('SyncManager', () => {
test('conflict', async () => {
const schemaDB = await openDBWithSchema();
db = schemaDB as IDBPDatabase;
manager = new SyncManager(db, BASE_URL);
manager = new SyncManager(db, BASE_URL, {
withoutKeyPath: {
'key-val-store': [],
},
});

// untracked
await db.add(
Expand Down Expand Up @@ -117,7 +125,11 @@ suite.only('SyncManager', () => {
test('tombstone', async () => {
const schemaDB = await openDBWithSchema();
db = schemaDB as IDBPDatabase;
manager = new SyncManager(db, BASE_URL);
manager = new SyncManager(db, BASE_URL, {
withoutKeyPath: {
'key-val-store': [],
},
});

// untracked
await db.add(
Expand Down Expand Up @@ -148,12 +160,15 @@ suite.only('SyncManager', () => {
db = schemaDB as IDBPDatabase;
manager = new SyncManager(db, BASE_URL, {
buildPath: (operation, storeName, key) => {
if (storeName !== "products") {
if (storeName !== 'products') {
return;
}
return "/company-products" + (key ? ("/" + key) : "");
return '/company-products' + (key ? '/' + key : '');
},
updatedAtAttribute: 'lastUpdateDate',
withoutKeyPath: {
'key-val-store': [],
},
});

manager.start();
Expand Down Expand Up @@ -215,7 +230,11 @@ suite.only('SyncManager', () => {
test('add', async () => {
const schemaDB = await openDBWithSchema();
db = schemaDB as IDBPDatabase;
manager = new SyncManager(db, BASE_URL);
manager = new SyncManager(db, BASE_URL, {
withoutKeyPath: {
'key-val-store': [],
},
});

await db.add('object-store', {
id: 3,
Expand Down Expand Up @@ -254,7 +273,11 @@ suite.only('SyncManager', () => {
test('put', async () => {
const schemaDB = await openDBWithSchema();
db = schemaDB as IDBPDatabase;
manager = new SyncManager(db, BASE_URL);
manager = new SyncManager(db, BASE_URL, {
withoutKeyPath: {
'key-val-store': [],
},
});

await db.add('object-store', {
id: 4,
Expand Down Expand Up @@ -294,7 +317,11 @@ suite.only('SyncManager', () => {
test('put (discard local)', async () => {
const schemaDB = await openDBWithSchema();
db = schemaDB as IDBPDatabase;
manager = new SyncManager(db, BASE_URL);
manager = new SyncManager(db, BASE_URL, {
withoutKeyPath: {
'key-val-store': [],
},
});

await db.put('object-store', {
id: 6,
Expand Down Expand Up @@ -326,7 +353,11 @@ suite.only('SyncManager', () => {
test('put (override remote)', async () => {
const schemaDB = await openDBWithSchema();
db = schemaDB as IDBPDatabase;
manager = new SyncManager(db, BASE_URL);
manager = new SyncManager(db, BASE_URL, {
withoutKeyPath: {
'key-val-store': [],
},
});

await db.put('object-store', {
id: 7,
Expand Down Expand Up @@ -359,7 +390,11 @@ suite.only('SyncManager', () => {
test('delete', async () => {
const schemaDB = await openDBWithSchema();
db = schemaDB as IDBPDatabase;
manager = new SyncManager(db, BASE_URL);
manager = new SyncManager(db, BASE_URL, {
withoutKeyPath: {
'key-val-store': [],
},
});

await db.add('object-store', {
id: 4,
Expand Down Expand Up @@ -466,7 +501,11 @@ suite.only('SyncManager', () => {
test('has pending changes', async () => {
const schemaDB = await openDBWithSchema();
db = schemaDB as IDBPDatabase;
manager = new SyncManager(db, BASE_URL);
manager = new SyncManager(db, BASE_URL, {
withoutKeyPath: {
'key-val-store': [],
},
});

// untracked
await db.add(
Expand Down

0 comments on commit 66c927c

Please sign in to comment.