-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconnection.js
44 lines (35 loc) · 1.08 KB
/
connection.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
'use strict';
const Debug = require('debug');
class Connection {
constructor(connection) {
this.connection = connection;
this.debug = new Debug('sharedb-promises:connection:debug');
this.error = new Debug('sharedb-promises:connection:error');
return this;
}
async fetchSnapshot() {
const { connection } = this;
const [ collection, id, version ] = arguments;
let _version = version || null; // if undefined needs to be forced to null;
if(_version) {
_version = parseInt(_version);
}
return new Promise((resolve, reject) => {
this.debug("fetchSnapshot >", collection, id, _version);
try {
connection.fetchSnapshot(collection, id, _version, (err, snapshot) => {
if (err) {
this.error("fetchSnapshot !", collection, id, _version, err, err.stack);
return reject(err);
}
this.debug("fetchSnapshot <", collection, id, _version, snapshot);
return resolve(snapshot);
});
} catch(err) {
this.error("fetchSnapshot !", collection, id, _version, err, err.stack);
return reject(err);
}
});
}
}
module.exports = Connection;