|
|
@ -48,21 +48,48 @@ class Client { |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
requestObjectBatch(method, params, secondParam) { |
|
|
|
return new Promise((resolve, reject) => { |
|
|
|
let arguments_far_calls = {}; |
|
|
|
let contents = []; |
|
|
|
const { key, data } = params; |
|
|
|
for (let item of data) { |
|
|
|
const id = ++this.id; |
|
|
|
let param = ""; |
|
|
|
if (key in item) param = item[key]; |
|
|
|
if (secondParam !== undefined) { |
|
|
|
contents.push(util.makeRequest(method, [param, secondParam], id)); |
|
|
|
} else { |
|
|
|
contents.push(util.makeRequest(method, [param], id)); |
|
|
|
} |
|
|
|
arguments_far_calls[id] = { param, data: item }; |
|
|
|
} |
|
|
|
const content = '[' + contents.join(',') + ']'; |
|
|
|
this.callback_message_queue[this.id] = util.createPromiseResultBatch(resolve, reject, arguments_far_calls); |
|
|
|
// callback will exist only for max id
|
|
|
|
this.conn.write(content + '\n'); |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
requestBatch(method, params, secondParam) { |
|
|
|
if (!this.status) { |
|
|
|
return Promise.reject(new Error('Connection to server lost, please retry')); |
|
|
|
} |
|
|
|
if (typeof params === "object" && "key" in params && "data" in params) { |
|
|
|
return this.requestObjectBatch(method, params, secondParam) |
|
|
|
} |
|
|
|
return new Promise((resolve, reject) => { |
|
|
|
let arguments_far_calls = {}; |
|
|
|
let contents = []; |
|
|
|
for (let param of params) { |
|
|
|
const id = ++this.id; |
|
|
|
let data = {}; |
|
|
|
if (secondParam !== undefined) { |
|
|
|
contents.push(util.makeRequest(method, [param, secondParam], id)); |
|
|
|
} else { |
|
|
|
contents.push(util.makeRequest(method, [param], id)); |
|
|
|
} |
|
|
|
arguments_far_calls[id] = param; |
|
|
|
arguments_far_calls[id] = { param, data }; |
|
|
|
} |
|
|
|
const content = '[' + contents.join(',') + ']'; |
|
|
|
this.callback_message_queue[this.id] = util.createPromiseResultBatch(resolve, reject, arguments_far_calls); |
|
|
|