Skip to content

Commit

Permalink
Refactor synced GET a little bit
Browse files Browse the repository at this point in the history
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
  • Loading branch information
raucao committed Oct 31, 2018
1 parent 44672c5 commit 9555621
Showing 1 changed file with 15 additions and 17 deletions.
32 changes: 15 additions & 17 deletions src/syncedgetputdelete.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
},

Expand Down

0 comments on commit 9555621

Please sign in to comment.