npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

cypress-style-async

v2.0.0

Published

This library helps you create a chainable, queue-backed async API similar to the `cy` global used in [Cypress](cypress.io).

Downloads

12

Readme

Cypress-Style Async

This library helps you create a chainable, queue-backed async API similar to the cy global used in Cypress.

The way it works, is you register a bunch of commands that can be performed, and then the consumer of your API uses synchronous methods to queue those commands up, and then they are run asynchronously and serially in the background.

Please read This page from Cypress's docs to get a better idea of how this sync-feeling-but-actually-async queue-backed API pattern works.

Usage

const CypressStyleAsync = require("cypress-style-async");

const mySystem = new CypressStyleAsync({
  onError(error) {
    console.error(error);
  }
});

// mySystem.api is an object that behaves like the `cy` object in Cypress.

// registerCommand adds a method to mySystem.api.
mySystem.registerCommand("example", async (command, api) => {
  // command is an object with the shape { name, args }.
  command.name; // "example"
  command.args; // An array of arguments that `mySystem.api.example` was called with

  // api is an object.
  api.context; // An object that persists throughout the lifetime of mySystem. You can add properties to it with api.writeContext, and then read them again later in any command.

  api.writeContext({ something: 4 }); // Adds the property `something` with value `4` to the api.context object.

  api.clearContext(); // clears all the properties in the context object.

  await api.sleep(100); // uses setTimeout to wait 100ms.

  api.retry({ error: new Error("everything is bad"), maxRetries: 10 });
  // api.retry tells the system that an error occurred, and to re-run this command, unless
  // we have already retried this command the number of times specified in `maxRetries`, in which
  // case the error will bubble out to the `onError` function that was passed in when `mySystem` was
  // constructed, and `mySystem` won't execute any other commands in its queue.
});

// Now you can call `example`:
mySystem.api.example("bla", "bla");

// Here's a more concrete/realistic example of a command.
mySystem.registerCommand("fetchBlob", async (command, api) => {
  const url = command.args[0]; // User must pass url as first argument
  const options = command.args[1];

  let response;
  try {
    response = await window.fetch(url, options);
  } catch (error) {
    // If the fetch failed, we retry this command up to 10 times
    api.retry({ error, maxRetries: 10 });
    return;
  }

  const blob = await response.blob();

  api.writeContext({ fetchedBlob: blob });
};

// Once you have all your commands registered, you expose only `mySystem.api` to your users.
module.exports = mySystem.api;

License

MIT