Created
September 6, 2019 16:05
-
-
Save mrsharpoblunto/096675904ea7aecca6304be63b700db0 to your computer and use it in GitHub Desktop.
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
function queryAPI(path) { | |
const cacheEntry = window.__data[path]; | |
if (!cacheEntry) { | |
// issue a normal XHR API request | |
return fetch(path); | |
} else if (cacheEntry.data) { | |
// the server has pushed us the data already | |
return Promise.resolve(cacheEntry.data); | |
} else { | |
// the server is still pushing the data | |
// so we'll put ourselves in the queue to | |
// be notified when its ready | |
const waiting = {}; | |
cacheEntry.waiting.push(waiting); | |
return new Promise((resolve) => { | |
waiting.resolve = resolve; | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment