mirror of
https://github.com/pyscript/pyscript.git
synced 2026-02-14 19:00:38 -05:00
Use Promise.all to fetch files part of py-config (#1322)
This is a first step towards loading more stuff simultaneously rather than sequentially. The functional part of this is pretty small: call `calculateFetchPaths` and then `Promise.all(fetchPaths.map(loadFileFromURL));`. I also transposed the return type of `calculateFetchPaths` since it's more convenient to consume this way. I redid the logic in `calculateFetchPaths` a bit. I renamed `src/plugins/fetch.ts` to `calculateFetchPaths.ts` since the file performs no fetching. I also renamed `loadFromFile` to `loadFileFromURL`.
This commit is contained in:
26
pyscriptjs/src/plugins/calculateFetchPaths.ts
Normal file
26
pyscriptjs/src/plugins/calculateFetchPaths.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import { joinPaths } from '../utils';
|
||||
import { FetchConfig } from '../pyconfig';
|
||||
import { UserError, ErrorCode } from '../exceptions';
|
||||
|
||||
export function calculateFetchPaths(fetch_cfg: FetchConfig[]): { url: string; path: string }[] {
|
||||
for (const { files, to_file, from = '' } of fetch_cfg) {
|
||||
if (files !== undefined && to_file !== undefined) {
|
||||
throw new UserError(ErrorCode.BAD_CONFIG, `Cannot use 'to_file' and 'files' parameters together!`);
|
||||
}
|
||||
if (files === undefined && to_file === undefined && from.endsWith('/')) {
|
||||
throw new UserError(
|
||||
ErrorCode.BAD_CONFIG,
|
||||
`Couldn't determine the filename from the path ${from}, please supply 'to_file' parameter.`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return fetch_cfg.flatMap(function ({ from = '', to_folder = '.', to_file, files }) {
|
||||
if (files !== undefined) {
|
||||
return files.map(file => ({ url: joinPaths([from, file]), path: joinPaths([to_folder, file]) }));
|
||||
}
|
||||
const filename = to_file || from.slice(1 + from.lastIndexOf('/'));
|
||||
const to_path = joinPaths([to_folder, filename]);
|
||||
return [{ url: from, path: to_path }];
|
||||
});
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
import { joinPaths } from '../utils';
|
||||
import { FetchConfig } from '../pyconfig';
|
||||
import { UserError, ErrorCode } from '../exceptions';
|
||||
|
||||
export function calculatePaths(fetch_cfg: FetchConfig[]) {
|
||||
const fetchPaths: string[] = [];
|
||||
const paths: string[] = [];
|
||||
fetch_cfg.forEach(function (each_fetch_cfg: FetchConfig) {
|
||||
const from = each_fetch_cfg.from || '';
|
||||
const to_folder = each_fetch_cfg.to_folder || '.';
|
||||
const to_file = each_fetch_cfg.to_file;
|
||||
const files = each_fetch_cfg.files;
|
||||
if (files !== undefined) {
|
||||
if (to_file !== undefined) {
|
||||
throw new UserError(ErrorCode.BAD_CONFIG, `Cannot use 'to_file' and 'files' parameters together!`);
|
||||
}
|
||||
for (const each_f of files) {
|
||||
const each_fetch_path = joinPaths([from, each_f]);
|
||||
fetchPaths.push(each_fetch_path);
|
||||
const each_path = joinPaths([to_folder, each_f]);
|
||||
paths.push(each_path);
|
||||
}
|
||||
} else {
|
||||
fetchPaths.push(from);
|
||||
const filename = to_file || from.split('/').pop();
|
||||
if (filename === '') {
|
||||
throw new UserError(
|
||||
ErrorCode.BAD_CONFIG,
|
||||
`Couldn't determine the filename from the path ${from}, please supply 'to_file' parameter.`,
|
||||
);
|
||||
} else {
|
||||
paths.push(joinPaths([to_folder, filename]));
|
||||
}
|
||||
}
|
||||
});
|
||||
return [paths, fetchPaths];
|
||||
}
|
||||
Reference in New Issue
Block a user