scratchfoundation / scratchx

Scratch Extension Site
https://github.com/llk/scratchx
190 stars 120 forks source link

Nested asynchronous reporter blocks #109

Closed khanning closed 1 year ago

khanning commented 7 years ago

When an asynchronous reporter block "R" is placed inside another asynchronous block the corresponding JavaScript function is entered twice and the second return value is stored and returned during any subsequent calls to the block. This only happens the first time the block is executed after the extension is loaded, but because of the stored return value it disrupts all future calls to the block.

The following example demonstrates the behavior:

example_extension.js

image

ext.wait = function(secs, callback) {
  var num = count++;
  console.log("run count "+num+" entered");
  setTimeout(function() {
  console.log("run count "+num+" returned");
    callback(num);
  }, secs * 1000);
};
  1. Extension is loaded and green flag is clicked
  2. ext.wait function is entered twice in succession
  3. The next time the green flag is clicked, the return value from the second ext.wait call is returned without waiting

This isn't a common scenario and I can't think of a situation where this could happen with the core Scratch blocks, but it is possible to run into with reporter blocks that wait in ScratchX.