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

bulkage

v1.0.1

Published

Bulk and Debounce execution and resolution of promises

Downloads

3

Readme

Build Status

node-bulkage

Bulk and Debounce execution and resolution of promises

Installation

npm install --save bulkage

Usage

Bulkage makes it easy to write functions or methods whose signature is unitary but actually perform I/O in bulk. It is useful for:

Implementing I/O optimizations without changing your business logic

const getUserById: (id: string) => Promise<User> = Bulkage(async (bulk: Array<[string]>) => {
  const allIds = bulk.map(([id]) => id)
  const allUsers = await fetchManyUsersByIds(allIds)
  return allIds.map((id) => allUsers.find((user) => user.id === id))
})

const users = await Promise.all([
  getById(1),
  getById(2),
  getById(3),
])
///^ These are all made in one bulk request
///^ for example: SELECT * from users where id in ('1', '2', '3');

It is worth noting that several calls can be bulked together even when they are executed in different contexts (e.g. differents HTTP requests)

Debouncing costly or complex processing

By default, Bulkage bulks calls made on the same tick. But you can use a policy based on a time window. The call is then debounced according to the number of milliseconds you specified.

import delay from 'delay'
const processStuff: () => Promise<void> = Bulkage(10, async (bulk: Array<[]>) => { await doStuff() })
processStuff() ///< this one is first bulk
processStuff() ///< this one is first bulk
await delay(9)
processStuff() ///< this one is first bulk
await delay(10)
processStuff() ///< This one is another bulk

Keeping track of background or deferred logic

There is more fine tuning available for the policy of the debounce strategy. for example with a garbage collection usecase:

import ms from 'ms'
const removeRef: (reference: Ref) => Promise<void> = Bulkage({ debounce: ms('2s'), max: ms('20s') }, async (bulk: Array<[Reference]>) => {
  const referencesToRemove = bulk.map(([reference]) => reference)
  await removeReferences(referencesToRemove)
})

Reference

Bulkage(resolver: (bulk: Array<[...Arguments]>) => Promise<Array<Result>>): Bulkage.Bulkage<...Arguments, Result>

Creates a new Bulkage. A Bulkage is a function with a unitary-style signature. The resolver is the function that will be called when several calls need to be executed. The type of the resulting Bulkage is inferred from the type of the resolver. Here ...Arguments means a finite list of types. Result can be any type including void.

For example:

const getTimesTwo = Bulkage(async (bulk: Array<[number]>) => {
  return bulk.map(([n]) => n * 2)
})
typeof getTimesTwo ///> (n: number) => Promise<number>

The return type of the resolver can be void which means the resulting Bulkage will have a return type of Promise<void>. However if the return type of the resolver is an array, the it MUST have the same length as the bulk for which it was called. This is necessary because the index of each element in the resulting array is used to associate it with the corresponding pending call.

Bulkage(policy, resolver)

Same as above, except the policy is used to change the behaviour of the scheduler.

  • policy: number: Use a DebounceScheduler which will debounce calls within this number of milliseconds
  • policy: { debounce: number }: Same as above
  • policy: { debounce: number, max: number }: Same as above except calls cannot be debounced indefinitely and will eventually be called after max milliseconds

If policy is omitted, the a conservative TickScheduler is used which only bulks calls made on the exact same tick.

Debugging

Bulkage uses the debug module to trace its behaviour. Start your program with environment DEBUG=bulkage or DEBUG=bulkage:trace to see what is going on.

Test

You can run the tests with npm test. You will need to know mocha

Contributing

Anyone is welcome to submit issues and pull requests

License

MIT

Copyright (c) 2020 Florent Jaby

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.