asBatch

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
import asBatch from "@dalisoft/as-batch";
const asBatch = require("@dalisoft/as-batch");
const asBatch = window.asBatch;
Example
const calling = new asBatch({
onCallsTimeout: async calls => {
await timeout(2000);
return await Promise.all(calls);
}
});
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