lbry.tech/app/client.js

75 lines
1.3 KiB
JavaScript
Raw Normal View History

2018-08-29 01:57:18 +02:00
"use strict";
// P A C K A G E S
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 devtools from "choo-devtools";
2018-10-01 22:47:10 +02:00
import { require as local } from "app-root-path";
2018-08-29 01:57:18 +02:00
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
2018-10-10 23:15:43 +02:00
const head = local("/app/components/head").default;
const wrapper = local("/app/components/wrapper").default;
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
if (process.env.NODE_ENV !== "production") app.use(devtools());
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")));
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>
`;
};
}