Socket
Book a DemoInstallSign in
Socket

@dalisoft/as-batch

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@dalisoft/as-batch

Convert many calls into batches

latest
Source
npmnpm
Version
0.1.8
Version published
Maintainers
1
Created
Source

asBatch

Greenkeeper badge

Convert many calls into batches

Mostly use this for solving 1+N (N+1) problem

Features

  • Fast
  • Automatic handling
  • No duplication
  • Almost zero-config
  • Flexible
  • On browsers works too
  • Types declaration for IDE/Editor

Import

// ES6
import asBatch from "@dalisoft/as-batch";

// or

// CommonJS
const asBatch = require("@dalisoft/as-batch");

// or

const asBatch = window.asBatch;

Example

const calling = new asBatch({
    onCallsTimeout: async calls => {
      await timeout(2000);

      return await Promise.all(calls);
    }
  });

// No await calling, because await blocks caching and this not works effectively
// Also available other ways, see tests and i'm happy your PR for examples
// Just i don't have time to create examples
  calling
    .call(
      async () => {
        return requestWithSomeLib('[endpoint]/posts);
      },
      responses => responses.find(response => response.src === '/posts').posts
    )
    .then(posts =>
      console.log('see posts', posts)
    );

For more info see tests.

Options

  • cache - store of cache, should be like ES6 Map constructor
  • callsCache - store of calls cache, should be like ES6 Map constructor
  • key - default key for instance, if no one will not specify in methods
  • transform - this options only for .register method as useful for query and/or something similar
  • maxAgeOfCache - max age of cached results and keys which are removed after expiring
  • onRegisterTimeout - this option only for .register method and callback when within specified delay no calls happen, this callback will be fired
  • onRegisterTimeoutDelay - delay ms for onRegisterTimeout
  • onCallsTimeout - this option only for .call method and callback when within specified delay no calls happen, this callback will be fired
  • onCallsTimeoutDelay - delay ms for onCallsTimeout

Methods

.register(key?: string | Function, value?: string | Function, resolve?: Function): Promise | CacheResult | Any

Registers anything and then call onRegisterTimeout if no again calling this method within specified delay

.call(key?: string | Function, value?: string | Function, resolve?: Function): Promise | CacheResult | Any

Returns the cached call within specified delay

.fetchRegistered(): Promise | Any

Fetchs registered caches from .register method

.delete(key: string)

Removes the callback from cache

.clear()

Clear cache instance to be empty

License

MIT

Keywords

call

FAQs

Package last updated on 06 Jun 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts