From 9555621727a67e06f243993d1bdaf4ddc77e31c9 Mon Sep 17 00:00:00 2001 From: Sebastian Kippe Date: Tue, 30 Oct 2018 20:50:42 +0100 Subject: [PATCH] Refactor synced GET a little bit Simplify the code. Aside from being more readable, this fixes a problem with the UglifyJS compressor destroying the code by restructuring it wrong. closes #1133 --- src/syncedgetputdelete.js | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/src/syncedgetputdelete.js b/src/syncedgetputdelete.js index fa6f7e92d..ad2e035ea 100644 --- a/src/syncedgetputdelete.js +++ b/src/syncedgetputdelete.js @@ -5,29 +5,27 @@ function shareFirst(path) { path.match(/^\/public\/.*[^\/]$/) ); } -function maxAgeInvalid(maxAge) { - return maxAge !== false && typeof(maxAge) !== 'number'; +function defaultMaxAge(context) { + if ((typeof context.remote === 'object') && + context.remote.connected && context.remote.online) { + return 2 * context.getSyncInterval(); + } else { + log('Not setting default maxAge, because remote is offline or not connected'); + return false; + } } var SyncedGetPutDelete = { get: function (path, maxAge) { - if (this.local) { - if (maxAge === undefined) { - if ((typeof this.remote === 'object') && - this.remote.connected && this.remote.online) { - maxAge = 2*this.getSyncInterval(); - } else { - log('Not setting default maxAge, because remote is offline or not connected'); - maxAge = false; - } - } - - if (maxAgeInvalid(maxAge)) { - return Promise.reject('Argument \'maxAge\' must be false or a number'); + if (!this.local) { + return this.remote.get(path); + } else { + if (typeof maxAge === 'undefined') { + maxAge = defaultMaxAge(this); + } else if (typeof maxAge !== 'number' && maxAge !== false) { + return Promise.reject(`Argument 'maxAge' must be 'false' or a number`); } return this.local.get(path, maxAge, this.sync.queueGetRequest.bind(this.sync)); - } else { - return this.remote.get(path); } },