From 5cc5394f8efb15209e1f0476534a06ab91b4b545 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Tue, 12 Mar 2019 09:30:37 +0100 Subject: [PATCH] MOBILE-2911 site: Clone response when reusing cache promise --- src/classes/site.ts | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/classes/site.ts b/src/classes/site.ts index bd24fe482..04d133a2c 100644 --- a/src/classes/site.ts +++ b/src/classes/site.ts @@ -576,7 +576,12 @@ export class CoreSite { // Check for an ongoing identical request if we're not ignoring cache. if (preSets.getFromCache && this.ongoingRequests[cacheId]) { - return this.ongoingRequests[cacheId]; + return this.ongoingRequests[cacheId].then((response) => { + // Clone the data, this may prevent errors if in the callback the object is modified. + return this.utils.clone(response); + }).catch((error) => { + return Promise.reject(this.utils.clone(error)); + }); } const promise = this.getFromCache(method, data, preSets, false, originalData).catch(() => { @@ -586,8 +591,7 @@ export class CoreSite { this.saveToCache(method, data, response, preSets); } - // We pass back a clone of the original object, this may prevent errors if in the callback the object is modified. - return this.utils.clone(response); + return response; }).catch((error) => { if (error.errorcode == 'invalidtoken' || (error.errorcode == 'accessexception' && error.message.indexOf('Invalid token - token expired') > -1)) { @@ -694,7 +698,12 @@ export class CoreSite { } }); - return promise; + return promise.then((response) => { + // We pass back a clone of the original object, this may prevent errors if in the callback the object is modified. + return this.utils.clone(response); + }).catch((error) => { + return Promise.reject(this.utils.clone(error)); + }); } /**