UNPKG

@kevin_men/websocket

Version:

TDengine Connector for nodejs and browser using WebSocket.

77 lines (76 loc) 2.6 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.WSRows = void 0; const taosResult_1 = require("../common/taosResult"); const wsError_1 = require("../common/wsError"); const log_1 = __importDefault(require("../common/log")); class WSRows { constructor(wsInterface, resp) { this._wsClient = wsInterface; this._wsQueryResponse = resp; this._taosResult = new taosResult_1.TaosResult(resp); this._isClose = false; } async next() { if (this._wsQueryResponse.is_update || this._isClose) { log_1.default.debug("WSRows::Next::End=>", this._taosResult, this._isClose); return false; } let data = this._taosResult.getData(); if (this._taosResult && data != null) { if (data && Array.isArray(this._taosResult.getData()) && data.length > 0) { return true; } } this._taosResult = await this.getBlockData(); if (this._taosResult.getData()) { return true; } return false; } async getBlockData() { try { let wsFetchResponse = await this._wsClient.fetch(this._wsQueryResponse); log_1.default.debug("[wsQuery.execute.wsFetchResponse]==>\n", wsFetchResponse); if (wsFetchResponse.completed) { this.close(); this._taosResult.setData(null); } else { this._taosResult.setRowsAndTime(wsFetchResponse.rows, wsFetchResponse.timing); return await this._wsClient.fetchBlock(wsFetchResponse, this._taosResult); } return this._taosResult; } catch (err) { this.close(); throw new wsError_1.TaosResultError(err.code, err.message); } } getMeta() { return this._taosResult.getMeta(); } getData() { if (this._wsQueryResponse.is_update) { return undefined; } let data = this._taosResult.getData(); if (this._taosResult && data != null) { if (Array.isArray(data) && data.length > 0) { return data.pop(); } } return undefined; } async close() { if (this._isClose) { return; } this._isClose = true; await this._wsClient.freeResult(this._wsQueryResponse); } } exports.WSRows = WSRows;