k6 icon indicating copy to clipboard operation
k6 copied to clipboard

Making SetTimeout and co. globally available by default

Open mstoykov opened this issue 1 year ago • 5 comments

Feature Description

Make the newly stabilized k6/timers exports:

  1. SetTimeout
  2. SetInterval
  3. ClearTimeout
  4. ClearInterval

globally available

Suggested Solution (optional)

Keep the module, and just have code that imports and set them on globalThis by default that is run an VU initialization.

Already existing or connected issues / PRs (optional)

Stabilization PR #3587 Original stabilization issue #3297 SetTimeout not being globally available #3329

mstoykov avatar Feb 09 '24 12:02 mstoykov

I feel that having these functions globally accessible is what users will expect. However, I can't say I'm a JavaScript expert, so I am unsure if my feelings are relevant.

olegbespalov avatar Feb 09 '24 16:02 olegbespalov

Similar to @olegbespalov, not a JS expert neither, but I guess that's what experienced JS users would expect, while for the rest it doesn't matter. So 👍🏻

joanlopez avatar Feb 12 '24 05:02 joanlopez

For clarification and my understanding, what does the target user-experience look like?

My assumption reading this is:

// No need to import the `k6/timers` module

export default function () {
    setTimeout(...)  // timer functions are available in the runtime "natively", like in v8 browser runtimes
    setInterval(...)
    clearTimeout(...)
    clearInterval(...)
 }

If so, I think that makes a lot of sense, and although I'm not completely set on that, it is also something we might want to consider also for "Web APIs" we integrate in the future (webcrypto, streams, fetch (if we do), etc...) to provide a user-experience close to what is available in other runtimes.

🙇🏻

oleiade avatar Feb 12 '24 10:02 oleiade

Yes, that will be a thing that users will be able to do - without needing to import anything

mstoykov avatar Feb 12 '24 13:02 mstoykov

there is no event loop in Goja (TMK). So I am not sure who/what would call the global setInterval. Using Goja for this lib seems like a terrible mistake and frankly I am gonna run as far as I can in another direction (no offense but hopefully this helps other people..)

amills-vibeirl avatar Feb 28 '24 00:02 amills-vibeirl

@amills-vibeirl, while goja doesn't have an event loop in itself, k6 adds one. As will most projects using any js engine that want to have control over its event loop.

mstoykov avatar Apr 26 '24 08:04 mstoykov