Skip to content

Commit

Permalink
Split config/data prints should be sub-arrays of data array
Browse files Browse the repository at this point in the history
  • Loading branch information
Berenz committed Jul 7, 2017
1 parent 60d33ab commit 530463c
Showing 1 changed file with 16 additions and 26 deletions.
42 changes: 16 additions & 26 deletions js/qz-tray.js
Original file line number Diff line number Diff line change
Expand Up @@ -882,7 +882,7 @@ var qz = (function() {
* ex. <code>'{"call":"<callName>","params":{...},"timestamp":1450000000}'</code>
*
* @param {Object<Config>|Array<Object<Config>>} configs Previously created config object or objects.
* @param {Array<Object|string>} data Array of data being sent to the printer.<br/>
* @param {Array<Object|string>|Array<Array<Object|string>>} data Array of data being sent to the printer.<br/>
* String values are interpreted as <code>{type: 'raw', format: 'command', flavor: 'plain', data: &lt;string>}</code>.
* @param {string} data.data
* @param {string} data.type Printing type. Valid types are <code>[pixel | raw*]</code>. *Default
Expand Down Expand Up @@ -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 = {
Expand All @@ -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
Expand Down

0 comments on commit 530463c

Please sign in to comment.