Skip to content

Commit

Permalink
[Background Fetch] Throw DOMException for responseReady on abort.
Browse files Browse the repository at this point in the history
According to the spec, responseReady in BackgroundFetchRecord should
throw an AbortError DOMException if the fetch was abandoned.

https://wicg.github.io/background-fetch/#create-record-objects (2.4.3)

Change-Id: Ieadf278acd061e05b8822014d0934f050fcac702
  • Loading branch information
rayankans authored and chromium-wpt-export-bot committed Oct 17, 2018
1 parent eaf6fb2 commit 97ab4b4
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
14 changes: 13 additions & 1 deletion background-fetch/abort.https.window.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,16 @@ backgroundFetchTest(async (test, backgroundFetch) => {
};
});

}, 'Calling BackgroundFetchRegistration.abort sets the correct fields and responses are still available');
}, 'Calling BackgroundFetchRegistration.abort sets the correct fields and responses are still available');

backgroundFetchTest(async (test, backgroundFetch) => {
const registration = await backgroundFetch.fetch(
uniqueId(), '/serviceworker/resources/slow-response.php');
assert_true(await registration.abort());

const {results} = await getMessageFromServiceWorker();
assert_equals(results.length, 1);
assert_false(results[0].response);
assert_equals(results[0].name, 'AbortError');

}, 'An aborted fetch throws a DOM exception when accessing an incomplete record', 'sw-abort.js');
23 changes: 23 additions & 0 deletions background-fetch/service_workers/sw-abort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
importScripts('sw-helpers.js');

async function getFetchResult(record) {
try {
await record.responseReady;
} catch (e) {
return {
response: false,
name: e.name,
};
}

return {
response: true,
};
}
self.addEventListener('backgroundfetchabort', event => {
event.waitUntil(
event.registration.matchAll()
.then(records =>
Promise.all(records.map(record => getFetchResult(record))))
.then(results => sendMessageToDocument({results})));
});
8 changes: 4 additions & 4 deletions background-fetch/service_workers/sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ async function getFetchResult(record) {
};
}

function handleBackgroundFetchUpdateEvent(event) {
function handleBackgroundFetchEvent(event) {
event.waitUntil(
event.registration.matchAll()
.then(records =>
Expand All @@ -25,6 +25,6 @@ function handleBackgroundFetchUpdateEvent(event) {
}));
}

self.addEventListener('backgroundfetchsuccess', handleBackgroundFetchUpdateEvent);
self.addEventListener('backgroundfetchfail', handleBackgroundFetchUpdateEvent);
self.addEventListener('backgroundfetchabort', handleBackgroundFetchUpdateEvent);
self.addEventListener('backgroundfetchsuccess', handleBackgroundFetchEvent);
self.addEventListener('backgroundfetchfail', handleBackgroundFetchEvent);
self.addEventListener('backgroundfetchabort', handleBackgroundFetchEvent);

0 comments on commit 97ab4b4

Please sign in to comment.