-
Notifications
You must be signed in to change notification settings - Fork 14
/
load-svg-cached.ts
46 lines (39 loc) · 1.13 KB
/
load-svg-cached.ts
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
import cache from './cache'
import cloneSvg from './clone-svg'
import makeAjaxRequest from './make-ajax-request'
import { processRequestQueue, queueRequest } from './request-queue'
import { Errback } from './types'
const loadSvgCached = (
url: string,
httpRequestWithCredentials: boolean,
callback: Errback,
) => {
if (cache.has(url)) {
const cacheValue = cache.get(url)
if (cacheValue === undefined) {
queueRequest(url, callback)
return
}
/* istanbul ignore else */
if (cacheValue instanceof SVGSVGElement) {
callback(null, cloneSvg(cacheValue))
return
}
// Errors are always refetched.
}
// Seed the cache to indicate we are loading this URL.
cache.set(url, undefined)
queueRequest(url, callback)
makeAjaxRequest(url, httpRequestWithCredentials, (error, httpRequest) => {
/* istanbul ignore else */
if (error) {
cache.set(url, error)
} else if (
httpRequest.responseXML?.documentElement instanceof SVGSVGElement
) {
cache.set(url, httpRequest.responseXML.documentElement)
}
processRequestQueue(url)
})
}
export default loadSvgCached