-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfetch.js
More file actions
52 lines (44 loc) · 1.69 KB
/
Copy pathfetch.js
File metadata and controls
52 lines (44 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
const { fetch, FileReader, URL } = window
const dataUrlToBlobUrl = async (dataUrl) => {
const response = await fetch(dataUrl)
const blob = await response.blob()
return URL.createObjectURL(blob)
}
const blobToDataUrl = (blob) => new Promise((resolve) => {
const reader = new FileReader()
reader.addEventListener('load', () => resolve(reader.result), false)
reader.readAsDataURL(blob)
})
// TODO: use create function to prevent global config data
let FETCH_OPTION = { method: 'GET', cache: 'default', mode: 'cors', credentials: 'same-origin' }
const setFetchOption = (option = {}) => { FETCH_OPTION = { ...FETCH_OPTION, ...option } }
let CACHED_TEXT_FETCH_MAP = {} // cache fetch promise to prevent multi request for the same source
let CACHED_BLOB_FETCH_MAP = {} // cache fetch promise to prevent multi request for the same source
const resetFetchCache = () => {
CACHED_TEXT_FETCH_MAP = {}
CACHED_BLOB_FETCH_MAP = {}
}
const fetchTextWithCache = (url) => {
if (!CACHED_TEXT_FETCH_MAP[ url ]) {
__DEV__ && console.log('fetchTextWithCache', url)
CACHED_TEXT_FETCH_MAP[ url ] = fetch(url, FETCH_OPTION).then((response) => response.text())
}
return CACHED_TEXT_FETCH_MAP[ url ]
}
const fetchBlobWithCache = (url) => {
if (!CACHED_BLOB_FETCH_MAP[ url ]) {
__DEV__ && console.log('fetchBlobWithCache', url)
CACHED_BLOB_FETCH_MAP[ url ] = fetch(url, FETCH_OPTION).then((response) => response.blob())
}
return CACHED_BLOB_FETCH_MAP[ url ]
}
const fetchDataUrlWithCache = async (dataUrl) => blobToDataUrl(await fetchBlobWithCache(dataUrl))
export {
dataUrlToBlobUrl,
blobToDataUrl,
setFetchOption,
resetFetchCache,
fetchTextWithCache,
fetchBlobWithCache,
fetchDataUrlWithCache
}