refac: remove async
This commit is contained in:
parent
f577128923
commit
cc9a67d055
7 changed files with 48 additions and 51 deletions
17
lib/nodes.d.ts
vendored
17
lib/nodes.d.ts
vendored
|
@ -1,9 +1,8 @@
|
|||
import { Nil, Nilable } from "./lang.js";
|
||||
export declare type AnyNode = AnySyncNode | AnyAsyncNode;
|
||||
export declare type AnyAsyncNode = Promise<AnySyncNode>;
|
||||
export declare type AnySyncNode = TextNode | Elem | Frag | Nil | false;
|
||||
export declare class TextNode extends String {
|
||||
}
|
||||
export declare type AnyNode = TextNode | Elem | Frag | Nil | false;
|
||||
|
||||
export declare class TextNode extends String {}
|
||||
|
||||
export declare function F(...children: AnyNode[]): Frag;
|
||||
export declare class Frag {
|
||||
#private;
|
||||
|
@ -15,7 +14,11 @@ export declare class Frag {
|
|||
withChild(node: AnyNode): this;
|
||||
addChild(node: AnyNode): void;
|
||||
}
|
||||
export declare function E(tagName: string, attrs: ElemAttrs, ...children: AnyNode[]): Elem;
|
||||
export declare function E(
|
||||
tagName: string,
|
||||
attrs: ElemAttrs,
|
||||
...children: AnyNode[]
|
||||
): Elem;
|
||||
export declare type ElemAttrs = Record<string, unknown>;
|
||||
export declare class Elem extends Frag {
|
||||
#private;
|
||||
|
@ -25,5 +28,5 @@ export declare class Elem extends Frag {
|
|||
withAttrs(attrs: Record<string, unknown>): Elem;
|
||||
withAttr(name: string, value: unknown): Elem;
|
||||
addAttr(name: string, value: unknown): void;
|
||||
addChild(node: AnySyncNode): void;
|
||||
addChild(node: AnyNode): void;
|
||||
}
|
||||
|
|
2
lib/str.d.ts
vendored
2
lib/str.d.ts
vendored
|
@ -1,5 +1,5 @@
|
|||
import { Renderer } from "./types.js";
|
||||
import { Elem } from "./nodes.js";
|
||||
export declare class StrRenderer implements Renderer<string> {
|
||||
render(node: Elem | Promise<Elem>): Promise<string>;
|
||||
render(node: Elem): string;
|
||||
}
|
||||
|
|
19
lib/str.mjs
19
lib/str.mjs
|
@ -1,25 +1,24 @@
|
|||
import { isBool, isNil } from "./lang.mjs";
|
||||
import { Elem, TextNode } from "./nodes.mjs";
|
||||
export class StrRenderer {
|
||||
async render(node) {
|
||||
return encodeNode(await node);
|
||||
render(node) {
|
||||
return encodeNode(node);
|
||||
}
|
||||
}
|
||||
async function encodeAnyNode(node) {
|
||||
const syncNode = await node;
|
||||
return !syncNode
|
||||
function encodeAnyNode(node) {
|
||||
return !node
|
||||
? null
|
||||
: syncNode instanceof TextNode
|
||||
? encodeTextNode(syncNode)
|
||||
: encodeNode(syncNode);
|
||||
: node instanceof TextNode
|
||||
? encodeTextNode(node)
|
||||
: encodeNode(node);
|
||||
}
|
||||
function encodeTextNode(node) {
|
||||
return String(node);
|
||||
}
|
||||
async function encodeNode(node) {
|
||||
function encodeNode(node) {
|
||||
const encodedChildren = isNil(node.children)
|
||||
? undefined
|
||||
: await Promise.all(node.children.map(encodeAnyNode)).then((children) => children.filter((c) => Boolean(c)));
|
||||
: node.children.map(encodeAnyNode).filter((c) => Boolean(c));
|
||||
return node instanceof Elem
|
||||
? encodeHtmlElement(node.tagName, node.attrs, encodedChildren)
|
||||
: encodeHtmlFragment(encodedChildren);
|
||||
|
|
2
lib/types.d.ts
vendored
2
lib/types.d.ts
vendored
|
@ -1,4 +1,4 @@
|
|||
import { Elem } from "./nodes.js";
|
||||
export interface Renderer<T> {
|
||||
render(node: Elem | Promise<Elem>): Promise<T>;
|
||||
render(node: Elem): T;
|
||||
}
|
||||
|
|
|
@ -1,8 +1,6 @@
|
|||
import { isNil, Nil, Nilable } from "./lang.mjs";
|
||||
|
||||
export type AnyNode = AnySyncNode | AnyAsyncNode;
|
||||
export type AnyAsyncNode = Promise<AnySyncNode>;
|
||||
export type AnySyncNode = TextNode | Elem | Frag | Nil | false;
|
||||
export type AnyNode = TextNode | Elem | Frag | Nil | false;
|
||||
|
||||
export class TextNode extends String {}
|
||||
|
||||
|
@ -90,7 +88,7 @@ export class Elem extends Frag {
|
|||
this.#attrs[name] = value;
|
||||
}
|
||||
|
||||
addChild(node: AnySyncNode): void {
|
||||
addChild(node: AnyNode): void {
|
||||
if (this.#isSelfClosed)
|
||||
throw new Error("You cannot add child to self closed element");
|
||||
super.addChild(node);
|
||||
|
|
21
src/str.mts
21
src/str.mts
|
@ -3,30 +3,27 @@ import { isBool, isNil, Nullable } from "./lang.mjs";
|
|||
import { AnyNode, Elem, Frag, TextNode } from "./nodes.mjs";
|
||||
|
||||
export class StrRenderer implements Renderer<string> {
|
||||
async render(node: Elem | Promise<Elem>): Promise<string> {
|
||||
return encodeNode(await node);
|
||||
render(node: Elem): string {
|
||||
return encodeNode(node);
|
||||
}
|
||||
}
|
||||
|
||||
async function encodeAnyNode(node: AnyNode): Promise<Nullable<string>> {
|
||||
const syncNode = await node;
|
||||
return !syncNode
|
||||
function encodeAnyNode(node: AnyNode): Nullable<string> {
|
||||
return !node
|
||||
? null
|
||||
: syncNode instanceof TextNode
|
||||
? encodeTextNode(syncNode)
|
||||
: encodeNode(syncNode);
|
||||
: node instanceof TextNode
|
||||
? encodeTextNode(node)
|
||||
: encodeNode(node);
|
||||
}
|
||||
|
||||
function encodeTextNode(node: TextNode): string {
|
||||
return String(node);
|
||||
}
|
||||
|
||||
async function encodeNode(node: Elem | Frag): Promise<string> {
|
||||
function encodeNode(node: Elem | Frag): string {
|
||||
const encodedChildren = isNil(node.children)
|
||||
? undefined
|
||||
: await Promise.all(node.children.map(encodeAnyNode)).then((children) =>
|
||||
children.filter((c): c is string => Boolean(c))
|
||||
);
|
||||
: node.children.map(encodeAnyNode).filter((c): c is string => Boolean(c));
|
||||
return node instanceof Elem
|
||||
? encodeHtmlElement(node.tagName, node.attrs, encodedChildren)
|
||||
: encodeHtmlFragment(encodedChildren);
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import { Elem } from "./nodes.mjs";
|
||||
|
||||
export interface Renderer<T> {
|
||||
render(node: Elem | Promise<Elem>): Promise<T>;
|
||||
render(node: Elem): T;
|
||||
}
|
||||
|
|
Reference in a new issue