Revisit the loader implementation (again)

This commit is contained in:
0x1eef 2024-10-02 06:44:54 -03:00
parent e24cc3d7fc
commit a28903c8b7
3 changed files with 33 additions and 33 deletions

View file

@ -2,10 +2,10 @@ import type { Item, FontItem } from './postman/item';
import item from './postman/item'; import item from './postman/item';
import request from './postman/request'; import request from './postman/request';
type Postman = { fetch: () => Promise<Package> }; type Postman = { deliver: () => Promise<Parcel> };
type Args = Array<Item | FontItem | Function> type Args = Array<Item | FontItem | Function>
type Items = Array<Item | FontItem>; type Items = Array<Item | FontItem>;
type Package = { export type Parcel = {
fonts: FontFace[] fonts: FontFace[]
images: HTMLElement[] images: HTMLElement[]
css: HTMLElement[] css: HTMLElement[]
@ -30,7 +30,7 @@ export { item };
export default function (...args: Args) { export default function (...args: Args) {
const self: Postman = Object.create(null); 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); const [items, callback] = parseArgs(args);
items.sort((i1, i2) => i1.priority >= i2.priority ? 1 : -1); items.sort((i1, i2) => i1.priority >= i2.priority ? 1 : -1);
@ -62,11 +62,11 @@ export default function (...args: Args) {
/* unreachable */ /* unreachable */
return null; return null;
}); });
return reqs as Array<Promise<Package>>; return reqs as Array<Promise<Parcel>>;
}; };
self.fetch = async () => { self.deliver = async () => {
await Promise.all<Package>(spawnRequests()); await Promise.all<Parcel>(spawnRequests());
return result; return result;
}; };

View file

@ -1,4 +1,5 @@
import postman, { item } from "postman"; import postman, { item } from "postman";
import type { Parcel } from "postman";
import { formatNumber } from "~/lib/t"; import { formatNumber } from "~/lib/t";
(function () { (function () {
@ -19,7 +20,7 @@ import { formatNumber } from "~/lib/t";
return f; return f;
})(); })();
postman( const delivery = postman(
item.script(`/js/main/vendor.js?v=${rev}`, { id: "0" }), item.script(`/js/main/vendor.js?v=${rev}`, { id: "0" }),
item.script(`/js/main/surah-index.js?v=${rev}`, { id: "1" }), item.script(`/js/main/surah-index.js?v=${rev}`, { id: "1" }),
...fonts, ...fonts,
@ -29,13 +30,12 @@ import { formatNumber } from "~/lib/t";
bar.value = percent; bar.value = percent;
num.innerText = formatNumber(docel.lang, Number(percent.toFixed(0))); num.innerText = formatNumber(docel.lang, Number(percent.toFixed(0)));
}), }),
) ).deliver();
.fetch() delivery.then((parcel: Parcel) => {
.then((pkg) => {
[main, css].forEach((el) => el.remove()); [main, css].forEach((el) => el.remove());
pkg.fonts.forEach((f) => document.fonts.add(f)); parcel.fonts.forEach((f) => document.fonts.add(f));
pkg.css.forEach((s) => document.head.appendChild(s)); parcel.css.forEach((s: HTMLElement) => document.head.appendChild(s));
pkg.scripts parcel.scripts
.sort((a, b) => Number(a.id) - Number(b.id)) .sort((a, b) => Number(a.id) - Number(b.id))
.forEach((s) => { .forEach((s) => {
document.body.removeChild(document.body.appendChild(s)); document.body.removeChild(document.body.appendChild(s));

View file

@ -1,4 +1,5 @@
import postman, { item } from "postman"; import postman, { item } from "postman";
import type { Parcel } from "postman";
import { formatNumber } from "~/lib/t"; import { formatNumber } from "~/lib/t";
(function () { (function () {
@ -21,7 +22,7 @@ import { formatNumber } from "~/lib/t";
return f; return f;
})(); })();
postman( const delivery = postman(
item.script(`/js/main/vendor.js?v=${rev}`, { id: "0" }), item.script(`/js/main/vendor.js?v=${rev}`, { id: "0" }),
item.script(`/js/main/surah-stream.js?v=${rev}`, { id: "1" }), item.script(`/js/main/surah-stream.js?v=${rev}`, { id: "1" }),
...fonts, ...fonts,
@ -36,14 +37,13 @@ import { formatNumber } from "~/lib/t";
bar.value = percent; bar.value = percent;
num.innerText = formatNumber(docel.lang, Number(percent.toFixed(0))); num.innerText = formatNumber(docel.lang, Number(percent.toFixed(0)));
}), }),
) ).deliver();
.fetch() delivery.then((parcel: Parcel) => {
.then((pkg) => {
[main, css].forEach((el) => el.remove()); [main, css].forEach((el) => el.remove());
pkg.fonts.forEach((f) => document.fonts.add(f)); parcel.fonts.forEach((f) => document.fonts.add(f));
pkg.css.forEach((s) => document.head.appendChild(s)); parcel.css.forEach((s) => document.head.appendChild(s));
pkg.json.forEach((o) => document.body.appendChild(o)); parcel.json.forEach((o) => document.body.appendChild(o));
pkg.scripts parcel.scripts
.sort((a, b) => Number(a.id) - Number(b.id)) .sort((a, b) => Number(a.id) - Number(b.id))
.forEach((s) => { .forEach((s) => {
document.body.removeChild(document.body.appendChild(s)); document.body.removeChild(document.body.appendChild(s));