Skip to content

Commit

Permalink
fix(files): untangle file names
Browse files Browse the repository at this point in the history
  • Loading branch information
djMax committed Mar 23, 2024
1 parent b415680 commit 9b6f095
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ Now, to invoke the pager, you pass some options:

And then just a list of data sources to use for the fetch.

You can convert a DataSource to a DataGenerator using the queuedDataSource function, which also allows you to specify a filter to be applied after the results are fetched.
You can convert a DataSource to a DataGenerator using the asDataGenerator function, which also allows you to specify a filter to be applied after the results are fetched.

**THERE ARE BUGS** - not because I know what they are but because this is hard and has not been battle tested nearly enough.
2 changes: 1 addition & 1 deletion src/QueuedDataSource.ts → src/asDataGenerator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DataGenerator, DataSource, ResultWithCursor } from './types';

export function queuedDataSource<T extends ResultWithCursor>(
export function asDataGenerator<T extends ResultWithCursor>(
dataSource: DataSource<T>,
filter?: (result: T) => boolean,
): DataGenerator<T> {
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from './queuedDataSource';
export * from './asDataGenerator';
export * from './multiSourcePager';
export * from './types';
8 changes: 4 additions & 4 deletions src/multiSourcePager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import TinyQueue from 'tinyqueue';
import { getCursorArray, toCursor } from './cursor';
import { CollatedDatasource, DataGenerator, DataSource, ResultWithCursor } from './types';
import { ExtractResultType } from './internal-types';
import { queuedDataSource } from './queuedDataSource';
import { asDataGenerator } from './asDataGenerator';

interface QueueEntry {
result: ResultWithCursor;
Expand All @@ -28,13 +28,13 @@ export async function multiSourcePager<Types extends Array<Source>>(
const cursors: string[] = getCursorArray(options.cursor);
let total: number | undefined = 0;

const asDataGenerator = dataSources.map((ds) => ('getGenerator' in ds) ? ds : queuedDataSource(ds));
const generators = asDataGenerator.map((ds, index) => ds.getGenerator(cursors[index], true));
const dataGenerators = dataSources.map((ds) => ('getGenerator' in ds) ? ds : asDataGenerator(ds));
const generators = dataGenerators.map((ds, index) => ds.getGenerator(cursors[index], true));

await Promise.all(generators.map(async (generator, index) => {
const item = await generator.next();
if (total !== undefined) {
const t = asDataGenerator[index].totalResults();
const t = dataGenerators[index].totalResults();
if (t === undefined) {
total = undefined;
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/queuePager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { describe, it, expect } from 'vitest';
import { MockLetterSource, mockDoubleLetters, mockLetters } from '../__tests__/LetterDataSource';

import { multiSourcePager } from './multiSourcePager';
import { queuedDataSource } from './queuedDataSource';
import { asDataGenerator } from './asDataGenerator';

// Comparator for sorting by cursor (ISO date)
const comparator = (a: string, b: string) => a.localeCompare(b);
Expand Down Expand Up @@ -112,7 +112,7 @@ describe('multi source pager', () => {
const dataSourceB = new MockLetterSource(mockDoubleLetters, 5);
const pager = await multiSourcePager({
comparator,
}, queuedDataSource(dataSourceA, (r) => !!(r.data.length % 2)), queuedDataSource(dataSourceB, (r) => !!(r.data.length % 2)));
}, asDataGenerator(dataSourceA, (r) => !!(r.data.length % 2)), asDataGenerator(dataSourceB, (r) => !!(r.data.length % 2)));
const p1 = await pager.getNextResults(10);
expect(p1.results).toMatchInlineSnapshot(`
[
Expand Down

0 comments on commit 9b6f095

Please sign in to comment.