From 543d0fca5734301740f14ee0974c25a1b56981c3 Mon Sep 17 00:00:00 2001 From: rr- Date: Mon, 11 Apr 2016 18:45:58 +0200 Subject: [PATCH] client/api: cache API requests Specifically, cache all GET requests until first POST/PUT/DELETE request. --- client/js/api.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/client/js/api.js b/client/js/api.js index 457dccd..0e20058 100644 --- a/client/js/api.js +++ b/client/js/api.js @@ -10,21 +10,33 @@ class Api { this.user = null; this.userName = null; this.userPassword = null; + this.cache = {}; } 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) { + this.cache = {}; return this._process(url, request.post, data, files); } put(url, data, files) { + this.cache = {}; return this._process(url, request.put, data, files); } delete(url) { + this.cache = {}; return this._process(url, request.delete); }