Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

inbatches

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

inbatches - npm Package Compare versions

Comparing version 0.0.6 to 0.0.7

14

dist/batcher.js

@@ -7,3 +7,3 @@ "use strict";

cache = new Map();
executions = [];
callbacks = [];
append(key) {

@@ -14,3 +14,3 @@ if (this.cache.has(key)) {

const promise = new Promise((resolve, reject) => {
this.executions.push({ key, resolve, reject });
this.callbacks.push({ key, resolve, reject });
});

@@ -41,3 +41,3 @@ this.cache.set(key, promise);

if (this.current) {
if (this.current.active && this.current.executions.length < this.options.maxBatchSize) {
if (this.current.active && this.current.callbacks.length < this.options.maxBatchSize) {
return this.current;

@@ -65,7 +65,7 @@ }

batch.active = false;
if (batch.executions.length === 0) {
if (batch.callbacks.length === 0) {
return;
}
try {
this.run(batch.executions.map(callback => callback.key))
this.run(batch.callbacks.map(callback => callback.key))
.then(values => this.fulfill(batch, values))

@@ -79,3 +79,3 @@ .catch(error => this.reject(batch, error));

fulfill(batch, values) {
batch.executions.forEach((callback, index) => {
batch.callbacks.forEach((callback, index) => {
const value = values[index];

@@ -89,3 +89,3 @@ if (value instanceof Error)

reject(batch, error) {
batch.executions.forEach(callback => {
batch.callbacks.forEach(callback => {
callback.reject(error);

@@ -92,0 +92,0 @@ });

{
"name": "inbatches",
"version": "0.0.6",
"version": "0.0.7",
"private": false,

@@ -5,0 +5,0 @@ "license": "MIT",

@@ -1,9 +0,14 @@

# InBatches (📦📦📦)
# @InBatches(📦,📦,📦,...)
InBatches is a zero-dependency TypeScript library that provides a convenient way to batch and execute asynchronous
operations in a controlled manner. This library is especially useful for scenarios where you need to perform multiple
asynchronous operations efficiently, such as when making network requests or performing database queries.
InBatches is a zero-dependency generic TypeScript library that provides a convenient way to batch executions that runs
asynchronous.
Heavily inspired by [graphql/dataloader](https://github.com/graphql/dataloader) but better 😜
It is designed to be used as part of your application's data fetching layer to provide a consistent API over various
backends and reduce requests to those backends via batching.
This library is especially useful for scenarios where you need to perform multiple asynchronous operations efficiently,
such as when making network requests or performing database queries.
Heavily inspired by [graphql/dataloader](https://github.com/graphql/dataloader) but using classes and decorators 😜
## Table of Contents

@@ -30,3 +35,3 @@

### Basic Usage
### Using the `Batcher` Class

@@ -37,2 +42,3 @@ ```typescript

// Define a class that extends Batcher and implements the `run` method
// the `run` method will be called with an array of keys collected from the `enqueue` method
class MyBatcher extends Batcher<number, string> {

@@ -49,12 +55,10 @@ async run(ids: number[]): Promise<string[]> {

// Enqueue keys for batching and execution
const resultPromise1 = batcher.enqueue(1);
const resultPromise2 = batcher.enqueue(2);
resultPromise1.then(result => {
console.log(result); // Output: "{ id: 1, name: 'Result for key 1' }"
// Enqueue keys for batched execution
const result = [1, 2, 3, 4, 5].map(async id => {
return await batcher.enqueue(id);
});
resultPromise2.then(result => {
console.log(result); // Output: "{ id: 2, name: 'Result for key 2' }"
// The result will be an array of results in the same order as the keys
result.then(results => {
console.log(results); // Output: [{ id: 1, name: 'Result for key 1' }, ...]
});

@@ -71,6 +75,9 @@ ```

class MyService {
@InBatches()
// (optional) overloaded method, where you define the keys as `number` and the return type as `string` for typings
async fetch(keys: number): Promise<string>;
// in reality the Decorator will wrap this method and it will never be called with a single key :)
@InBatches() // This method is now batch-enabled
async fetch(keys: number | number[]): Promise<string | string[]> {
// This method is now batch-enabled
// Perform asynchronous operations using the keys
if (Array.isArray(keys)) {

@@ -87,12 +94,9 @@ return this.db.getMany(keys);

// Enqueue keys for batching and execution
const resultPromise1 = service.fetch(1);
const resultPromise2 = service.fetch(2);
resultPromise1.then(results => {
console.log(results); // Output: { id: 1, name: 'Result for key 1' }
const result = [1, 2, 3, 4, 5].map(async id => {
return await service.fetch(id);
});
resultPromise2.then(results => {
console.log(results); // Output: { id: 2, name: 'Result for key 2' }
// The result will be an array of results in the same order as the keys
result.then(results => {
console.log(results); // Output: [{ id: 1, name: 'Result for key 1' }, ...]
});

@@ -108,3 +112,5 @@ ```

- `maxBatchSize`: The maximum number of keys to batch together. Default is `25`.
- `delayWindowInMs`: The delay window in milliseconds before dispatching the batch. Default is `undefined`.
- `delayWindowInMs`: (not recommended) The delay window in milliseconds before dispatching the batch. Default
is `undefined` and will use `process.nextTick` to dispatch the batch, which is highly efficient and fast. Only use
this if you really want to accumulate promises calls in a window of time before dispatching the batch.

@@ -125,11 +131,15 @@ ### `Batcher<K, V>` Class

```typescript
class MyService {
@InBatches({ maxBatchSize: 10 })
async fetchResults(keys: number | number[]): Promise<string | string[]> {
// Batch-enabled method logic
}
```typescript
class MyService {
// (optional) overloaded method, where you define the keys as `number` and the return type as `string` for typings
async fetchResults(keys: number): Promise<string>
@InBatches({ maxBatchSize: 10 })
async fetchResults(keys: number | number[]): Promise<string | string[]> {
// Batch-enabled method logic
}
```
}
```
## Contributing

@@ -136,0 +146,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc