-
-
Notifications
You must be signed in to change notification settings - Fork 36.4k
Expand file tree
/
Copy pathCache.js
More file actions
115 lines (85 loc) · 2.14 KB
/
Cache.js
File metadata and controls
115 lines (85 loc) · 2.14 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/**
* @class
* @classdesc A simple caching system, used internally by {@link FileLoader}.
* To enable caching across all loaders that use {@link FileLoader}, add `THREE.Cache.enabled = true.` once in your app.
* @hideconstructor
*/
const Cache = {
/**
* Whether caching is enabled or not.
*
* @static
* @type {boolean}
* @default false
*/
enabled: false,
/**
* A dictionary that holds cached files.
*
* @static
* @type {Object<string,Object>}
*/
files: {},
/**
* Adds a cache entry with a key to reference the file. If this key already
* holds a file, it is overwritten.
*
* @static
* @param {string} key - The key to reference the cached file.
* @param {Object} file - The file to be cached.
*/
add: function ( key, file ) {
if ( this.enabled === false ) return;
if ( isBlobURL( key ) ) return;
// log( 'Cache', 'Adding key:', key );
this.files[ key ] = file;
},
/**
* Gets the cached value for the given key.
*
* @static
* @param {string} key - The key to reference the cached file.
* @return {Object|undefined} The cached file. If the key does not exist `undefined` is returned.
*/
get: function ( key ) {
if ( this.enabled === false ) return;
if ( isBlobURL( key ) ) return;
// log( 'Cache', 'Checking key:', key );
return this.files[ key ];
},
/**
* Removes the cached file associated with the given key.
*
* @static
* @param {string} key - The key to reference the cached file.
*/
remove: function ( key ) {
delete this.files[ key ];
},
/**
* Remove all values from the cache.
*
* @static
*/
clear: function () {
this.files = {};
}
};
/**
* Returns true if the given cache key contains the blob: scheme.
*
* @private
* @param {string} key - The cache key.
* @return {boolean} Whether the given cache key contains the blob: scheme or not.
*/
function isBlobURL( key ) {
try {
const urlString = key.slice( key.indexOf( ':' ) + 1 ); // remove type identifier
const url = new URL( urlString );
return url.protocol === 'blob:';
} catch ( e ) {
// If the string is not a valid URL, it throws an error
return false;
}
}
export { Cache };