Skip to content

Commit

Permalink
Add size() to expose amount of requests in queue (#2941)
Browse files Browse the repository at this point in the history
Note that expired entries (per `maxRetentionTime`) are also included in this count

Closes #2940
  • Loading branch information
StephanBijzitter committed Sep 29, 2021
1 parent 03055e6 commit 6753cf5
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 0 deletions.
10 changes: 10 additions & 0 deletions packages/workbox-background-sync/src/Queue.ts
Expand Up @@ -242,6 +242,16 @@ class Queue {
return unexpiredEntries;
}

/**
* Returns the number of entries present in the queue.
* Note that expired entries (per `maxRetentionTime`) are also included in this count.
*
* @return {Promise<number>}
*/
async size(): Promise<number> {
return await this._queueStore.size();
}

/**
* Adds the entry to the QueueStore and registers for a sync event.
*
Expand Down
13 changes: 13 additions & 0 deletions packages/workbox-background-sync/src/lib/QueueDb.ts
Expand Up @@ -86,6 +86,19 @@ export class QueueDb {
return results ? results : new Array<QueueStoreEntry>();
}

/**
* Returns the number of entries filtered by index
*
* @param queueName
* @return {Promise<number>}
*/
async getEntryCountByQueueName(
queueName: string,
): Promise<number> {
const db = await this.getDb();
return db.countFromIndex(REQUEST_OBJECT_STORE_NAME, QUEUE_NAME_INDEX, IDBKeyRange.only(queueName));
}

/**
* Deletes a single entry by id.
*
Expand Down
11 changes: 11 additions & 0 deletions packages/workbox-background-sync/src/lib/QueueStore.ts
Expand Up @@ -134,6 +134,17 @@ export class QueueStore {
return await this._queueDb.getAllEntriesByQueueName(this._queueName);
}

/**
* Returns the number of entries in the store matching the `queueName`.
*
* @param {Object} options See {@link module:workbox-background-sync.Queue~size}
* @return {Promise<number>}
* @private
*/
async size(): Promise<number> {
return await this._queueDb.getEntryCountByQueueName(this._queueName);
}

/**
* Deletes the entry for the given ID.
*
Expand Down
20 changes: 20 additions & 0 deletions test/workbox-background-sync/sw/lib/test-QueueDb.mjs
Expand Up @@ -350,6 +350,26 @@ describe(`QueueDb`, () => {
});
});

describe('getEntryCountByQueueName', () => {
it(`should return the number of entries in IDB filtered by index`, async () => {
const queueDb = new QueueDb();

await queueDb.addEntry(entry1);
await queueDb.addEntry(entry2);
await queueDb.addEntry(entry3);
await queueDb.addEntry(entry4);
await queueDb.addEntry(entry5);

expect(await queueDb.getEntryCountByQueueName('a')).to.equal(3);
expect(await queueDb.getEntryCountByQueueName('b')).to.equal(2);

await db.clear('requests');

expect(await queueDb.getEntryCountByQueueName('a')).to.equal(0);
expect(await queueDb.getEntryCountByQueueName('b')).to.equal(0);
});
});

describe('deleteEntry', () => {
it(`should delete an entry for the given ID`, async () => {
const queueDb = new QueueDb();
Expand Down
47 changes: 47 additions & 0 deletions test/workbox-background-sync/sw/lib/test-QueueStore.mjs
Expand Up @@ -449,6 +449,53 @@ describe(`QueueStore`, function () {
});
});

describe(`size`, function () {
it(`should return the number of entries in IDB with the right queue name`, async function () {
const queueStore1 = new QueueStore('a');
const queueStore2 = new QueueStore('b');

const sr1 = await StorableRequest.fromRequest(new Request('/one'));
const sr2 = await StorableRequest.fromRequest(new Request('/two'));
const sr3 = await StorableRequest.fromRequest(new Request('/three'));
const sr4 = await StorableRequest.fromRequest(new Request('/four'));
const sr5 = await StorableRequest.fromRequest(new Request('/five'));

await queueStore1.pushEntry({
requestData: sr1.toObject(),
timestamp: 1000,
metadata: {name: 'meta1'},
});
await queueStore2.pushEntry({
requestData: sr2.toObject(),
timestamp: 2000,
metadata: {name: 'meta2'},
});
await queueStore2.pushEntry({
requestData: sr3.toObject(),
timestamp: 3000,
metadata: {name: 'meta3'},
});
await queueStore2.pushEntry({
requestData: sr4.toObject(),
timestamp: 4000,
metadata: {name: 'meta4'},
});
await queueStore1.pushEntry({
requestData: sr5.toObject(),
timestamp: 5000,
metadata: {name: 'meta5'},
});

expect(await queueStore1.size()).to.equal(2);
expect(await queueStore2.size()).to.equal(3);

await db.clear('requests');

expect(await queueStore1.size()).to.deep.equal(0);
expect(await queueStore2.size()).to.deep.equal(0);
});
});

describe(`delete`, function () {
it(`should delete an entry for the given ID`, async function () {
const queueStore = new QueueStore('a');
Expand Down
19 changes: 19 additions & 0 deletions test/workbox-background-sync/sw/test-Queue.mjs
Expand Up @@ -811,4 +811,23 @@ describe(`Queue`, function () {
expect(await db.getAll('requests')).to.have.lengthOf(2);
});
});

describe(`size()`, function () {
it(`returns the number of requests in the QueueStore instance`, async function () {
const queue = new Queue('a');

const request1 = new Request('/one', {method: 'POST', body: '...'});
const request2 = new Request('/two', {method: 'POST', body: '...'});
const request3 = new Request('/three', {method: 'POST', body: '...'});

await queue.pushRequest({request: request1});
await queue.pushRequest({request: request2});
await queue.pushRequest({
request: request3,
metadata: {meta: 'data'},
});

expect(await queue.size()).to.equal(3);
});
});
});

0 comments on commit 6753cf5

Please sign in to comment.