From a28903c8b7d30bebd8985bb0b53f81673ccd4abf Mon Sep 17 00:00:00 2001 From: 0x1eef <0x1eef@protonmail.com> Date: Wed, 2 Oct 2024 06:44:54 -0300 Subject: [PATCH] Revisit the loader implementation (again) --- packages/typescript/postman/src/index.ts | 12 +++++----- src/js/loaders/SurahIndexLoader.ts | 26 +++++++++++----------- src/js/loaders/SurahStreamLoader.ts | 28 ++++++++++++------------ 3 files changed, 33 insertions(+), 33 deletions(-) diff --git a/packages/typescript/postman/src/index.ts b/packages/typescript/postman/src/index.ts index 6373840..b76b6aa 100644 --- a/packages/typescript/postman/src/index.ts +++ b/packages/typescript/postman/src/index.ts @@ -2,10 +2,10 @@ import type { Item, FontItem } from './postman/item'; import item from './postman/item'; import request from './postman/request'; -type Postman = { fetch: () => Promise }; +type Postman = { deliver: () => Promise }; type Args = Array type Items = Array; -type Package = { +export type Parcel = { fonts: FontFace[] images: HTMLElement[] css: HTMLElement[] @@ -30,7 +30,7 @@ export { item }; export default function (...args: Args) { const self: Postman = Object.create(null); - const result: Package = { fonts: [], images: [], css: [], scripts: [], json: [] }; + const result: Parcel = { fonts: [], images: [], css: [], scripts: [], json: [] }; const [items, callback] = parseArgs(args); items.sort((i1, i2) => i1.priority >= i2.priority ? 1 : -1); @@ -62,11 +62,11 @@ export default function (...args: Args) { /* unreachable */ return null; }); - return reqs as Array>; + return reqs as Array>; }; - self.fetch = async () => { - await Promise.all(spawnRequests()); + self.deliver = async () => { + await Promise.all(spawnRequests()); return result; }; diff --git a/src/js/loaders/SurahIndexLoader.ts b/src/js/loaders/SurahIndexLoader.ts index 0b68662..b7ff37d 100644 --- a/src/js/loaders/SurahIndexLoader.ts +++ b/src/js/loaders/SurahIndexLoader.ts @@ -1,4 +1,5 @@ import postman, { item } from "postman"; +import type { Parcel } from "postman"; import { formatNumber } from "~/lib/t"; (function () { @@ -19,7 +20,7 @@ import { formatNumber } from "~/lib/t"; return f; })(); - postman( + const delivery = postman( item.script(`/js/main/vendor.js?v=${rev}`, { id: "0" }), item.script(`/js/main/surah-index.js?v=${rev}`, { id: "1" }), ...fonts, @@ -29,16 +30,15 @@ import { formatNumber } from "~/lib/t"; bar.value = percent; num.innerText = formatNumber(docel.lang, Number(percent.toFixed(0))); }), - ) - .fetch() - .then((pkg) => { - [main, css].forEach((el) => el.remove()); - pkg.fonts.forEach((f) => document.fonts.add(f)); - pkg.css.forEach((s) => document.head.appendChild(s)); - pkg.scripts - .sort((a, b) => Number(a.id) - Number(b.id)) - .forEach((s) => { - document.body.removeChild(document.body.appendChild(s)); - }); - }); + ).deliver(); + delivery.then((parcel: Parcel) => { + [main, css].forEach((el) => el.remove()); + parcel.fonts.forEach((f) => document.fonts.add(f)); + parcel.css.forEach((s: HTMLElement) => document.head.appendChild(s)); + parcel.scripts + .sort((a, b) => Number(a.id) - Number(b.id)) + .forEach((s) => { + document.body.removeChild(document.body.appendChild(s)); + }); + }); })(); diff --git a/src/js/loaders/SurahStreamLoader.ts b/src/js/loaders/SurahStreamLoader.ts index bb7c7fd..ba91cd6 100644 --- a/src/js/loaders/SurahStreamLoader.ts +++ b/src/js/loaders/SurahStreamLoader.ts @@ -1,4 +1,5 @@ import postman, { item } from "postman"; +import type { Parcel } from "postman"; import { formatNumber } from "~/lib/t"; (function () { @@ -21,7 +22,7 @@ import { formatNumber } from "~/lib/t"; return f; })(); - postman( + const delivery = postman( item.script(`/js/main/vendor.js?v=${rev}`, { id: "0" }), item.script(`/js/main/surah-stream.js?v=${rev}`, { id: "1" }), ...fonts, @@ -36,17 +37,16 @@ import { formatNumber } from "~/lib/t"; bar.value = percent; num.innerText = formatNumber(docel.lang, Number(percent.toFixed(0))); }), - ) - .fetch() - .then((pkg) => { - [main, css].forEach((el) => el.remove()); - pkg.fonts.forEach((f) => document.fonts.add(f)); - pkg.css.forEach((s) => document.head.appendChild(s)); - pkg.json.forEach((o) => document.body.appendChild(o)); - pkg.scripts - .sort((a, b) => Number(a.id) - Number(b.id)) - .forEach((s) => { - document.body.removeChild(document.body.appendChild(s)); - }); - }); + ).deliver(); + delivery.then((parcel: Parcel) => { + [main, css].forEach((el) => el.remove()); + parcel.fonts.forEach((f) => document.fonts.add(f)); + parcel.css.forEach((s) => document.head.appendChild(s)); + parcel.json.forEach((o) => document.body.appendChild(o)); + parcel.scripts + .sort((a, b) => Number(a.id) - Number(b.id)) + .forEach((s) => { + document.body.removeChild(document.body.appendChild(s)); + }); + }); })();