- From: Duan Yao <notifications@github.com>
- Date: Tue, 12 May 2015 22:17:48 -0700
- To: whatwg/fetch <fetch@noreply.github.com>
- Message-ID: <whatwg/fetch/issues/19/101516067@github.com>
I think this problem can be solved by add a new API: global fetch events.
These events should be fired when the page is fetching any resources, no matter caused by XHR, fetch, `<img>`, or CSS. These events should have a "affectBusyIndicator" setter to allow authors to decides whether a particular fetch can affect busy indicator of UAs. Of cause, global fetch events can also be used to implement a progress bar.
The suggested API follows MutationObserver's style, because this may be more efficient for large amount of events:
```javascript
var fetchObserver = new FetchObserver(onFetch);
fetchObserver.connect(window, { // connect to current window object
contexts: ['fetch', 'xmlhttprequest', 'form'], // filter by [fetch's contexts](https://fetch.spec.whatwg.org/#concept-request-context)
types: ['start', 'progress', 'end', 'error'], // filter by types of fetch events
methods: ['GET', 'POST'], // filter by methods
nested: true, // also observe nested browsing contexts, e.g. iframes, workers
});
function onFetch(fetchRecords) {
for (var i = 0; i < fetchRecords.length; i++) {
var fetchRecord = fetchRecords[i];
if (fetchRecord.type === 'start' &&
/^http:\/\/somedomain\.net\/myapp\//.test(fetchRecord.url)) { // filter by URL
fetchRecords[i].affectBusyIndicator = true; // inform the UA that this fetch should affect the busy indicator
} else if (fetchRecord.type === 'progress') {
// compute and update the progress bar
}
}
}
```
---
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/fetch/issues/19#issuecomment-101516067
Received on Wednesday, 13 May 2015 05:18:15 UTC