OK, apart from complying to what keeps this kind of software development alive (code sharing) I assume some of you may be curious to what happened in the development of CumulusUtils.
First I designed the user interface to have the swapping dashboards with the gauges. That created problems pretty soon, where I could not get the windRose to show after the switch. Mark helped me out and indicated to throw away the rose and build it a new one by calling the initialisation of the gauges. That appeared to be Pandora's box in the sense that some more items needed to be published globally so we had to change things in gauges.js. Later the initialisation was done, not by calling init(), but by calling $.getScript().
The first changes were small so they were made in the general code. Then, while testing, it became more complex. Because BCJkiwi is currently launching his new template the testers @PaulMy and @laulau used multiple instances of websites with the same origin so it was needed that CumulusUtils had the possibility to point to a RealTime location other than its own root. Those changes could be made, but it meant all kinds of errors came up including the failing of the Ajax calls.
The essence of the problem was that with all timers outstanding, neither CumulusUtils nor gauges.js with its Ajax calls were re-entrant. I could not call the re-initialisation from gauges itself, nor from CumulusUtils. So I had to fix that.
I did the following.
In gauges.js:
- I removed all code not required for CumulusUtils (basically the popups). Non-essential but it cleared the view on the code.
- I created a function Pause() which stops the timers and cancels an outstanding Ajax request.
- I published config, gaugeGlobals, init and Pause.
In cumulusutils.js:
- I gave the variables gauges.config.realTimeUrl and gauges.config.realtimeInterval its required value in the Document Ready function.
- Also started the reatime.txt reading on the CumulusUtils level (which has its own timer)
- In the actual function which does the switch ( ToggleDashboard() )
- I cancelled all timers and outstanding Ajax requests.
- Did the switch
- Removed the windRose
- Reinitialised gauges.js by calling $.getScript and doing so in a synced way (to prevent the non-re-entrance problems)
- Emulate the Document Ready function to set the variables under the first point
In coding terms the solution is as follows:
Code: Select all
Pause = function () {
PauseInEffect = true; // To prevent the Ajax call from happening during restart when that is not completely finished
clearTimeout(downloadTimer);
clearInterval(tickTockInterval);
if ($.active > 0) { jqXHR.abort(); jqXHR = null; }
},
Code: Select all
function ToggleDashboard() {
gauges.Pause(true);
clearTimeout(RT_timer);
[... do the actual switch ...]
$('#rosePlot').parent().remove();
$.when( $.getScript('lib/gauges.js'),
$.Deferred(function(deferred){ $(deferred.resolve); })
).done(function(){
gauges.config.realTimeUrl = '{Sup.GetUtilsIniValue("Website", "CumulusRealTimeLocation", "")}realtimegauges.txt';
gauges.config.realtimeInterval = {Sup.GetUtilsIniValue("Website", "CumulusRealTimeInterval", "15")};
RT_timer = setInterval(loadRealtimeTxt, {Sup.GetUtilsIniValue("Website", "CumulusRealTimeInterval", "15")} * 1000);
});
}
That's it
Thanks to @laulau for helping debugging
.