diff --git a/packages/workbox-background-sync/src/Queue.ts b/packages/workbox-background-sync/src/Queue.ts index 367ac2ebf..b90999f7d 100644 --- a/packages/workbox-background-sync/src/Queue.ts +++ b/packages/workbox-background-sync/src/Queue.ts @@ -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} + */ + async size(): Promise { + return await this._queueStore.size(); + } + /** * Adds the entry to the QueueStore and registers for a sync event. * diff --git a/packages/workbox-background-sync/src/lib/QueueDb.ts b/packages/workbox-background-sync/src/lib/QueueDb.ts index 62517959b..e12502642 100644 --- a/packages/workbox-background-sync/src/lib/QueueDb.ts +++ b/packages/workbox-background-sync/src/lib/QueueDb.ts @@ -86,6 +86,19 @@ export class QueueDb { return results ? results : new Array(); } + /** + * Returns the number of entries filtered by index + * + * @param queueName + * @return {Promise} + */ + async getEntryCountByQueueName( + queueName: string, + ): Promise { + const db = await this.getDb(); + return db.countFromIndex(REQUEST_OBJECT_STORE_NAME, QUEUE_NAME_INDEX, IDBKeyRange.only(queueName)); + } + /** * Deletes a single entry by id. * diff --git a/packages/workbox-background-sync/src/lib/QueueStore.ts b/packages/workbox-background-sync/src/lib/QueueStore.ts index de94c5509..077e62d51 100644 --- a/packages/workbox-background-sync/src/lib/QueueStore.ts +++ b/packages/workbox-background-sync/src/lib/QueueStore.ts @@ -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} + * @private + */ + async size(): Promise { + return await this._queueDb.getEntryCountByQueueName(this._queueName); + } + /** * Deletes the entry for the given ID. * diff --git a/test/workbox-background-sync/sw/lib/test-QueueDb.mjs b/test/workbox-background-sync/sw/lib/test-QueueDb.mjs index 59c31eb4b..d5d1c362f 100644 --- a/test/workbox-background-sync/sw/lib/test-QueueDb.mjs +++ b/test/workbox-background-sync/sw/lib/test-QueueDb.mjs @@ -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(); diff --git a/test/workbox-background-sync/sw/lib/test-QueueStore.mjs b/test/workbox-background-sync/sw/lib/test-QueueStore.mjs index 7ee7361a4..2e1aaff97 100644 --- a/test/workbox-background-sync/sw/lib/test-QueueStore.mjs +++ b/test/workbox-background-sync/sw/lib/test-QueueStore.mjs @@ -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'); diff --git a/test/workbox-background-sync/sw/test-Queue.mjs b/test/workbox-background-sync/sw/test-Queue.mjs index 11a6abcfa..efa46d066 100644 --- a/test/workbox-background-sync/sw/test-Queue.mjs +++ b/test/workbox-background-sync/sw/test-Queue.mjs @@ -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); + }); + }); });