batchloader
Advanced tools
Comparing version 0.0.2 to 0.0.3
@@ -0,1 +1,2 @@ | ||
import { MappedBatchLoader } from './mappedbatchloader'; | ||
import { IBatchLoader } from './types'; | ||
@@ -13,5 +14,5 @@ export declare type BatchLoadFn<Key, Value> = (keys: Key[]) => Value[] | Promise<Value[]>; | ||
loadMany(keys: Key[]): Promise<Value[]>; | ||
createMapppedLoader<MappedValue>(mapFn: (value: Value) => MappedValue, batchDelay?: number): BatchLoader<Key, MappedValue>; | ||
mapLoader<MappedValue>(mapFn: (value: Value) => MappedValue): MappedBatchLoader<Key, Value, MappedValue>; | ||
protected triggerBatch(): Promise<Value[]>; | ||
protected runBatchNow(): Promise<Value[]>; | ||
} |
@@ -11,2 +11,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const mappedbatchloader_1 = require("./mappedbatchloader"); | ||
class BatchLoader { | ||
@@ -36,15 +37,4 @@ constructor(batchFn, keyToUniqueId, batchDelay = 0) { | ||
} | ||
createMapppedLoader(mapFn, batchDelay = this.batchDelay) { | ||
return new BatchLoader((keys) => __awaiter(this, void 0, void 0, function* () { | ||
const values = yield this.batchFn(keys); | ||
const mapped = values.map(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; | ||
}), this.keyToUniqueId, batchDelay); | ||
mapLoader(mapFn) { | ||
return new mappedbatchloader_1.MappedBatchLoader(this, mapFn); | ||
} | ||
@@ -51,0 +41,0 @@ triggerBatch() { |
@@ -18,7 +18,2 @@ "use strict"; | ||
}; | ||
if (typeof process !== 'undefined' && | ||
process.env && | ||
process.env.NODE_ENV !== 'production') { | ||
console.log('Deprecated! Use BatchLoader.createMapppedLoader instead!'); | ||
} | ||
} | ||
@@ -25,0 +20,0 @@ load(key) { |
{ | ||
"name": "batchloader", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"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", |
@@ -99,3 +99,3 @@ import { BatchLoader } from './batchloader'; | ||
test('sync createMapppedLoader', async () => { | ||
test('sync mapLoader', async () => { | ||
const idss = [] as number[][]; | ||
@@ -111,3 +111,3 @@ const loader = new BatchLoader( | ||
String | ||
).createMapppedLoader(String); | ||
).mapLoader(String); | ||
@@ -139,3 +139,3 @@ expect(await loader.load(3)).toBe('6'); | ||
test('async createMapppedLoader', async () => { | ||
test('async mapLoader', async () => { | ||
const idss = [] as number[][]; | ||
@@ -151,3 +151,3 @@ const loader = new BatchLoader( | ||
String | ||
).createMapppedLoader((x): Promise<string> => Promise.resolve(String(x))); | ||
).mapLoader((x): Promise<string> => Promise.resolve(String(x))); | ||
@@ -154,0 +154,0 @@ expect(await loader.load(3)).toBe('6'); |
@@ -0,1 +1,2 @@ | ||
import { MappedBatchLoader } from 'src/mappedbatchloader'; | ||
import { IBatchLoader } from 'src/types'; | ||
@@ -39,23 +40,6 @@ | ||
public createMapppedLoader<MappedValue>( | ||
mapFn: (value: Value) => MappedValue, | ||
batchDelay = this.batchDelay | ||
): BatchLoader<Key, MappedValue> { | ||
return new BatchLoader( | ||
async (keys: Key[]): Promise<MappedValue[]> => { | ||
const values = await this.batchFn(keys); | ||
const mapped = values.map(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[]; | ||
}, | ||
this.keyToUniqueId, | ||
batchDelay | ||
); | ||
public mapLoader<MappedValue>( | ||
mapFn: (value: Value) => MappedValue | ||
): MappedBatchLoader<Key, Value, MappedValue> { | ||
return new MappedBatchLoader(this, mapFn); | ||
} | ||
@@ -62,0 +46,0 @@ |
@@ -9,12 +9,3 @@ import { IBatchLoader } from 'src/types'; | ||
protected mapFn: (v: Value) => MappedValue | Promise<MappedValue> | ||
) { | ||
if ( | ||
typeof process !== 'undefined' && | ||
process.env && | ||
process.env.NODE_ENV !== 'production' | ||
) { | ||
// tslint:disable-next-line no-console | ||
console.log('Deprecated! Use BatchLoader.createMapppedLoader instead!'); | ||
} | ||
} | ||
) {} | ||
@@ -21,0 +12,0 @@ public load(key: Key): Promise<MappedValue> { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
28201
484