FeedFetcher
Documentation for v2.7
What is this for
This package is meant for the browser context.
In order to make scheduled fetch requests (usually for data feeds) easier and less annoying when it comes to exceptions and errors, feed fetcher handles most of it internally while providing an interface that is very similar to what you are used in the browser: setTimeout/setInterval, it also allows you to feed things once, or use the former methods and clearInterval/clearTimeout as you would normally.
For the error types when fetching look at the error types section.
Importing [Webpack]
Import into a webpack project by using
import feedFetcher from 'feed-fetcher'
If you need to support older browsers that don't have Promise, fetch, or requestAnimationFrame make sure you include polyfills before importing anything from FeedFetcher
import './your-polyfills'
import feedFetcher from 'feed-fetcher'
Importing [Script Tag]
NOTE: Reminder to include fetch and Promise polyfills for browsers like internet explorer, BEFORE including the FeedFetcher script
<script src="https://cdn.jsdelivr.net/npm/promise-polyfill@8/dist/polyfill.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/whatwg-fetch@2.0.4/fetch.min.js"> </script>
<script src="FeedFetcher.min.js"> </script>
window.feedFetcher
Using setInterval and clearInterval
let intervalID = feedFetcher.setInterval({
url: 'https://somewebsite.com/file.json',
onData: (data, prevData) => {
console.log('New Data:', data)
feedFetcher.clearInterval(intervalID)
}
}, 10000)
setInterval parameters
let options = {
url: '/file1.json',
mode: 'json',
refreshRate: 5000,
timeout: 15000,
onData: (data, prevData) => {},
onSameData: (data) => {},
onError: (error) => {
}
}
Using setTimeout, clearTimeout and fetch
setTimeout and clearTimeout work identically to setInterval and clearInterval where the first parameter is the options, and the second parameter is milliseconds.
fetch
Fetch is a shorthand for fetching a feed just once and cancelling the subsequent fetching internally, no matter what the result ends up being.
feedFetcher.fetch({
url: '/',
timeout: 5000,
mode: 'text',
onData: (data) => {
console.log('Data received: ', data)
},
onError: (error) => {
console.warn('Error occurred with fetch: ', error)
}
})
Alternative parameters
You can also do the following shorthand for function parameters:
url[, mode], onData [, onError, onSameData], milliseconds
The mandatory parameters are url, onData, and milliseconds.
you can specify mode in between url and onData, and specify other method handlers before milliseconds.
feedFetcher.fetch('https://somewebsite.com/file.json', (data) => {
console.log(data)
})
feedFetcher.setInterval('/', 'text',
(data) => {
console.log(data)
},
(error) => {
console.error(error)
}
, 7500)
Error Types
By logging the error object with the onError callback, and reading the errorType property, we can know what went wrong.
error.errorType == 'FETCH_TIMEOUT'
error.errorType == 'JSON_PARSE_FAIL'
error.errorType == 'JSON_AND_TEXT_PARSE_FAIL'
error.errorType == 'BLOB_PARSE_FAIL'
error.errorType == 'BLOB_AND_TEXT_PARSE_FAIL'
error.errorType == 'FETCH_FAIL'
error.errorType == 'USER_METHOD_ERROR'
requestAnimationFrame Mode
If you are confident you will have requestAnimationFrame support on your target browser and you do not want the fetches to run when the page is tabbed out;
You can set the internal mode of feed fetcher to work via requestAnimationFrame.
Note that you can only change this when there are no feeds set up to run at an interval or timeout. So ideally, at the beginning of the app.
feedFetcher.setMode('raf')