MOBILE-2310 core: Fix issues with WS cache

main
Dani Palou 2018-01-03 13:11:05 +01:00
parent 764309edd7
commit 5aef9af45b
3 changed files with 32 additions and 18 deletions

View File

@ -482,14 +482,7 @@ export class CoreSite {
// We pass back a clone of the original object, this may // We pass back a clone of the original object, this may
// prevent errors if in the callback the object is modified. // prevent errors if in the callback the object is modified.
if (typeof response == 'object') { return this.utils.clone(response);
if (Array.isArray(response)) {
return Array.from(response);
} else {
return Object.assign({}, response);
}
}
return response;
}).catch((error) => { }).catch((error) => {
if (error.errorcode == 'invalidtoken' || if (error.errorcode == 'invalidtoken' ||
(error.errorcode == 'accessexception' && error.message.indexOf('Invalid token - token expired') > -1)) { (error.errorcode == 'accessexception' && error.message.indexOf('Invalid token - token expired') > -1)) {

View File

@ -307,15 +307,14 @@ export class CoreUtilsProvider {
* *
* @param {any} from Object to copy the properties from. * @param {any} from Object to copy the properties from.
* @param {any} to Object where to store the properties. * @param {any} to Object where to store the properties.
* @param {boolean} [clone=true] Whether the properties should be cloned (so they are different instances).
*/ */
copyProperties(from: any, to: any) : void { copyProperties(from: any, to: any, clone = true) : void {
for (let name in from) { for (let name in from) {
let value = from[name]; if (clone) {
if (typeof value == 'object') { to[name] = this.clone(from[name]);
// Clone the object.
to[name] = Object.assign({}, value);
} else { } else {
to[name] = value; to[name] = from[name];
} }
} }
} }
@ -413,15 +412,16 @@ export class CoreUtilsProvider {
for (let name in obj) { for (let name in obj) {
if (!obj.hasOwnProperty(name)) continue; if (!obj.hasOwnProperty(name)) continue;
if (typeof obj[name] == 'object') { let value = obj[name];
let flatObject = this.flattenObject(obj[name]); if (typeof value == 'object' && !Array.isArray(value)) {
let flatObject = this.flattenObject(value);
for (let subName in flatObject) { for (let subName in flatObject) {
if (!flatObject.hasOwnProperty(subName)) continue; if (!flatObject.hasOwnProperty(subName)) continue;
toReturn[name + '.' + subName] = flatObject[subName]; toReturn[name + '.' + subName] = flatObject[subName];
} }
} else { } else {
toReturn[name] = obj[name]; toReturn[name] = value;
} }
} }
@ -1109,7 +1109,26 @@ export class CoreUtilsProvider {
* @return {string} Stringified object. * @return {string} Stringified object.
*/ */
sortAndStringify(obj: object) : string { sortAndStringify(obj: object) : string {
return JSON.stringify(obj, Object.keys(this.flattenObject(obj)).sort()); return JSON.stringify(this.sortProperties(obj));
}
/**
* Given an object, sort its properties and the properties of all the nested objects.
*
* @param {object} obj The object to sort. If it isn't an object, the original value will be returned.
* @return {object} Sorted object.
*/
sortProperties(obj: object) : object {
if (typeof obj == 'object' && !Array.isArray(obj)) {
// It's an object, sort it.
return Object.keys(obj).sort().reduce((accumulator, key) => {
// Always call sort with the value. If it isn't an object, the original value will be returned.
accumulator[key] = this.sortProperties(obj[key]);
return accumulator;
}, {});
} else {
return obj;
}
} }
/** /**

View File

@ -126,6 +126,8 @@ export class CoreWSProvider {
preSets.responseExpected = true; preSets.responseExpected = true;
} }
data = data || {};
data = this.utils.clone(data); // Clone the data so the changes don't affect the original data.
data.wsfunction = method; data.wsfunction = method;
data.wstoken = preSets.wsToken; data.wstoken = preSets.wsToken;
siteUrl = preSets.siteUrl + '/webservice/rest/server.php?moodlewsrestformat=json'; siteUrl = preSets.siteUrl + '/webservice/rest/server.php?moodlewsrestformat=json';