Skip to content

Instantly share code, notes, and snippets.

@tresf
Last active October 1, 2024 07:00
Show Gist options
  • Save tresf/42a8f25124840a6c496b to your computer and use it in GitHub Desktop.
Save tresf/42a8f25124840a6c496b to your computer and use it in GitHub Desktop.
Connect and print
function connectAndPrint() {
// our promise chain
connect().then(function() {
return print();
}).then(function() {
success(); // exceptions get thrown all the way up the stack
}).catch(fail); // so one catch is often enough for all promises
// NOTE: If a function returns a promise, you don't need to wrap it in a fuction call.
// The following is perfectly valid:
//
// connect().then(print).then(success).catch(fail);
//
// Important, in this case success is NOT a promise, so it should stay wrapped in a function() to avoid confusion
}
// connection wrapper
// - allows active and inactive connections to resolve regardless
// - try to connect once before firing the mimetype launcher
// - if connection fails, catch the reject, fire the mimetype launcher
// - after mimetype launcher is fired, try to connect 3 more times
function connect() {
return new Promise(function(resolve, reject) {
if (qz.websocket.isActive()) { // if already active, resolve immediately
resolve();
} else {
// try to connect once before firing the mimetype launcher
qz.websocket.connect().then(resolve, function retry() {
// if a connect was not successful, launch the mimetime, try 3 more times
window.location.assign("qz:launch");
qz.websocket.connect({ retries: 2, delay: 1 }).then(resolve, reject);
});
}
});
}
// print logic
function print() {
var printer = "XPS Document Writer";
var options = { size: { width: 8.5, height: 11}, units: "in", density: "600" };
var config = qz.configs.create(printer, options);
var data = [{ type: 'pdf', data: 'assets/pdf_sample.pdf' }];
// return the promise so we can chain more .then().then().catch(), etc.
return qz.print(config, data);
}
// notify successful print
function success() {
alert("Success");
}
// exception catch-all
function fail(e) {
alert("Error: " + e);
}
@proseLA
Copy link

proseLA commented Aug 8, 2018

this code gets stuck into an infinite loop, until it crashes the page. look at this part of the code:

            qz.websocket.connect().then(resolve, function reject() {
                // if a connect was not succesful, launch the mimetime, try 3 more times
                window.location.assign("qz:launch");
                qz.websocket.connect({ retries: 2, delay: 1 }).then(resolve, reject);
            });

after the 2 retries, your code will then try to resolve and reject, which is what it just did. and now you are in an infinite loop. problem. if you want to do what you comment says, i think the code should be:

            qz.websocket.connect().then(resolve, function reject() {
                // if a connect was not succesful, launch the mimetime, try 3 more times
                window.location.assign("qz:launch");
                qz.websocket.connect({ retries: 2, delay: 1 }).then(resolve, function () {
                     fail("Unable to connect to qz-tray");
                });
            });

i'm hardly the javascript expert, but you can test it for your self. try connecting using a certificate and reject that certificate and click remember that action. if you try connecting again using a stored certificate that is the reject list, you hang and then crash the page.

@tresf
Copy link
Author

tresf commented Nov 6, 2018

@proseLA good catch. I think the problem was that the example code redefined what reject was, causing it to fallback to its own internal function (ouch). I just renamed it to retry(). Can you please test this and see if it helps?

-           qz.websocket.connect().then(resolve, function reject() {
+           qz.websocket.connect().then(resolve, function retry() {
                // if a connect was not succesful, launch the mimetime, try 3 more times
                window.location.assign("qz:launch");
                qz.websocket.connect({ retries: 2, delay: 1 }).then(resolve, reject);
            });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment