client/api: cache API requests

Specifically, cache all GET requests until first POST/PUT/DELETE
request.
This commit is contained in:
rr- 2016-04-11 18:45:58 +02:00
parent 256a4e49b9
commit 543d0fca57
1 changed files with 13 additions and 1 deletions

View File

@ -10,21 +10,33 @@ class Api {
this.user = null; this.user = null;
this.userName = null; this.userName = null;
this.userPassword = null; this.userPassword = null;
this.cache = {};
} }
get(url) { get(url) {
return this._process(url, request.get); if (url in this.cache) {
return new Promise((resolve, reject) => {
resolve(this.cache[url]);
});
}
return this._process(url, request.get).then(response => {
this.cache[url] = response;
return Promise.resolve(response);
});
} }
post(url, data, files) { post(url, data, files) {
this.cache = {};
return this._process(url, request.post, data, files); return this._process(url, request.post, data, files);
} }
put(url, data, files) { put(url, data, files) {
this.cache = {};
return this._process(url, request.put, data, files); return this._process(url, request.put, data, files);
} }
delete(url) { delete(url) {
this.cache = {};
return this._process(url, request.delete); return this._process(url, request.delete);
} }