forked from nicojs/typed-html
-
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5e7ffe9
commit 20a9db3
Showing
7 changed files
with
673 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<html> | ||
<body> | ||
<template id="B:1" data-gf></template> | ||
</body> | ||
</html> | ||
<script id="kita-html-suspense"> | ||
/*! Apache-2.0 https://kita.js.org */ function $KITA_RC(i) { | ||
var d = document, | ||
q = d.querySelector.bind(d), | ||
v = q('div[id="B:' + i + '"][data-sf]'), | ||
t = q('template[id="N:' + i + '"][data-sr]'), | ||
s = q('script[id="S:' + i + '"][data-ss]'), | ||
f = d.createDocumentFragment(), | ||
c, | ||
j, | ||
r; | ||
if (t && v && s) { | ||
while ((c = t.content.firstChild)) f.appendChild(c); | ||
v.parentNode.replaceChild(f, v); | ||
t.remove(); | ||
s.remove(); | ||
r = d.querySelectorAll('template[id][data-sr]'); | ||
do { | ||
j = 0; | ||
for (c = 0; c < r.length; c++) | ||
if (r[c] != t) j = $KITA_RC(r[c].id.slice(2)) ? !0 : j; | ||
} while (j); | ||
return !0; | ||
} | ||
} | ||
</script> | ||
|
||
<template id="N:1" data-gr>Slept for 0ms</template> | ||
<script id="S:1" data-gs> | ||
$KITA_RC(1); | ||
</script> | ||
|
||
<template id="N:1" data-gr>Slept for 200ms</template> | ||
<script id="S:1" data-gs> | ||
$KITA_RC(1); | ||
</script> | ||
|
||
<template id="N:1" data-gr>Slept for 400ms</template> | ||
<script id="S:1" data-gs> | ||
$KITA_RC(1); | ||
</script> | ||
|
||
<template id="N:1" data-gr>Slept for 600ms</template> | ||
<script id="S:1" data-gs> | ||
$KITA_RC(1); | ||
</script> | ||
|
||
<template id="N:1" data-gr>Slept for 800ms</template> | ||
<script id="S:1" data-gs> | ||
$KITA_RC(1); | ||
</script> | ||
|
||
<template id="N:1" data-gr>Slept for 1000ms</template> | ||
<script id="S:1" data-gs> | ||
$KITA_RC(1); | ||
</script> | ||
|
||
<template id="N:1" data-gr>Slept for 1200ms</template> | ||
<script id="S:1" data-gs> | ||
$KITA_RC(1); | ||
</script> | ||
|
||
<template id="N:1" data-gr>Slept for 1400ms</template> | ||
<script id="S:1" data-gs> | ||
$KITA_RC(1); | ||
</script> | ||
|
||
<template id="N:1" data-gr>Slept for 1600ms</template> | ||
<script id="S:1" data-gs> | ||
$KITA_RC(1); | ||
</script> | ||
|
||
<template id="N:1" data-gr>Slept for 1800ms</template> | ||
<script id="S:1" data-gs> | ||
$KITA_RC(1); | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,52 @@ | ||
import http from 'http'; | ||
import { setTimeout } from 'timers/promises'; | ||
import Html, { PropsWithChildren } from '../index'; | ||
import { Suspense, renderToStream } from '../suspense'; | ||
|
||
async function SleepForMs({ ms, children }: PropsWithChildren<{ ms: number }>) { | ||
await setTimeout(ms * 2); | ||
return Html.contentsToString([children || String(ms)]); | ||
} | ||
import Html from '../index'; | ||
import { SuspenseGenerator, pipeHtml } from '../suspense'; | ||
|
||
function renderLayout(rid: number | string) { | ||
return ( | ||
<html> | ||
<div> | ||
{Array.from({ length: 5 }, (_, i) => ( | ||
<Suspense rid={rid} fallback={<div>{i} FIuter</div>}> | ||
<div>Outer {i}!</div> | ||
|
||
<SleepForMs ms={i % 2 === 0 ? i * 2500 : i * 5000}> | ||
<Suspense rid={rid} fallback={<div>{i} FInner!</div>}> | ||
<SleepForMs ms={i * 5000}> | ||
<div>Inner {i}!</div> | ||
</SleepForMs> | ||
</Suspense> | ||
</SleepForMs> | ||
</Suspense> | ||
))} | ||
</div> | ||
<body> | ||
<SuspenseGenerator rid={rid} source={SleepGenerator()} /> | ||
</body> | ||
</html> | ||
); | ||
} | ||
|
||
async function* SleepGenerator() { | ||
for (let i = 0; i < 10; i++) { | ||
await setTimeout(i * 200); | ||
yield 'Slept for ' + i * 200 + 'ms'; | ||
} | ||
} | ||
|
||
SUSPENSE_ROOT.enabled = true; | ||
|
||
http | ||
.createServer((req, response) => { | ||
.createServer((req, rep) => { | ||
// This simple webserver only has a index.html file | ||
if (req.url !== '/' && req.url !== '/index.html') { | ||
response.end(); | ||
rep.statusCode = 404; | ||
rep.statusMessage = 'Not Found'; | ||
rep.end(); | ||
return; | ||
} | ||
|
||
// ⚠️ Charset utf8 is important to avoid old browsers utf7 xss attacks | ||
response.setHeader('Content-Type', 'text/html; charset=utf-8'); | ||
// Creates the request map | ||
const id = SUSPENSE_ROOT.requestCounter++; | ||
SUSPENSE_ROOT.requests.set(id, { | ||
stream: new WeakRef(rep), | ||
running: 0, | ||
sent: false | ||
}); | ||
|
||
// Creates the html stream | ||
const htmlStream = renderToStream(renderLayout); | ||
// ⚠️ Charset utf8 is important to avoid old browsers utf7 xss attacks | ||
rep.writeHead(200, 'OK', { 'content-type': 'text/html; charset=utf-8' }); | ||
|
||
// Pipes it into the response | ||
htmlStream.pipe(response); | ||
rep.flushHeaders; | ||
|
||
// If its an express or fastify server, just use | ||
// response.type('text/html; charset=utf-8').send(htmlStream); | ||
pipeHtml(renderLayout(id), rep, id); | ||
}) | ||
.listen(8080, () => { | ||
console.log('Listening to http://localhost:8080'); | ||
console.log('Listening to http://localhost:8080 ' + Math.random()); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.