Skip to content

Commit

Permalink
Add a size method to the Queue class
Browse files Browse the repository at this point in the history
  • Loading branch information
philipwalton committed Apr 8, 2019
1 parent bdc37e6 commit e55bcd7
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 1 deletion.
9 changes: 9 additions & 0 deletions packages/workbox-background-sync/Queue.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,15 @@ class Queue {
return this._removeRequest('shift');
}

/**
* Returns the number of entries in the store.
*
* @return {number}
*/
async size() {
return await this._queueStore.size();
}

/**
* Adds the entry to the QueueStore and registers for a sync event.
*
Expand Down
16 changes: 16 additions & 0 deletions packages/workbox-background-sync/lib/QueueStore.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,22 @@ export class QueueStore {
return this._removeEntry({direction: 'next'});
}

/**
* Returns the number of entries in the store.
*
* @return {number}
* @private
*/
async size() {
const cb = (txn, done) => {
txn.objectStore(OBJECT_STORE_NAME)
.index(INDEXED_PROP)
.count(IDBKeyRange.only(this._queueName))
.onsuccess = ({target}) => done(target.result);
};
return await this._db.transaction([OBJECT_STORE_NAME], 'readonly', cb);
}

/**
* Removes and returns the first or last entry in the queue (based on the
* `direction` argument) matching the `queueName`.
Expand Down
1 change: 0 additions & 1 deletion packages/workbox-expiration/Plugin.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import './_version.mjs';
* used immediately.
*
* When using `maxEntries`, the entry least-recently requested will be removed from the cache first.
*
*
* @memberof workbox.expiration
*/
Expand Down
31 changes: 31 additions & 0 deletions test/workbox-background-sync/sw/lib/test-QueueStore.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -528,4 +528,35 @@ describe(`QueueStore`, function() {
expect(entries[2].id).to.equal(firstId + 2);
});
});

describe(`size`, function() {
it(`should return the number of entries in the store`, 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()});
await queueStore2.pushEntry({requestData: sr2.toObject()});

expect(await queueStore1.size()).to.equal(1);
expect(await queueStore2.size()).to.equal(1);

await queueStore2.pushEntry({requestData: sr3.toObject()});
await queueStore2.pushEntry({requestData: sr4.toObject()});
await queueStore1.pushEntry({requestData: sr5.toObject()});

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

await db.clear('requests');

expect(await queueStore1.size()).to.equal(0);
expect(await queueStore2.size()).to.equal(0);
});
});
});
11 changes: 11 additions & 0 deletions test/workbox-background-sync/sw/test-Queue.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -700,4 +700,15 @@ describe(`Queue`, function() {
await queue.registerSync();
});
});

describe(`size`, function() {
it(`should return the size of the QueueStore instance`, async function() {
sandbox.stub(QueueStore.prototype, 'size').returns(10);

const queue = new Queue('a');
expect(await queue.size()).to.equal(10);

expect(QueueStore.prototype.size.callCount).to.equal(1);
});
});
});

0 comments on commit e55bcd7

Please sign in to comment.