WaitForSpoolingComplete()
This walk-through illustrates taking a classic document that has a controlled print experience and works only in Internet Explorer and making it work with ScriptX.Services in any browser on any platform using the minimum amount of development effort and making as few changes as possible (though more changes might be desirable).
Previously : Stage 6 - Preparing for advanced uses
Background
Each of the functions Print, PrintHTML and BatchPrintPDF will return immediately after the print request has been started and/or queued with the print continuing either in the background with ScriptX.Add or with ScriptX.Services at the server.
The browser remains responsive and so the user is able close the tab/window while the print is in progress.
ScriptX.Addon
With ScriptX.Addon, if the window/tab is closed or navigated away from then any print in progress would immediately stop.
So, if a navigation occurs or window/tab is closed while printing with ScriptX.Addon, ScriptX.Addon will pause the action for 5 seconds to allow the print to complete.
If the print is still not complete then the user will be asked if they wish to abandon the print (the window/tab will close) or wait for it to complete before continuing the action. The prompts that appear are clear and concise and can be customised.
ScriptX.Services
No such solution is available to javascript.
The browser beforeunload event can be used to some effect but suffers from the fact that the dialog message is misleading as only a generic message will ever be displayed and there is no way to offer users more sophisticated options other than "stay or leave".
This means there is no satisfactory way to implement the behaviour of ScriptX.Add-on with ScriptX.Services.
There is an extent to which this does not matter as printing is occuring in a separate service and will continue after the navigation or closure of a tab/window.
However, there are occasions where finer control and feedback to the user is worthwhile. For example:
-
A number of jobs have been submitted to the
printHtml()
API. If the window/tab is closed many prompts may be given to the user to request permission to close the window. -
Jobs submitted to
printHtml()
should be verified that they have succeeded and re-submit/report those that fail for some reason. - Visually show printing is in progress and the user should wait.
- Cleanly avoiding an error display if a print is started with ScriptX.Servicees and the window is closed immediately after.
Each of these use cases is handled by the WaitForSpoolingComplete
API.
The WaitForSpoolingComplete() API Is emulated with the ScriptX.Services client library but requires an additional argument. To ease the transition the MeadCoScriptXJS library implements a function that returns a promise and can be used with either ScriptX.Addon or ScriptX.Services.
This means this code:
factory.printing.Print(false);
factory.printing.WaitForSpoolingComplete();
self.close();
becomes:
factory.printing.Print(false);
MeadCo.ScriptX.WaitForSpoolingComplete().finally(function() {
self.close();
})
Or, in a modern browser where await can be used with a Promise then it is a very simple change:
factory.printing.Print(false);
await MeadCo.ScriptX.WaitForSpoolingComplete();
self.close();
For a more complete example:
$("#btn-print").click(function () {
MeadCo.ScriptX.PrintPage2(true).then(function (bStarted) {
if (bStarted) {
var $dlg = $("#dlg-busy");
$dlg.modal("show");
MeadCo.ScriptX.WaitForSpoolingComplete().finally(function () {
$dlg.modal("hide");
});
}
});
});
Or, for a more complete example where code will only run in a modern browser where await can be used with a Promise:
$("#btn-print").click(function () {
if ( await MeadCo.ScriptX.PrintPage2(true) ) {
var $dlg = $("#dlg-busy");
$dlg.modal("show");
await MeadCo.ScriptX.WaitForSpoolingComplete();
$dlg.modal("hide");
}
});
Open your browser (any browser) and go to the address https://scriptxprintsamples.meadroid.com/ThenToNow/now-stage7
A more extensive discussion of WaitForSpoolingComplete() is available at ScriptX Samples