-
Notifications
You must be signed in to change notification settings - Fork 141
/
syncedgetputdelete.js
66 lines (58 loc) · 1.86 KB
/
syncedgetputdelete.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
const log = require('./log');
function shareFirst(path) {
return ( this.backend === 'dropbox' &&
path.match(/^\/public\/.*[^\/]$/) );
}
function maxAgeInvalid(maxAge) {
return maxAge !== false && typeof(maxAge) !== 'number';
}
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');
}
return this.local.get(path, maxAge, this.sync.queueGetRequest.bind(this.sync));
} else {
return this.remote.get(path);
}
},
put: function (path, body, contentType) {
if (shareFirst.bind(this)(path)) {
return SyncedGetPutDelete._wrapBusyDone.call(this, this.remote.put(path, body, contentType));
}
else if (this.local) {
return this.local.put(path, body, contentType);
} else {
return SyncedGetPutDelete._wrapBusyDone.call(this, this.remote.put(path, body, contentType));
}
},
'delete': function (path) {
if (this.local) {
return this.local.delete(path);
} else {
return SyncedGetPutDelete._wrapBusyDone.call(this, this.remote.delete(path));
}
},
_wrapBusyDone: function (result) {
var self = this;
this._emit('wire-busy');
return result.then(function (r) {
self._emit('wire-done', { success: true });
return Promise.resolve(r);
}, function (err) {
self._emit('wire-done', { success: false });
return Promise.reject(err);
});
}
};
module.exports = SyncedGetPutDelete;