HI Rich,
I finally found out why I didn’t use the variable’s content to produce the right code, so now I can do but unfortunately this introduces a new problem in code generation.
Let’s take example 1 where just use a String like before:
and it correctly produces what we expect - please take a particular note on the initialization part line13 to 15 in the beginning of the code as we will compare that in a moment.
well done … now let’s do the variable version:
which generates fine code - see lines 18 onwards: it assigns the string to the variable and then uses the variable as an indirection for accessing the timers. Very well …
but wait! Have a lock on the initialization part line13 to 15
It also uses the variable indirection for rightfully initializing the timer to null if undefined but this is always done in the beginning of the source by a blockly technique that makes sure that the same code that is required by many blocks is produced only once and then put to the beginning.
Of course, this must fail, because the variable neither exists at that point nor is it set correctly.
So why do we need this code in the beginning? It makes the whole code later on much easier. Instead we could put all the check into the code everywhere we we access the timer-variable.
So the only way I could think of is that we remove this part but then we have to make sure that everywhere where we use the timer[…]-field we first have to check if is not only not undefined or nor null.
So for the following that could fail if timerName wasn’t existing
// timerName = 'Stefan';
if (this.timers[timerName] && this.timers['timerName].isActive()) {
}
we had to generate this boilerplate code even for the simplest part of the code
if (timerName!== undefined && !timerName && this.timers[timerName] && this.timers[timerName].isActive()) {
}
Now imagine you had this code
if (this.timers[timerName] === null) {
this.timers[timerName] = scriptExecution.createTimer(zonedDateTime.now().plusSeconds(10), function(){
this.timers[timerName] = null
})
}else {
this.timers[timerName].reschedule(zonedDateTime.now().plusSeconds(10))
}
which would result into
if (timerName!==undefined && !timerName && this.timers[timerName] === null) {
this.timers[timerName] = scriptExecution.createTimer(zonedDateTime.now().plusSeconds(10), function(){
this.timers[timerName] = null
})
}else if (timerName!== undefined && !timerName) {
this.timers[timerName].reschedule(zonedDateTime.now().plusSeconds(10))
}
… and even after thinking quite some time I am not sure if this is completely correct … and now if we had this?
You could for sure argue that “who cares about the generated code” as it is in the background where noone sees it. Well, at least I do because have to be sure that it works and that one could at least comprehend what is going on…
If even tried to encapsulate the check into a function but couldn’t quite find a good way of doing it.
What I am trying to say is that this doable but I have a bad feeling delivering the generated code due to the many permutations we could run into that would not work as expected. I am honestly torn back and forth about doing it out not. I am kind of stuck here…