-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🎁 Improve data fetching & store words locally (#65)
* Use generator function to recursively fetch words * Check for localforage data before fetching
- Loading branch information
1 parent
46a1674
commit 2b29ec8
Showing
5 changed files
with
141 additions
and
29 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import localforage from 'localforage' | ||
|
||
const ENDPOINT = 'https://cdn.contentful.com/spaces/8j8wvx07a2uv/entries' | ||
const TOKEN = 'f582803bba0fe0513deecb0f9edf8e0e0d31c631247ccc64d7d99087e7a75e85' | ||
|
||
localforage.config({ | ||
name: 'Gibson Ipsum', | ||
storeName: 'gibsonipsum', | ||
description: 'Store array of words for offline use.', | ||
}) | ||
|
||
function* fetchAllWords({ url, nextSkip }) { | ||
let shouldFetch = true | ||
|
||
// Loop forever, yielding the results of the ajax call. | ||
while (true) { | ||
yield fetch(url) | ||
.then((resp) => resp.json()) | ||
.then((resp) => { | ||
let { items, skip, total } = resp | ||
|
||
if (items && items.length > 0) { | ||
/** | ||
* Set the endpoint URL and boolean flag for the | ||
* next time this generator runs. | ||
*/ | ||
nextSkip = skip + 100 | ||
url = `${ENDPOINT}?access_token=${TOKEN}&skip=${nextSkip}` | ||
shouldFetch = skip + items.length < total | ||
|
||
return { words: items, shouldFetch } | ||
} else { | ||
return null | ||
} | ||
}) | ||
} | ||
} | ||
|
||
export async function fetchWords(opts) { | ||
/** | ||
* Initialize the array where we store the words (only called the | ||
* first time through this function). | ||
*/ | ||
if (!opts.hasOwnProperty('wordlist')) { | ||
opts.wordlist = [] | ||
} | ||
|
||
/** | ||
* Make the initial call to get the generator, specifying the url | ||
* to get the first page of data. | ||
*/ | ||
if (!opts.hasOwnProperty('fetch')) { | ||
opts.fetch = await fetchAllWords({ | ||
url: `${ENDPOINT}?access_token=${TOKEN}`, | ||
nextSkip: 0, | ||
}) | ||
} | ||
|
||
let { fetch, wordlist, onComplete } = opts | ||
let next = fetch.next() | ||
|
||
/** | ||
* Get the result of the most recent ajax call from the | ||
* generator function. | ||
*/ | ||
next.value.then(({ words, shouldFetch }) => { | ||
/** | ||
* Append words to main list and either call this function | ||
* again, or the callback function. | ||
*/ | ||
if (words) { | ||
wordlist = wordlist.concat(words) | ||
shouldFetch | ||
? fetchWords({ fetch, wordlist, onComplete }) | ||
: onComplete(wordlist) | ||
} else { | ||
onComplete(wordlist) | ||
} | ||
}) | ||
} | ||
|
||
export const retrieveWords = () => { | ||
return localforage.getItem('words').then((res) => { | ||
return res | ||
}) | ||
} | ||
|
||
export const storeWords = (wordsArray) => { | ||
return localforage | ||
.setItem('words', wordsArray) | ||
.then((value) => { | ||
return value | ||
}) | ||
.catch((err) => { | ||
console.log('There was an error storing the words.', err) | ||
}) | ||
} |
2b29ec8
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully aliased the URL https://gibsonipsum-pj7xil2xu.now.sh to the following aliases: