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

add a $.delay() function #720

Open
pmcelhaney opened this issue Jan 17, 2024 · 2 comments
Open

add a $.delay() function #720

pmcelhaney opened this issue Jan 17, 2024 · 2 comments
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@pmcelhaney
Copy link
Owner

It would be nice to have a function that simulates lag.

export const GET: HTTPGet = async ($) => {
   // wait 2 seconds;
   await $.delay(2000);
   return $.response[200].json("ok"); 
}

It's a matter of adding a function to the return value of the the endpoint function in src/server/resgistry.ts

         query: castParameters(requestData.query, parameterTypes.query),
+
+        delay: async (milliseconds: number) =>
+          // eslint-disable-next-line promise/avoid-new
+          await new Promise((resolve) => {
+            setTimeout(resolve, milliseconds);
+          }),
       });

For type safety, the new property would need to be added to the RequestData type of the same file.

Extra credit

  1. Add a unit test.
  2. Add an optional second argument, so that delay(1000, 4000) will create a random delay between 1 and 4 seconds.
  3. Make it work even if we forget to put await before $.delay(...).
  4. Add an optional third argument, which is a probability distribution function.
@pmcelhaney pmcelhaney added enhancement New feature or request good first issue Good for newcomers labels Jan 17, 2024
@pmcelhaney
Copy link
Owner Author

Should we also add a --delay argument to the CLI that would apply to all endpoints?

In my experience it tends to be a couple of specific endpoints that are notably slow. But I'm sure someone's going to look for a command line argument. Give the people what they want?

@dethell
Copy link
Collaborator

dethell commented Feb 22, 2024

I'd say adding it to $ is sufficient rather than delaying the entire thing. If we want to test an overall delay in the browser we can use the DevTools in Chrome (or whatever) to throttle everything.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants