Avoid conccurent _syncTime calls

This commit is contained in:
Stefan Aebischer
2020-01-15 14:40:10 -05:00
parent e5fd04afc1
commit 995cd6ed73

View File

@@ -15,6 +15,7 @@ module.exports = class Request {
constructor(key, secret, livenet=false) { constructor(key, secret, livenet=false) {
this.baseUrl = baseUrls[livenet === true ? 'livenet' : 'testnet']; this.baseUrl = baseUrls[livenet === true ? 'livenet' : 'testnet'];
this._timeOffset = null; this._timeOffset = null;
this._syncTimePromise = null;
if(key) assert(secret, 'Secret is required for private enpoints'); if(key) assert(secret, 'Secret is required for private enpoints');
@@ -105,6 +106,18 @@ module.exports = class Request {
} }
async _syncTime() { async _syncTime() {
this._timeOffset = await this.getTimeOffset(); if(this._syncTimePromise !== null) return this._syncTimePromise;
this._syncTimePromise = new Promise(async (resolve, reject) => {
try {
this._timeOffset = await this.getTimeOffset();
this._syncTimePromise = null;
resolve();
} catch(err) {
reject(err);
}
});
return this._syncTimePromise;
} }
} }