lbry.tech/app/client.js

72 lines
1.2 KiB
JavaScript
Raw Normal View History

2018-08-29 01:57:18 +02:00
"use strict";
2018-11-30 21:46:22 +01:00
// I M P O R T S
2018-08-29 01:57:18 +02:00
2018-08-29 18:58:55 +02:00
import async from "choo-async";
import asyncHtml from "choo-async/html";
2018-08-29 01:57:18 +02:00
import choo from "choo";
import ssr from "choo-ssr";
2018-10-10 19:56:35 +02:00
// U T I L S
2018-08-29 01:57:18 +02:00
import head from "~component/head";
import wrapper from "~component/wrapper";
2018-08-29 01:57:18 +02:00
// P R O G R A M
function main() {
2018-08-29 18:58:55 +02:00
const app = async(choo());
2018-08-29 01:57:18 +02:00
const page = view => (
shell(
ssr.head(
head,
ssr.state()
),
ssr.body(wrapper(view))
)
);
app.use(ssr());
app.route("/", page(require("./views/home")));
app.route("/api/*", page(require("./views/api")));
2019-01-25 17:36:26 +01:00
app.route("/spec", page(require("./views/spec")));
2018-08-29 01:57:18 +02:00
app.route("/*", page(require("./views/redirect")));
2018-08-29 18:58:55 +02:00
app.mount("html");
2018-08-29 01:57:18 +02:00
return app;
}
2018-08-29 18:58:55 +02:00
if (typeof window !== "undefined") main();
2018-08-29 01:57:18 +02:00
// E X P O R T
module.exports = exports = main;
// H E L P E R
2018-10-01 22:47:10 +02:00
function shell(head, body) {
2018-08-29 01:57:18 +02:00
return (state, emit) => {
2018-08-29 18:58:55 +02:00
const bodyPromise = Promise.resolve(body(state, emit));
const headPromise = bodyPromise.then(() => head(state, emit)); // resolve `head` once `body` is resolved
2018-08-29 01:57:18 +02:00
2018-08-29 18:58:55 +02:00
return asyncHtml`
2018-08-29 01:57:18 +02:00
<!DOCTYPE html>
<html lang="en">
2018-08-29 18:58:55 +02:00
${headPromise}
${bodyPromise}
2018-08-29 01:57:18 +02:00
</html>
`;
};
}