DEV Community

Cover image for Stop, Kill and loop protection
JSLab
JSLab

Posted on

Stop, Kill and loop protection

JSLab feature notes: one docs page, explained. Today: Stop, Kill and loop protection, from The editor and live output.

JSLab, a JavaScript & TypeScript scratchpad for macOS, runs your code as you type it. Code you're typing is code that's half-written, and half-written code doesn't always end. This note covers what happens then.

A run can stay active on purpose

A run doesn't end when the last line runs. It stays active while it has open timers, servers, sockets or child processes, and the status bar says so:

let ticks = 0;
const timer = setInterval(() => {
  ticks++;
  console.log(`tick ${ticks}`);
  if (ticks === 3) clearInterval(timer);
}, 500);
"started";
Enter fullscreen mode Exit fullscreen mode

This one ends itself after three ticks. One that doesn't is what Stop is for.

Stop and Kill

  • ⇧⌘R (Stop) stops a run gracefully and closes its timers, servers and the rest. If the run doesn't respond, JSLab kills it after 500 ms.
  • ⌥⌘R (Kill) kills it immediately.

Each run is its own process (or a freshly reloaded page in a browser tab), so killing it can't hurt the editor. process.exit() flushes its output before the run ends.

When a tab stops answering

If your code stops responding altogether, JSLab shows This tab isn't responding, with Kill and Wait. By default it appears after 3 seconds. Change that with Unresponsive Timeout in Settings → Advanced, anywhere from 1,000 to 60,000 ms.

Loop protection

Most hangs in a scratchpad are a loop that never ends, so JSLab catches those before you have to:

// i never reaches 10
let i = 0;
while (i < 10) {
  i = i * 2;
}
Enter fullscreen mode Exit fullscreen mode

for, while, do…while, for…in and for…of loops are stopped after too many iterations, with an error that names the loop's line.

  • Loop Limit (Settings → Advanced): 2,000 iterations by default, from 100 to 10,000,000.
  • Loop Protection in the same place turns it off, as does Toggle Loop Protection in the command palette (⇧⌘P).

Curious how it works? Actions → Show Transpiled Output shows the JavaScript your tab actually runs. Switch Hide instrumentation off and you can see the loop-protection code JSLab adds around your loops.

Safe Mode

If a tab hangs every time JSLab opens, hold Shift while you launch it. JSLab starts in Safe Mode, with Auto Run paused, so you can fix the code before it runs. JSLab also reopens in Safe Mode by itself if it didn't shut down cleanly while running code.

JSLab launches Nov 15. The full page: https://js-lab.app/docs/editor

Top comments (0)