@cubejs-client/core
Advanced tools
Comparing version 0.2.5 to 0.2.6
@@ -26,2 +26,3 @@ 'use strict'; | ||
var _asyncToGenerator = _interopDefault(require('@babel/runtime/helpers/asyncToGenerator')); | ||
require('core-js/modules/es6.promise'); | ||
var whatwgFetch = require('whatwg-fetch'); | ||
@@ -408,3 +409,15 @@ | ||
var API_URL = "https://statsbot.co/cubejs-api/v1"; | ||
var mutexCounter = 0; | ||
var MUTEX_ERROR = 'Mutex has been changed'; | ||
var mutexPromise = function mutexPromise(promise) { | ||
return new Promise(function (resolve, reject) { | ||
promise.then(function (r) { | ||
return resolve(r); | ||
}, function (e) { | ||
return e !== MUTEX_ERROR && reject(e); | ||
}); | ||
}); | ||
}; | ||
var CubejsApi = | ||
@@ -434,2 +447,4 @@ /*#__PURE__*/ | ||
value: function loadMethod(request, toResult, options, callback) { | ||
var mutexValue = ++mutexCounter; | ||
if (typeof options === 'function' && !callback) { | ||
@@ -441,3 +456,14 @@ callback = options; | ||
options = options || {}; | ||
var mutexKey = options.mutexKey || 'default'; | ||
if (options.mutexObj) { | ||
options.mutexObj[mutexKey] = mutexValue; | ||
} | ||
var checkMutex = function checkMutex() { | ||
if (options.mutexObj && options.mutexObj[mutexKey] !== mutexValue) { | ||
throw MUTEX_ERROR; | ||
} | ||
}; | ||
var loadImpl = | ||
@@ -461,20 +487,23 @@ /*#__PURE__*/ | ||
if (!(response.status === 502)) { | ||
_context.next = 5; | ||
_context.next = 6; | ||
break; | ||
} | ||
checkMutex(); | ||
return _context.abrupt("return", loadImpl()); | ||
case 5: | ||
_context.next = 7; | ||
case 6: | ||
_context.next = 8; | ||
return response.json(); | ||
case 7: | ||
case 8: | ||
body = _context.sent; | ||
if (!(body.error === 'Continue wait')) { | ||
_context.next = 11; | ||
_context.next = 13; | ||
break; | ||
} | ||
checkMutex(); | ||
if (options.progressCallback) { | ||
@@ -486,14 +515,16 @@ options.progressCallback(new ProgressResult(body)); | ||
case 11: | ||
case 13: | ||
if (!(response.status !== 200)) { | ||
_context.next = 13; | ||
_context.next = 16; | ||
break; | ||
} | ||
checkMutex(); | ||
throw new Error(body.error); | ||
case 13: | ||
case 16: | ||
checkMutex(); | ||
return _context.abrupt("return", toResult(body)); | ||
case 14: | ||
case 18: | ||
case "end": | ||
@@ -512,3 +543,3 @@ return _context.stop(); | ||
if (callback) { | ||
loadImpl().then(function (r) { | ||
mutexPromise(loadImpl()).then(function (r) { | ||
return callback(null, r); | ||
@@ -519,3 +550,3 @@ }, function (e) { | ||
} else { | ||
return loadImpl(); | ||
return mutexPromise(loadImpl()); | ||
} | ||
@@ -522,0 +553,0 @@ } |
{ | ||
"name": "@cubejs-client/core", | ||
"version": "0.2.5", | ||
"version": "0.2.6", | ||
"description": "cube.js client", | ||
@@ -5,0 +5,0 @@ "main": "dist/cubejs-client-core.js", |
@@ -8,2 +8,12 @@ import { fetch } from 'whatwg-fetch'; | ||
let mutexCounter = 0; | ||
const MUTEX_ERROR = 'Mutex has been changed'; | ||
const mutexPromise = (promise) => { | ||
return new Promise((resolve, reject) => { | ||
promise.then(r => resolve(r), e => e !== MUTEX_ERROR && reject(e)); | ||
}) | ||
}; | ||
class CubejsApi { | ||
@@ -24,2 +34,3 @@ constructor(apiToken, options) { | ||
loadMethod(request, toResult, options, callback) { | ||
const mutexValue = ++mutexCounter; | ||
if (typeof options === 'function' && !callback) { | ||
@@ -32,6 +43,17 @@ callback = options; | ||
const mutexKey = options.mutexKey || 'default'; | ||
if (options.mutexObj) { | ||
options.mutexObj[mutexKey] = mutexValue; | ||
} | ||
const checkMutex = () => { | ||
if (options.mutexObj && options.mutexObj[mutexKey] !== mutexValue) { | ||
throw MUTEX_ERROR; | ||
} | ||
}; | ||
const loadImpl = async () => { | ||
const response = await request(); | ||
if (response.status === 502) { | ||
checkMutex(); | ||
return loadImpl(); // TODO backoff wait | ||
@@ -41,2 +63,3 @@ } | ||
if (body.error === 'Continue wait') { | ||
checkMutex(); | ||
if (options.progressCallback) { | ||
@@ -48,10 +71,12 @@ options.progressCallback(new ProgressResult(body)); | ||
if (response.status !== 200) { | ||
checkMutex(); | ||
throw new Error(body.error); // TODO error class | ||
} | ||
checkMutex(); | ||
return toResult(body); | ||
}; | ||
if (callback) { | ||
loadImpl().then(r => callback(null, r), e => callback(e)); | ||
mutexPromise(loadImpl()).then(r => callback(null, r), e => callback(e)); | ||
} else { | ||
return loadImpl(); | ||
return mutexPromise(loadImpl()); | ||
} | ||
@@ -58,0 +83,0 @@ } |
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
456567
13063