Skip to content

Commit

Permalink
Merge pull request #202 from mrmlnc/ISSUE-181_async_method_as_default…
Browse files Browse the repository at this point in the history
…_method

ISSUE-181: Set async method as default method
  • Loading branch information
mrmlnc authored Jun 10, 2019
2 parents 87ed886 + ccde25e commit 40a1096
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 46 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ $ npm install --save fast-glob
const fg = require('fast-glob');

fg(['src/**/*.js', '!src/**/*.spec.js']).then((entries) => console.log(entries));
fg.async(['src/**/*.js', '!src/**/*.spec.js']).then((entries) => console.log(entries));
```

#### Synchronous
Expand Down Expand Up @@ -61,7 +60,6 @@ stream.once('end', () => console.log(entries));
## API

### fg(patterns, [options])
### fg.async(patterns, [options])

Returns a `Promise` with an array of matching [entries](#entry).

Expand Down
2 changes: 1 addition & 1 deletion src/benchmark/suites/async/fast-glob-current.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const settings = new Settings({

const timeStart = utils.timeStart();

glob.async(process.env.BENCHMARK_PATTERN as string, settings)
glob(process.env.BENCHMARK_PATTERN as string, settings)
.then((matches) => {
const memory = utils.getMemory();
const time = utils.timeEnd(timeStart);
Expand Down
6 changes: 3 additions & 3 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe('Package', () => {
it('should throw an error when input values can not pass validation', async () => {
try {
/* tslint:disable-next-line no-any */
await pkg.async(null as any);
await pkg(null as any);
throw new Error('An unexpected error was found.');
} catch (error) {
assert.strictEqual((error as Error).toString(), 'TypeError: Patterns must be a string or an array of strings');
Expand All @@ -69,7 +69,7 @@ describe('Package', () => {
'fixtures/second/nested/file.md'
];

const actual = await pkg.async(['fixtures/**/*.md']);
const actual = await pkg(['fixtures/**/*.md']);

actual.sort();

Expand All @@ -86,7 +86,7 @@ describe('Package', () => {
'fixtures/second/nested/file.md'
];

const actual = await pkg.async(['fixtures/first/**/*.md', 'fixtures/second/**/*.md']);
const actual = await pkg(['fixtures/first/**/*.md', 'fixtures/second/**/*.md']);

actual.sort();

Expand Down
71 changes: 31 additions & 40 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,9 @@ type EntryObjectModePredicate = { [P in keyof Pick<Options, 'objectMode'>]-?: tr
type EntryStatsPredicate = { [P in keyof Pick<Options, 'stats'>]-?: true };
type EntryObjectPredicate = EntryObjectModePredicate | EntryStatsPredicate;

function sync(source: Pattern | Pattern[], options: Options & EntryObjectPredicate): Entry[];
function sync(source: Pattern | Pattern[], options?: Options): string[];
function sync(source: Pattern | Pattern[], options?: Options): EntryItem[] {
assertPatternsInput(source);

const works = getWorks(source, ProviderSync, options);

return utils.array.flatten(works);
}

function async(source: Pattern | Pattern[], options: Options & EntryObjectPredicate): Promise<Entry[]>;
function async(source: Pattern | Pattern[], options?: Options): Promise<string[]>;
function async(source: Pattern | Pattern[], options?: Options): Promise<EntryItem[]> {
function FastGlob(source: Pattern | Pattern[], options: Options & EntryObjectPredicate): Promise<Entry[]>;
function FastGlob(source: Pattern | Pattern[], options?: Options): Promise<string[]>;
function FastGlob(source: Pattern | Pattern[], options?: Options): Promise<EntryItem[]> {
try {
assertPatternsInput(source);
} catch (error) {
Expand All @@ -37,26 +27,38 @@ function async(source: Pattern | Pattern[], options?: Options): Promise<EntryIte
return Promise.all(works).then(utils.array.flatten);
}

function stream(source: Pattern | Pattern[], options?: Options): NodeJS.ReadableStream {
assertPatternsInput(source);
namespace FastGlob {
export function sync(source: Pattern | Pattern[], options: Options & EntryObjectPredicate): Entry[];
export function sync(source: Pattern | Pattern[], options?: Options): string[];
export function sync(source: Pattern | Pattern[], options?: Options): EntryItem[] {
assertPatternsInput(source);

const works = getWorks(source, ProviderStream, options);
const works = getWorks(source, ProviderSync, options);

/**
* The stream returned by the provider cannot work with an asynchronous iterator.
* To support asynchronous iterators, regardless of the number of tasks, we always multiplex streams.
* This affects performance (+25%). I don't see best solution right now.
*/
return utils.stream.merge(works);
}
return utils.array.flatten(works);
}

export function stream(source: Pattern | Pattern[], options?: Options): NodeJS.ReadableStream {
assertPatternsInput(source);

function generateTasks(source: Pattern | Pattern[], options?: Options): Task[] {
assertPatternsInput(source);
const works = getWorks(source, ProviderStream, options);

const patterns = ([] as Pattern[]).concat(source);
const settings = new Settings(options);
/**
* The stream returned by the provider cannot work with an asynchronous iterator.
* To support asynchronous iterators, regardless of the number of tasks, we always multiplex streams.
* This affects performance (+25%). I don't see best solution right now.
*/
return utils.stream.merge(works);
}

return taskManager.generate(patterns, settings);
export function generateTasks(source: Pattern | Pattern[], options?: Options): Task[] {
assertPatternsInput(source);

const patterns = ([] as Pattern[]).concat(source);
const settings = new Settings(options);

return taskManager.generate(patterns, settings);
}
}

function getWorks<T>(source: Pattern | Pattern[], _Provider: new (settings: Settings) => Provider<T>, options?: Options): T[] {
Expand All @@ -82,15 +84,4 @@ function isString(source: unknown): source is string {
return typeof source === 'string';
}

export default async;
export {
async,
sync,
stream,
generateTasks,

Options,
Settings,
Task,
EntryItem
};
export = FastGlob;

0 comments on commit 40a1096

Please sign in to comment.