ggicci/goplay

Respect event delay in output

Closed this issue · 1 comments

Delay property is used to simulate timeouts in console output. It's necessary because the Go playground mocks system time and disables timeout functions like time.Sleep and etc.

For instance, let's take the snippet below:

fmt.Println("fizz")
time.Sleep(2 * time.Second)
fmt.Println("buzz")

If you don't simulate timeout between events, both lines will be printed simultaneously.

To fix this issue, it's necessary to add a simple setTimeout between outputs:

const sleep = n => new Promise((res) => setTimeout(res, n);

 for (const event of js.Events || []) {
      // not sure it this is delay in seconds or nanoseconds
      // but let's assume that .Delay is in seconds for demo purposes.
     if (event.Delay > 0) await sleep(event.Delay);
      switch (event.Kind) {
        case 'stdout':
          stdout.innerText += event.Message
          break
        case 'stderr':
          stderr.innerText += event.Message
          break
        case 'system':
          system.innerText += event.Message
          break
      }
    }

Thanks @x1unix :) I was wondering how to deal with event.Delay last night. Now it looks cleary. I'll work on it then :)