Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

uploading in a single block to preserve setTimeout order #124

Closed
wants to merge 1 commit into from
Closed

uploading in a single block to preserve setTimeout order #124

wants to merge 1 commit into from

Conversation

d3nd3
Copy link
Contributor

@d3nd3 d3nd3 commented Jun 19, 2020

forumpost <-

setTimeout(function() {console.log("after"),0};
console.log("before");

will not honour actual behavior if sequential execution of code, because its uploaded line by line, with idle time inbetween. If you wrap it in a function block then the output behavior is expected. This is very useful eg on a bangle watch emulation where the position of string on screen eg will be different :

var ypos = 5;
setTimeout( function(){
  ypos = ypos + 20;
  g.drawString("timeout", 5, ypos, 0);
},0);
var x =0;
while ( x < 100000 ) {
  x++;
}
ypos = ypos + 20;
g.drawString("final", 5, ypos, 0);

setTimeout(function() {console.log("after"),0};
console.log("before");

will not honour actual behavior if sequential execution of code, because its uploaded line by line, with idle time inbetween.  If you wrap it in a function block then the output behavior is expected.  This is very useful eg on a bangle watch emulation where the position of string on screen eg will be different :

var ypos = 5;
setTimeout( function(){
  ypos = ypos + 20;
  g.drawString("timeout", 5, ypos, 0);
},0);
var x =0;
while ( x < 100000 ) {
  x++;
}
ypos = ypos + 20;
g.drawString("final", 5, ypos, 0);
@MaBecker
Copy link

Uploading code is directly execute, so better do something like this:

function foo(){ 
  setTimeout( function(){ console.log("after");},0);
  console.log("before");
}

setTimeout(foo,1E3);

@d3nd3
Copy link
Contributor Author

d3nd3 commented Jun 19, 2020

Ye, figured that out. I wrote some code to wrap the right hand side of ide in a function block, like you've just written.
Not sure if you want that to be a permanent feature of the ide or not. I would recommend its much kinder to be people who are working within the ide because they have less real-world testing (uploading to device) to perform. And its laboursome to always wrap your code in function block manually.

But its up to you guys. You'll know best. xx

@fanoush
Copy link
Contributor

fanoush commented Jun 19, 2020

I fail to see how this is "very useful" and why to always modify code that someone uploads just because of 'setTimeout with zero parameter' case in combination with 'user thinks it is "laboursome to always wrap your code in function block manually"' case.

And BTW wrapping into function block like this would break variables, uploading

var x=5;

is not same as

(function(){

var x=5;

})();

second case have x undefined after upload

@gfwilliams
Copy link
Member

Thanks for the suggestion - but in this case it's not something I'd want to pull in.

Answered on the forum post as well, but for others that find this:

Espruino is designed for devices with limited RAM. As such, by allowing it to execute code as it is uploaded it is able to fit in twice as much code (it's not having to store the text version of what it is running, as well as the 'interpreted' version).

This change effectively not only negates the efficiency but makes it 50% worse: you now have to store the text version, interpreted giant function, as well as the result of executing that function.

http://www.espruino.com/Saving has some more info about ways of saving as well.

@gfwilliams gfwilliams closed this Jun 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants