Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor synced GET function #1135

Merged
merged 1 commit into from
Nov 1, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The log in this function is kind of misleading, as the function itself is not setting anything but just returning a value.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function is only used one time in the code below. It's never used for not setting the default max age. So I think it's OK in this case.

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