DenReg/registry/src/renderer.tsx

40 lines
1005 B
TypeScript
Raw Normal View History

2020-07-29 18:47:36 +00:00
/// <reference path="./types/jsx.d.ts" />
import { React, jsx } from "./deps.ts";
2020-07-31 22:03:23 +00:00
import config from "./config.ts";
2020-07-29 18:47:36 +00:00
class StringReader implements Deno.Reader {
private data: Uint8Array;
private offset = 0;
constructor(text: string) {
this.data = new TextEncoder().encode(text);
}
async read(p: Uint8Array): Promise<number | null> {
if (this.offset >= this.data.byteLength) return null;
const forLength = Math.min(p.length, this.data.length - this.offset);
for (let i = 0; i < forLength; i++) {
p[i] = this.data[i + this.offset];
}
this.offset += forLength;
return forLength;
}
}
export default async function render(
name: string,
data: any
): Promise<Deno.Reader> {
const component: {
default: () => JSX.IntrinsicElements | Promise<JSX.IntrinsicElements>;
2020-07-31 22:03:23 +00:00
} = await import(`./views/${name}.tsx`);
2020-07-29 18:47:36 +00:00
const res = await (<component.default {...data} />).render();
2020-07-29 18:47:36 +00:00
return new StringReader(res as string);
}