Skip to content

Instantly share code, notes, and snippets.

@PeteGCole
Created September 28, 2020 14:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PeteGCole/dab9a4f1f709251e125c44100b369920 to your computer and use it in GitHub Desktop.
Save PeteGCole/dab9a4f1f709251e125c44100b369920 to your computer and use it in GitHub Desktop.
WaitForSpoolingComplete scenarios
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h1>WaitForSpoolingComplete Scenarious</h1>
<div>
<button data-func="simpleScenario" id="btn_1">Simple scenario</button>&nbsp;
<button data-func="conditional" id="btn_2">Conditional printing</button>&nbsp;
<button data-func="loopReports1" id="btn_3">Looping printing</button>&nbsp;
<button data-func="loopReports2" id="btn_4">Looping printing with tail code</button>&nbsp;
</div>
<!-- Promise library for IE 11 -->
<script type="text/javascript">
// doNothingForAWhile - as it says on the tin.
//
// This is a mock of MeadCo.ScriptX.WaitForSpoolingComplete()
function doNothingForAWhile(msecs) {
return new Promise(function (resolve) {
console.log("Will hang around/do somme printing for ", msecs);
setTimeout(resolve, msecs);
});
}
function printReport(printSettings) {
console.log("Print: " + printSettings.header);
return doNothingForAWhile(500);
}
function simpleScenario() {
printReport({
header: "simple scenario report"
}).finally(function () {
console.log("Printing done, could close window")
});
}
function postPrint1() {
console.log("post printing function 1");
}
function postPrint2() {
console.log("post printing function 2");
}
function postPrint3() {
console.log("post printing function 3");
}
function conditional() {
var ps = {
header: "conditional scenario report"
};
var condition1 = true, condition2 = true;
if (condition1) {
printReport(ps).finally(function () {
// post printing action
postPrint1();
postPrint2();
if (condition2) {
ps.header = "new header";
printReport(ps).finally(function () {
postPrint3();
});
}
});
}
}
function shouldPrintReport(n) {
return n % 2 == 1;
}
function loopReports1(iStart) {
iStart = (typeof iStart !== 'undefined') ? iStart : 0;
for (i = iStart; i < 10; i++) {
if (shouldPrintReport(i)) {
var ps = { header: "Report: " + i };
printReport(ps).finally(function () {
postPrint1();
postPrint2();
loopReports1(i + 1);
});
return;
}
else {
console.log("Report skipped ", i);
}
}
// no code relying on synchronous behaviour here makes it easier.
}
function showBusyUI() {
console.log("busy printing starts ....");
}
function hideBusyUI() {
console.log("printing ends, ui to normal");
}
function printLoopReports2(iStart, fnDoneAll) {
iStart = (typeof iStart !== 'undefined') ? iStart : 0;
if (iStart === 0) {
showBusyUI();
}
for (i = iStart; i < 10; i++) {
if (shouldPrintReport(i)) {
var ps = { header: "Report: " + i };
printReport(ps).finally(function () {
postPrint1();
postPrint2();
printLoopReports2(i + 1, fnDoneAll);
});
return;
}
else {
console.log("Report skipped ", i);
}
}
// code relying on synchronous behaviour here.
// this code only executes if the loop reached completion
hideBusyUI();
fnDoneAll();
}
function loopReports2() {
printLoopReports2(0, function () {
// code relying on synchronous behaviour here.
console.log("Completed all reports");
});
}
window.addEventListener("load", function () {
console.log("Window startup ..");
doNothingForAWhile(1000).then(function () {
console.log("finished doing nothing");
});
var buttons = document.querySelectorAll('button');
buttons.forEach(function (item) {
item.addEventListener("click", function (event) {
console.log("execute: " + this.dataset.func);
eval(this.dataset.func + "();");
console.log("a print scenario is running");
});
});
console.log("Completed startup");
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment