Skip to content

Commit d4c0dbf

Browse files
author
Sebastian Kippe
authored
Merge pull request #1135 from remotestorage/bugfix/1133-max_age_handling_2
Refactor synced GET function
2 parents 7d6bb44 + 9555621 commit d4c0dbf

File tree

1 file changed

+15
-17
lines changed

1 file changed

+15
-17
lines changed

src/syncedgetputdelete.js

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,27 @@ function shareFirst(path) {
55
path.match(/^\/public\/.*[^\/]$/) );
66
}
77

8-
function maxAgeInvalid(maxAge) {
9-
return maxAge !== false && typeof(maxAge) !== 'number';
8+
function defaultMaxAge(context) {
9+
if ((typeof context.remote === 'object') &&
10+
context.remote.connected && context.remote.online) {
11+
return 2 * context.getSyncInterval();
12+
} else {
13+
log('Not setting default maxAge, because remote is offline or not connected');
14+
return false;
15+
}
1016
}
1117

1218
var SyncedGetPutDelete = {
1319
get: function (path, maxAge) {
14-
if (this.local) {
15-
if (maxAge === undefined) {
16-
if ((typeof this.remote === 'object') &&
17-
this.remote.connected && this.remote.online) {
18-
maxAge = 2*this.getSyncInterval();
19-
} else {
20-
log('Not setting default maxAge, because remote is offline or not connected');
21-
maxAge = false;
22-
}
23-
}
24-
25-
if (maxAgeInvalid(maxAge)) {
26-
return Promise.reject('Argument \'maxAge\' must be false or a number');
20+
if (!this.local) {
21+
return this.remote.get(path);
22+
} else {
23+
if (typeof maxAge === 'undefined') {
24+
maxAge = defaultMaxAge(this);
25+
} else if (typeof maxAge !== 'number' && maxAge !== false) {
26+
return Promise.reject(`Argument 'maxAge' must be 'false' or a number`);
2727
}
2828
return this.local.get(path, maxAge, this.sync.queueGetRequest.bind(this.sync));
29-
} else {
30-
return this.remote.get(path);
3129
}
3230
},
3331

0 commit comments

Comments
 (0)