From 466c10ba08bd284f51edc33c2ae81a3ab80ca09c Mon Sep 17 00:00:00 2001 From: 0x1eef <0x1eef@protonmail.com> Date: Fri, 12 Jul 2024 01:51:20 -0300 Subject: [PATCH] Add improvements --- package.json | 4 +++- src/index.ts | 36 ++++++++++++++++++++---------------- 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/package.json b/package.json index a8177c8..ce11cb8 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,9 @@ ], "scripts": { "build": "npm exec tsc", - "prepare": "npm run build" + "prepare": "npm run build", + "tsc": "npm exec tsc -- --noEmit", + "eslint": "npm exec eslint -- src/*.ts src/**/*.ts" }, "repository": { "type": "git", diff --git a/src/index.ts b/src/index.ts index 9402baa..7443517 100644 --- a/src/index.ts +++ b/src/index.ts @@ -25,51 +25,55 @@ function parseArgs(args: Args): [Items, (n: number) => number] { items.push(item); } }); - return [items, progresscb]; + return [items.sort((i1, i2) => (i1.priority >= i2.priority ? 1 : -1)), progresscb]; } export { item }; export default function (...args: Args): Postman { - const self: Postman = Object.create(null); - const result: Package = { fonts: [], images: [], css: [], scripts: [], json: [] }; + const self = Object.create(null); + const _package: Package = { + fonts: [], + images: [], + css: [], + scripts: [], + json: [], + }; const [items, progresscb] = parseArgs(args); - items.sort((i1, i2) => (i1.priority >= i2.priority ? 1 : -1)); let index = 0; - const onProgress = (el: T): T => { + function onProgress(el: T): T { index++; if (index <= items.length) { progresscb(100 * (index / items.length)); } return el; - }; + } - const spawnRequests = (): Array> => { - const reqs = items.map(async (item: Item | FontItem) => { + function fetch(): Array> { + return items.map(async (item: Item | FontItem) => { if ("fontFamily" in item) { const req = request.font; return await req(item) .then(el => onProgress(el)) - .then(font => result.fonts.push(font)) - .then(() => result); + .then(font => _package.fonts.push(font)) + .then(() => _package); } else if (item.requestId !== "font" && item.group !== "fonts") { const req = request[item.requestId]; - const ary = result[item.group]; + const ary = _package[item.group]; return await req(item) .then(el => onProgress(el)) .then(el => ary.push(el)) - .then(() => result); + .then(() => _package); } else { return null; } }); - return reqs; - }; + } self.deliver = async () => { - await Promise.all(spawnRequests()); - return result; + await Promise.all(fetch()); + return _package; }; return self;