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

collmise

v0.0.5

Published

Collmise provides two main functions - `unitCollmise` and `collmise`;

Downloads

17

Readme

Collmise - Collect, combine, and cache promises

Collmise provides two main functions - unitCollmise and collmise;

unitCollmise

Let's have a function for fetching all the books from the server.

const getAllBooks = () => {
  return fetch("/api/v1.0/books").then(response => response.json())
}

In the application, we might need to call getAllBooks different times from different places. The request/response flow might look like this:

We can optimize this flow by combining concurrent requests. We can achieve this easily using unitCollmise

import { unitCollmise } from "collmise";

const allBooksCollmise = unitCollmise();

const getAllBooks = () => {
  return allBooksCollmise.request(() => {
    return fetch("/api/v1.0/books").then(response => response.json())
  });
}

In this case, the flow will look like this:

Now let's cache our response for 1 minute. For that we just need to pass options to unitCollmise;

const allBooksCollmise = unitCollmise({
  responseCacheTimeoutMS: 60 * 1000
});

Sometimes you might want to disable cache and load all data. Let's add an optional parameter for that:

const getAllBooks = (loadFreshData = false) => {
  return allBooksCollmise.fresh(loadFreshData).request(() => {
    return fetch("/api/v1.0/books").then(response => response.json())
  });
}

Note for typescript users

You can add types either by passing type to unitCollmise generic like this:

const allBooksCollmise = unitCollmise<Books[]>();

or by using requestAs method instead of request:

const getAllBooks = () => {
  return allBooksCollmise.requestAs((): Promise<Books[]> => {
    return fetch("/api/v1.0/books").then(response => response.json())
  });
}

In both cases getAllBooks will have the return type Promise<Books[]>

collmise

While unitCollmise might be good for fetching the same data, it won't be much of an use when we want to fetch data with different identifications.

Let's have a function getBookById like this:

const getBookById = (bookId) => {
  return fetch(`/api/v1.0/books/${bookId}`).then(response => response.json())
}

The request/response flow will look like this:

now if we use collmise function like this:

import { collmise } from "collmise";

const booksCollmise = collmise();

const getBookById = (bookId) => {
  return booksCollmise.on(bookId).request(() => {
    return fetch(`/api/v1.0/books/${bookId}`).then(response => response.json())
  });
}

our flow graph will look like this:

Note for typescript users

You can add types either by passing type to collmise generic like this:

const booksCollmise = collmise<number, Books>();

Where first type is the type of identification and the second is the type of data. Alternatively you can use requestAs method instead of request the same way as in unitCollmise

You can cache response the same way as in unitCollmise

Let's have a function for fetching multiple books by ids.

const getManyBooksByIds = (bookIds) => {
  return fetch(`/api/v1.0/books?ids=${bookIds.join()}`).then(response => response.json())
}

Let's say that our flow looks like this:

Of course, we can optimize this flow using getManyBooksByIds method, but it will require us to optimize such cases one-by-one. And it soon becomes a burden if fetching one-by-one is spread across the application.

Let's use collmise and it's main magic - collectors.


const booksCollmise = collmise({
  collectingTimeoutMS: 15, // wait max 15 milliseconds for a single request to be collected by a collector 
}).addCollector({
  name: "manyByIds", // name it as you wish
  findOne: (bookId, books) => books.find(book => book.id === bookId), // find one book from response
  onRequest: (bookIds) => fetch(`/api/v1.0/books?ids=${bookIds.join()}`).then(response => response.json())
});

const getBookById = (bookId, loadFresh = false) => {
  // we can use `fresh` method here too
  return booksCollmise.on(bookId).fresh(loadFresh).request(() => {
    return fetch(`/api/v1.0/books/${bookId}`).then(response => response.json())
  });
}

The flow becomes like this 🔮

If you no longer wish to support sending individual requests at all, we can implement getBookById this way:

const getBookById = (bookId) => {
  return booksCollmise.on(bookId).submitToCollectors();
}

For advanced use cases please see the rest of the documentation here.