@exchanges/binance
Version:
Exchange provider for Binance API
117 lines • 4.95 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.userDataCall = void 0;
const apiCall_1 = require("../apiCall");
const createListenKey_1 = require("./createListenKey");
const pingListenKey_1 = require("./pingListenKey");
const closeListenKey_1 = require("./closeListenKey");
function userDataCall(options, callback) {
if (!options)
throw new TypeError('userDataCall options is empty');
if (typeof options !== 'object')
throw new TypeError('userDataCall options is not object');
if (!callback)
throw new TypeError('userDataCall callback is empty');
if (typeof callback !== 'function')
throw new TypeError('userDataCall callback is not function');
if (!options.client)
throw new TypeError('userDataCall options.client is empty');
if (!options.host)
throw new TypeError('userDataCall options.host is empty');
if (!options.path)
throw new TypeError('userDataCall options.path is empty');
let currentListenKey = null;
let currentUnlistener = null;
let currentInterval = null;
let currentRecreateTimeout = null;
const close = () => __awaiter(this, void 0, void 0, function* () {
// Закрытие внутренних событий
try {
if (currentRecreateTimeout) {
clearTimeout(currentRecreateTimeout);
currentRecreateTimeout = null;
}
if (currentInterval) {
clearInterval(currentInterval);
currentInterval = null;
}
if (currentListenKey) {
yield (0, closeListenKey_1.closeListenKey)({
client: options.client,
host: options.host,
path: options.path,
listenKey: currentListenKey,
data: options.data,
});
currentListenKey = null;
}
if (currentUnlistener) {
currentUnlistener();
currentUnlistener = null;
}
}
catch (err) {
currentRecreateTimeout = null;
currentInterval = null;
currentListenKey = null;
currentUnlistener = null;
}
});
const ping = () => __awaiter(this, void 0, void 0, function* () {
if (!currentListenKey)
return void 0;
try {
yield (0, pingListenKey_1.pingListenKey)({
client: options.client,
host: options.host,
path: options.path,
listenKey: currentListenKey,
data: options.data,
});
}
catch (err) {
// Ключа больше не существует, если снаружи не было вызвано отключение,
// то мы должны переподключиться
if (currentListenKey)
yield recreate();
}
});
const create = (throwError) => __awaiter(this, void 0, void 0, function* () {
try {
currentListenKey = yield (0, createListenKey_1.createListenKey)({
client: options.client,
host: options.host,
path: options.path,
data: options.data,
});
currentUnlistener = (0, apiCall_1.apiCall)({ client: options.client, host: options.host, path: currentListenKey, securityType: 'SOCKET' }, callback);
currentInterval = setInterval(() => ping(), options.client.userDataStreamPingInterval);
}
catch (err) {
if (throwError)
throw err;
yield recreate();
}
});
const recreate = () => __awaiter(this, void 0, void 0, function* () {
currentRecreateTimeout = setTimeout(() => __awaiter(this, void 0, void 0, function* () {
yield close();
createPromise = create(false);
}), options.client.userDataStreamRecreateTimeout);
});
let createPromise = create(true);
return () => {
createPromise.then(() => close());
};
}
exports.userDataCall = userDataCall;
//# sourceMappingURL=userDataCall.js.map