feat: cache resources

This commit is contained in:
cha0s 2019-03-20 18:26:55 -05:00
parent 6a791b4dc6
commit cc95512f39

View File

@ -10,9 +10,17 @@ export class Resource {
}
static read(uri) {
if (!this.loadCache) {
this.loadCache = {};
}
if (this.loadCache[uri]) {
return Promise.resolve(JSON.parse(JSON.stringify(this.loadCache[uri])));
}
if ('undefined' !== typeof window) {
return fetch(uri).then((response) => {
return response.json();
}).then((json) => {
return this.loadCache[uri] = json;
});
}
else {
@ -24,7 +32,7 @@ export class Resource {
if (error) {
return reject(error);
}
resolve(JSON.parse(buffer.toString('utf8')));
resolve(this.loadCache[uri] = JSON.parse(buffer.toString('utf8')));
});
});
}