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 request from './postman/request';
type Postman = { fetch: () => Promise<Package> };
type Postman = { deliver: () => Promise<Parcel> };
type Args = Array<Item | FontItem | Function>
type Items = Array<Item | FontItem>;
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<Promise<Package>>;
return reqs as Array<Promise<Parcel>>;
};
self.fetch = async () => {
await Promise.all<Package>(spawnRequests());
self.deliver = async () => {
await Promise.all<Parcel>(spawnRequests());
return result;
};

View file

@ -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));
});
});
})();

View file

@ -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));
});
});
})();