cqi-js: implement timeout

This commit is contained in:
Patrick Jentsch 2023-06-30 14:13:34 +02:00
parent c52c966863
commit e21ef2422d
2 changed files with 10 additions and 8 deletions

View File

@ -2,12 +2,12 @@ cqi.api.APIClient = class APIClient {
/**
* @param {string} host
* @param {string} corpusId
* @param {number} [timeout=Infinity] timeout
* @param {number} [timeout=60] timeout
* @param {string} [version=0.1] version
*/
constructor(host, corpus_id, timeout = Infinity, version = '0.1') {
constructor(host, corpus_id, timeout = 60, version = '0.1') {
this.host = host;
this.timeout = timeout;
this.timeout = timeout * 1000; // convert seconds to milliseconds
this.version = version;
this.socket = io(
this.host,
@ -26,8 +26,10 @@ cqi.api.APIClient = class APIClient {
*/
#request(fn_name, fn_args = {}) {
return new Promise((resolve, reject) => {
// TODO: implement this.socket.timeout(this.timeout).emit(...)
this.socket.emit('cqi', {fn_name: fn_name, fn_args: fn_args}, (response) => {
this.socket.timeout(this.timeout).emit('cqi', {fn_name: fn_name, fn_args: fn_args}, (timeoutError, response) => {
if (timeoutError) {
reject(timeoutError);
}
if (response.code === 200) {
resolve(response.payload);
}

View File

@ -2,12 +2,12 @@ cqi.CQiClient = class CQiClient {
/**
* @param {string} host
* @param {string} corpusId
* @param {number} [timeout=Infinity] timeout
* @param {number} [timeout=60] timeout
* @param {string} [version=0.1] version
*/
constructor(host, corpusId, timeout = Infinity, version = '0.1') {
constructor(host, corpusId, timeout = 60, version = '0.1') {
/** @type {cqi.api.APIClient} */
this.api = new cqi.api.APIClient(host, corpusId, version);
this.api = new cqi.api.APIClient(host, corpusId, timeout, version);
}
/**