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

batchloader

Package Overview
Dependencies
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

batchloader - npm Package Compare versions

Comparing version 0.0.9 to 0.0.10

lib/cacheproxyloader.d.ts

6

lib/batchloader.d.ts

@@ -1,2 +0,2 @@

import { Cache, CacheLoader } from './cacheloader';
import { CacheLoader, ICache } from './cacheloader';
import { MappedBatchLoader } from './mappedbatchloader';

@@ -16,4 +16,4 @@ import { IBatchLoader } from './types';

loadMany(keys: Key[]): Promise<Value[]>;
mapLoader<MappedValue>(mapFn: (value: Value) => MappedValue): MappedBatchLoader<Key, Value, MappedValue>;
cacheLoader(cache?: Cache<Key, Value>): CacheLoader<Key, Value>;
mapLoader<MappedValue>(mapFn: (value: Value, key: Key) => MappedValue): MappedBatchLoader<Key, Value, MappedValue>;
cacheLoader(cache?: ICache<Key, Value>): CacheLoader<Key, Value>;
protected triggerBatch(): Promise<Value[]>;

@@ -20,0 +20,0 @@ protected runBatchNow(): Promise<Value[]>;

import { MappedBatchLoader } from './mappedbatchloader';
import { IBatchLoader, MaybePromise } from './types';
export interface Cache<Key, Value> {
export interface ICache<Key, Value> {
clear(): void;

@@ -9,10 +9,10 @@ delete(key: Key): boolean;

}
export declare class CacheLoader<Key, Value> implements IBatchLoader<Key, Value>, Cache<Key, MaybePromise<Value>> {
export declare class CacheLoader<Key, Value> implements IBatchLoader<Key, Value>, ICache<Key, MaybePromise<Value>> {
protected loader: IBatchLoader<Key, Value>;
cache: Cache<Key, Value>;
cache: ICache<Key, Value>;
promiseCache: Map<Key, Promise<Value>>;
constructor(loader: IBatchLoader<Key, Value>, cache?: Cache<Key, Value>);
constructor(loader: IBatchLoader<Key, Value>, cache?: ICache<Key, Value>);
load(key: Key): Promise<Value>;
loadMany(keys: Key[]): Promise<Value[]>;
mapLoader<MappedValue>(mapFn: (value: Value) => MappedValue): MappedBatchLoader<Key, Value, MappedValue>;
mapLoader<MappedValue>(mapFn: (value: Value, key: Key) => MappedValue): MappedBatchLoader<Key, Value, MappedValue>;
get(key: Key): Promise<Value> | undefined;

@@ -19,0 +19,0 @@ set(key: Key, value: Value): void;

export * from './batchloader';
export * from './mappedbatchloader';
export * from './cacheproxyloader';

@@ -8,2 +8,3 @@ "use strict";

__export(require("./mappedbatchloader"));
__export(require("./cacheproxyloader"));
//# sourceMappingURL=index.js.map
import { IBatchLoader } from './types';
export declare class MappedBatchLoader<Key, Value, MappedValue> implements IBatchLoader<Key, MappedValue> {
protected loader: IBatchLoader<Key, Value>;
protected mapFn: (v: Value) => MappedValue | Promise<MappedValue>;
constructor(loader: IBatchLoader<Key, Value>, mapFn: (v: Value) => MappedValue | Promise<MappedValue>);
protected mapFn: (value: Value, key: Key) => MappedValue | Promise<MappedValue>;
constructor(loader: IBatchLoader<Key, Value>, mapFn: (value: Value, key: Key) => MappedValue | Promise<MappedValue>);
load(key: Key): Promise<MappedValue>;
loadMany(keys: Key[]): Promise<MappedValue[]>;
mapLoader<RemappedValue>(mapFn: (value: MappedValue) => RemappedValue): MappedBatchLoader<Key, MappedValue, RemappedValue>;
private mapItems;
mapLoader<RemappedValue>(mapFn: (value: MappedValue, key: Key) => RemappedValue): MappedBatchLoader<Key, MappedValue, RemappedValue>;
}

@@ -7,19 +7,20 @@ "use strict";

this.mapFn = mapFn;
this.mapItems = (vs) => {
const mapped = vs.map(this.mapFn);
const len = mapped.length;
for (let i = 0; i < len; i += 1) {
const item = mapped[i];
if (item != null && typeof item.then === 'function') {
return Promise.all(mapped);
}
}
return mapped;
};
}
load(key) {
return this.loader.load(key).then(this.mapFn);
return this.loader.load(key).then((value) => this.mapFn(value, key));
}
loadMany(keys) {
return this.loader.loadMany(keys).then(this.mapItems);
return this.loader.loadMany(keys).then((values) => {
let hasPromise = false;
const results = [];
const len = values.length;
for (let i = 0; i < len; i += 1) {
const res = this.mapFn(values[i], keys[i]);
results.push(res);
hasPromise =
hasPromise ||
(res != null && typeof res.then === 'function');
}
return hasPromise ? Promise.all(results) : results;
});
}

@@ -26,0 +27,0 @@ mapLoader(mapFn) {

{
"name": "batchloader",
"version": "0.0.9",
"version": "0.0.10",
"description": "BatchLoader is a utility for data fetching layer to reduce requests via batching written in TypeScript. Inspired by Facebook's DataLoader",

@@ -5,0 +5,0 @@ "main": "lib/index.js",

@@ -1,2 +0,2 @@

import { Cache, CacheLoader } from 'src/cacheloader';
import { CacheLoader, ICache } from 'src/cacheloader';
import { MappedBatchLoader } from 'src/mappedbatchloader';

@@ -52,3 +52,3 @@ import { IBatchLoader, MaybePromise } from 'src/types';

public mapLoader<MappedValue>(
mapFn: (value: Value) => MappedValue
mapFn: (value: Value, key: Key) => MappedValue
): MappedBatchLoader<Key, Value, MappedValue> {

@@ -58,3 +58,3 @@ return new MappedBatchLoader(this, mapFn);

public cacheLoader(cache?: Cache<Key, Value>): CacheLoader<Key, Value> {
public cacheLoader(cache?: ICache<Key, Value>): CacheLoader<Key, Value> {
return new CacheLoader(this, cache);

@@ -61,0 +61,0 @@ }

import { MappedBatchLoader } from 'src/mappedbatchloader';
import { IBatchLoader, MaybePromise } from 'src/types';
// tslint:disable-next-line interface-name
export interface Cache<Key, Value> {
export interface ICache<Key, Value> {
clear(): void;

@@ -13,3 +12,3 @@ delete(key: Key): boolean;

export class CacheLoader<Key, Value>
implements IBatchLoader<Key, Value>, Cache<Key, MaybePromise<Value>> {
implements IBatchLoader<Key, Value>, ICache<Key, MaybePromise<Value>> {
public promiseCache: Map<Key, Promise<Value>>;

@@ -19,3 +18,3 @@

protected loader: IBatchLoader<Key, Value>,
public cache: Cache<Key, Value> = new Map<Key, Value>()
public cache: ICache<Key, Value> = new Map<Key, Value>()
) {

@@ -48,3 +47,3 @@ this.promiseCache = new Map<Key, Promise<Value>>();

public mapLoader<MappedValue>(
mapFn: (value: Value) => MappedValue
mapFn: (value: Value, key: Key) => MappedValue
): MappedBatchLoader<Key, Value, MappedValue> {

@@ -51,0 +50,0 @@ return new MappedBatchLoader(this, mapFn);

export * from './batchloader';
export * from './mappedbatchloader';
export * from './cacheproxyloader';
import { IBatchLoader } from 'src/types';
// tslint:disable-next-line max-classes-per-file
export class MappedBatchLoader<Key, Value, MappedValue>

@@ -8,31 +7,33 @@ implements IBatchLoader<Key, MappedValue> {

protected loader: IBatchLoader<Key, Value>,
protected mapFn: (v: Value) => MappedValue | Promise<MappedValue>
protected mapFn: (
value: Value,
key: Key
) => MappedValue | Promise<MappedValue>
) {}
public load(key: Key): Promise<MappedValue> {
return this.loader.load(key).then(this.mapFn);
return this.loader.load(key).then((value) => this.mapFn(value, key));
}
public loadMany(keys: Key[]): Promise<MappedValue[]> {
return this.loader.loadMany(keys).then(this.mapItems);
return this.loader.loadMany(keys).then((values) => {
let hasPromise = false;
const results: Array<MappedValue | Promise<MappedValue>> = [];
const len = values.length;
for (let i = 0; i < len; i += 1) {
const res = this.mapFn(values[i], keys[i]);
results.push(res);
hasPromise =
hasPromise ||
(res != null && typeof (res as any).then === 'function');
}
return hasPromise ? Promise.all(results) : (results as MappedValue[]);
});
}
public mapLoader<RemappedValue>(
mapFn: (value: MappedValue) => RemappedValue
mapFn: (value: MappedValue, key: Key) => RemappedValue
): MappedBatchLoader<Key, MappedValue, RemappedValue> {
return new MappedBatchLoader(this, mapFn);
}
private mapItems = (vs: Value[]): MappedValue[] | Promise<MappedValue[]> => {
const mapped = vs.map(this.mapFn);
const len = mapped.length;
for (let i = 0; i < len; i += 1) {
const item = mapped[i];
if (item != null && typeof (item as any).then === 'function') {
// has at least one promise
return Promise.all(mapped);
}
}
return mapped as MappedValue[];
};
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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