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

.resume after a .pause results in several events triggering at once #416

Open
rachitnigam opened this issue Jan 7, 2019 · 1 comment
Open

Comments

@rachitnigam
Copy link
Collaborator

I made a page using test-data/integration/step-with-events as a template to demo event pausing with setInterval. Hitting pause in the demo page works as expected (setInterval calls are paused). Hitting resume causes the all the events that were paused to suddenly show up. The GIF illustrates this better:

pause-resume

I would have expected the events to show up one at a time after I hit resume? Is this the same bug found by @joydeep-b in #414?

Code for the demo page:

<!DOCTYPE html>
<html>

<body>
  <p>This page runs Stopify in the browser.</p>
  <textarea id="data">Output starts here...</textarea>
<script src="../../dist/stopify-full.bundle.js"></script>
<script>
var program = `
  onInterval(function() {
    console.info('callback called')
    while (false) {}
    if (paused) {
      throw 'should have been paused';
    }
    console.log(i++);
  }, 1000)
`;

j = 0

function onInterval(cb, time) {
  console.info(`onInterval called ${++j} times`)
  function handler() {
    runner.processEvent(cb,
      (result) => {
        if (result.type === 'exception') {
          window.clearInterval(intervalID)
          throw result.value
        }
      }, console.info);
  }
  var intervalID = window.setInterval(handler, time)
}


var runner = stopify.stopifyLocally(program, { }, {
  estimator: 'countdown',
  yieldInterval: 1
});

runner.g.i = 0;
runner.g.onInterval = onInterval;
runner.g.window = window;
runner.g.paused = true;

const data = document.getElementById('data');
runner.g.console = {
  log(str) {
    data.value = (data.value + str + "\n")
    const evt = new Event('change');
    data.dispatchEvent(evt)
  },
  info(str) {
    return console.info(str)
  }
}

function start() {
  runner.g.paused = false;
  return runner.run((result) => {
    if (result.type !== 'normal') {
      window.document.title = 'error';
    }
  });
}

function shouldResume () {
  if (!runner.g.paused) {
    return runner.resume();
  }
  else {
    return setTimeout(shouldResume, 100);
  }
}

function pause() {
  runner.g.paused = true;
  return runner.pause(shouldResume);
}

function resume() {
  runner.g.paused = false;
}

window.onerror = function() {
  window.document.title = "error";
}
</script>
  <button onclick="start()">Start</button>
  <button onclick="pause()">Pause</button>
  <button onclick="resume()">Resume</button>

</body>
</html>
@rachitnigam
Copy link
Collaborator Author

rachitnigam commented Jan 9, 2019

This is happening because stopify queues events without timestamps. Should stopify be enforcing the property that if time(e2) - time(e1) = n, then after a pause e2 triggers nms after e1? Presumably, it's important that e1 and e2 are associated to the same event handler.

Also, for reference, chrome debugger can completely stop and restart events in this way (which is not surprising).

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

No branches or pull requests

1 participant