Skip to content

Commit

Permalink
addressing promises
Browse files Browse the repository at this point in the history
  • Loading branch information
prateekbh committed Jan 5, 2018
1 parent 2e6c392 commit 9e6125f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
7 changes: 3 additions & 4 deletions packages/workbox-background-sync/Queue.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,6 @@ class Queue {
* `queueDidReplay` callback is invoked (which implies the queue is
* now empty). If any of the requests fail, a new sync registration is
* created to retry again later.
*
* @return {Promise}
*/
async replayRequests() {
const now = Date.now();
Expand Down Expand Up @@ -137,13 +135,14 @@ class Queue {
await this._runCallback('queueDidReplay', replayedRequests);

// If any requests failed, put the failed requests back in the queue
// and reject promise.
// and rethrow the failed requests count.
if (failedRequests.length) {
await Promise.all(failedRequests.map((storableRequest) => {
return this._queueStore.addEntry(storableRequest);
}));

return Promise.reject(failedRequests);
throw new WorkboxError('queue-replay-failed',
{name: this._name, count: failedRequests.length});
}
}

Expand Down
4 changes: 4 additions & 0 deletions packages/workbox-core/models/messages/messages.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ export default {
`registered.`;
},

'queue-replay-failed': ({name, count}) => {
return `${count} requests failed, while trying to replay Queue: ${name}.`;
},

'duplicate-queue-name': ({name}) => {
return `The Queue name '${name}' is already being used. ` +
`All instances of backgroundSync.Queue must be given unique names.`;
Expand Down
10 changes: 5 additions & 5 deletions test/workbox-background-sync/node/lib/test-Queue.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {DB_NAME, OBJECT_STORE_NAME} from
import {DBWrapper} from '../../../../packages/workbox-core/_private/DBWrapper.mjs';
import {resetEventListeners} from
'../../../../infra/testing/sw-env-mocks/event-listeners.js';
import {WorkboxError} from '../../../../packages/workbox-core/_private/WorkboxError.mjs';

const getObjectStoreEntries = async () => {
return await new DBWrapper(DB_NAME, 1).getAll(OBJECT_STORE_NAME);
Expand Down Expand Up @@ -304,10 +305,10 @@ describe(`[workbox-background-sync] Queue`, function() {
return;
}

return Promise.reject('should have exit from catch');
throw new Error('should have exit from catch');
});

it(`should reject replayRequests promise if re-fetching fails`,
it(`should throw WrokboxError if re-fetching fails`,
async function() {
sandbox.stub(self, 'fetch')
.onCall(1).rejects(new Error())
Expand All @@ -324,12 +325,11 @@ describe(`[workbox-background-sync] Queue`, function() {
try {
await queue.replayRequests(); // The second request should fail.
} catch (error) {
expect(error.length).to.be.equal(1);
expect(error[0].url).to.be.equal(failureURL);
expect(error).to.be.instanceof(WorkboxError);
return;
}

return Promise.reject('should have exit from catch');
throw new Error('should have exit from catch');
});

it(`should invoke all replay callbacks`, async function() {
Expand Down

0 comments on commit 9e6125f

Please sign in to comment.