From 530463ce46aa375dda1f62dd81d9c668e34a673b Mon Sep 17 00:00:00 2001 From: Berenz Date: Fri, 7 Jul 2017 16:14:37 -0400 Subject: [PATCH] Split config/data prints should be sub-arrays of data array --- js/qz-tray.js | 42 ++++++++++++++++-------------------------- 1 file changed, 16 insertions(+), 26 deletions(-) diff --git a/js/qz-tray.js b/js/qz-tray.js index f8edf48a9..f473df4c6 100644 --- a/js/qz-tray.js +++ b/js/qz-tray.js @@ -882,7 +882,7 @@ var qz = (function() { * ex. '{"call":"","params":{...},"timestamp":1450000000}' * * @param {Object|Array>} configs Previously created config object or objects. - * @param {Array} data Array of data being sent to the printer.
+ * @param {Array|Array>} data Array of data being sent to the printer.
* String values are interpreted as {type: 'raw', format: 'command', flavor: 'plain', data: <string>}. * @param {string} data.data * @param {string} data.type Printing type. Valid types are [pixel | raw*]. *Default @@ -939,10 +939,13 @@ var qz = (function() { if (signaturesTimestamps && !Array.isArray(signaturesTimestamps)) { signaturesTimestamps = [signaturesTimestamps]; } } - if (!Array.isArray(configs)) { configs = [configs]; } + if (!Array.isArray(configs)) { configs = [configs]; } //single config -> array of configs + if (!Array.isArray(data[0])) { data = [data]; } //single data array -> array of data arrays //clean up data formatting - _qz.tools.relative(data); + for(var d = 0; d < data.length; d++) { + _qz.tools.relative(data[d]); + } var sendToPrint = function(mapping) { var params = { @@ -954,32 +957,19 @@ var qz = (function() { return _qz.websocket.dataPromise('print', params, mapping.signature, mapping.timestamp); }; - //presort to group config data - var map = {}; - var signIdx = 0; + //chain instead of Promise.all, so resumeOnError can collect each error + var chain = []; for(var i = 0; i < configs.length || i < data.length; i++) { - var cfg = configs[Math.min(i, configs.length - 1)]; - var ref = cfg.printer ? cfg.printer.name : null; - - if (!map[ref]) { - map[ref] = { - config: cfg, - data: [], - signature: signatures[signIdx], - timestamp: signaturesTimestamps[signIdx] + (function(i_) { + var map = { + config: configs[Math.min(i_, configs.length - 1)], + data: data[Math.min(i_, data.length - 1)], + signature: signatures[i_], + timestamp: signaturesTimestamps[i_] }; - signIdx++; - } - map[ref].data.push(data[Math.min(i, data.length - 1)]); - } - - //chain instead of Promise.all, so resumeOnError can collect each error - var chain = []; - for(var set in map) { - (function(set_) { - chain.push(function() { return sendToPrint(map[set_]); }); - })(set); + chain.push(function() { return sendToPrint(map) }); + })(i); } //setup to catch errors if needed