Apply eslint

This commit is contained in:
0x1eef 2024-07-12 01:00:31 -03:00
parent 23a2b2f9e8
commit 2efc9eace2
3 changed files with 94 additions and 87 deletions

View file

@ -1,72 +1,74 @@
import type { Item, FontItem } from './postman/item';
import item from './postman/item';
import request from './postman/request';
import type { Item, FontItem } from "./postman/item";
import item from "./postman/item";
import request from "./postman/request";
type Postman = { deliver: () => Promise<Package> };
type Args = Array<Item | FontItem | Function>
interface Postman {
deliver: () => Promise<Package>;
}
type Args = Array<Item | FontItem | ((n: number) => number)>;
type Items = Array<Item | FontItem>;
type Package = {
fonts: FontFace[]
images: HTMLElement[]
css: HTMLElement[]
scripts: HTMLElement[]
json: HTMLElement[]
};
interface Package {
fonts: FontFace[];
images: HTMLElement[];
css: HTMLElement[];
scripts: HTMLElement[];
json: HTMLElement[];
}
function parseArgs(args: Args): [Items, Function] {
function parseArgs(args: Args): [Items, (n: number) => number] {
const items: Items = [];
let callback: Function = (n: number) => n
args.forEach((item) => {
if (typeof item === 'function') {
callback = item;
let progresscb = (n: number): number => n;
args.forEach(item => {
if (typeof item === "function") {
progresscb = item;
} else {
items.push(item);
}
});
return [items, callback];
return [items, progresscb];
}
export { item };
export default function (...args: Args) {
export default function (...args: Args): Postman {
const self: Postman = Object.create(null);
const result: Package = { fonts: [], images: [], css: [], scripts: [], json: [] };
const [items, callback] = parseArgs(args);
items.sort((i1, i2) => i1.priority >= i2.priority ? 1 : -1);
const [items, progresscb] = parseArgs(args);
items.sort((i1, i2) => (i1.priority >= i2.priority ? 1 : -1));
let index = 0;
const onProgress = <T>(el: T) => {
const onProgress = <T>(el: T): T => {
index++;
if (index <= items.length) {
callback(100 * (index / items.length));
progresscb(100 * (index / items.length));
}
return el;
};
const spawnRequests = () => {
const reqs = items.map((item: Item | FontItem) => {
if ('fontFamily' in item) {
const spawnRequests = (): Array<Promise<Package | null>> => {
const reqs = items.map(async (item: Item | FontItem) => {
if ("fontFamily" in item) {
const req = request.font;
return req(item)
.then((el) => onProgress<FontFace>(el))
.then((font) => result.fonts.push(font))
return await req(item)
.then(el => onProgress<FontFace>(el))
.then(font => result.fonts.push(font))
.then(() => result);
} else if(item.requestId !== 'font' && item.group !== 'fonts') {
} else if (item.requestId !== "font" && item.group !== "fonts") {
const req = request[item.requestId];
const ary = result[item.group];
return req(item)
.then((el) => onProgress<HTMLElement>(el))
.then((el) => ary.push(el))
return await req(item)
.then(el => onProgress<HTMLElement>(el))
.then(el => ary.push(el))
.then(() => result);
}
/* unreachable */
} else {
return null;
}
});
return reqs as Array<Promise<Package>>;
return reqs;
};
self.deliver = async () => {
await Promise.all<Package>(spawnRequests());
await Promise.all(spawnRequests());
return result;
};

View file

@ -1,65 +1,70 @@
type Group = 'fonts' | 'images' | 'css' | 'scripts' | 'json';
type RequestID = 'font' | 'image' | 'css' | 'script' | 'json';
type Group = "fonts" | "images" | "css" | "scripts" | "json";
type RequestID = "font" | "image" | "css" | "script" | "json";
export type Item = {
priority: number
group: Group
requestId: RequestID
href: string
props?: Partial<HTMLElement>
};
export interface Item {
priority: number;
group: Group;
requestId: RequestID;
href: string;
props?: Partial<HTMLElement>;
}
export type FontItem = {
fontFamily: string
fontFamily: string;
} & Item;
export default {
font(fontFamily: string, href: string): FontItem {
return {
priority: 1,
group: 'fonts',
requestId: 'font',
href, fontFamily,
group: "fonts",
requestId: "font",
href,
fontFamily,
};
},
image(href: string, props?: Partial<HTMLElement>): Item {
return {
priority: 2,
group: 'images',
requestId: 'image',
href, props
group: "images",
requestId: "image",
href,
props,
};
},
css(href: string, props?: Partial<HTMLElement>): Item {
return {
priority: 3,
group: 'css',
requestId: 'css',
href, props
group: "css",
requestId: "css",
href,
props,
};
},
script(href: string, props?: Partial<HTMLElement>): Item {
return {
priority: 4,
group: 'scripts',
requestId: 'script',
href, props
group: "scripts",
requestId: "script",
href,
props,
};
},
json(href: string, props?: Partial<HTMLElement>): Item {
return {
priority: 5,
group: 'json',
requestId: 'json',
href, props
}
group: "json",
requestId: "json",
href,
props,
};
},
progress(fn: (percent: number) => void): (percent: number) => void {
return fn;
}
},
};

View file

@ -1,43 +1,43 @@
import type { Item, FontItem } from './item';
import type { Item, FontItem } from "./item";
export default {
font(item: FontItem): Promise<FontFace> {
async font(item: FontItem): Promise<FontFace> {
const { fontFamily, href } = item;
return new FontFace(fontFamily, href).load();
return await new FontFace(fontFamily, href).load();
},
script(item: Item, options: RequestInit = {}): Promise<HTMLElement> {
async script(item: Item, options: RequestInit = {}): Promise<HTMLElement> {
const { href } = item;
return fetch(href, options)
.then((res) => res.text())
.then((text) => ({ type: 'application/javascript', text }))
.then((props) => Object.assign(document.createElement('script'), props));
return await fetch(href, options)
.then(async res => await res.text())
.then(text => ({ type: "application/javascript", text }))
.then(props => Object.assign(document.createElement("script"), props));
},
css(item: Item, options: RequestInit = {}): Promise<HTMLElement> {
async css(item: Item, options: RequestInit = {}): Promise<HTMLElement> {
const { href } = item;
return fetch(href, options)
.then((res) => res.text())
.then((text) => ({ innerText: text }))
.then((props) => Object.assign(document.createElement('style'), props));
return await fetch(href, options)
.then(async res => await res.text())
.then(text => ({ innerText: text }))
.then(props => Object.assign(document.createElement("style"), props));
},
image(item: Item): Promise<HTMLElement> {
async image(item: Item): Promise<HTMLElement> {
const { href } = item;
return new Promise<HTMLElement>((resolve, reject) => {
const el = document.createElement('img');
return await new Promise<HTMLElement>((resolve, reject) => {
const el = document.createElement("img");
el.onload = () => resolve(el);
el.onerror = reject;
el.src = href;
});
},
json(item: Item, options: RequestInit = {}): Promise<HTMLElement> {
async json(item: Item, options: RequestInit = {}): Promise<HTMLElement> {
const { href } = item;
return fetch(href, options)
.then((res) => res.text())
.then((text) => ({type: 'application/json', text}))
.then((props) => Object.assign(props, item.props || {}))
.then((props) => Object.assign(document.createElement('script'), props));
}
return await fetch(href, options)
.then(async res => await res.text())
.then(text => ({ type: "application/json", text }))
.then(props => Object.assign(props, item.props != null || {}))
.then(props => Object.assign(document.createElement("script"), props));
},
};