ssr data batches

This commit is contained in:
Seth Trowbridge 2023-04-20 21:07:26 -04:00
parent 7847410f3e
commit 866eb9b26d
4 changed files with 59 additions and 20 deletions

23
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,23 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"request": "launch",
"name": "Launch Program",
"type": "node",
"program": "${workspaceFolder}/server.tsx",
"cwd": "${workspaceFolder}",
"runtimeExecutable": "C:\\Users\\Seth\\.deno\\bin\\deno.EXE",
"runtimeArgs": [
"run",
"--unstable",
"--inspect-wait",
"--allow-all"
],
"attachSimplePort": 9229
}
]
}

View File

@ -10,13 +10,13 @@ const delay =async(inHandler:()=>void, inDelay:number):Promise<void>=>
}); });
}; };
const cacheRecord = {Promise:delay(()=>{}, 1000)}; const queue = [1, 2, 3];
while(queue.length)
cacheRecord.Promise.then(()=>{console.log(`handler one`)}); {
cacheRecord.Promise.then(()=>{console.log(`handler two`)}); await(delay(()=>{console.log(queue.pop())}, 1000))
}
cacheRecord.Promise = delay(()=>{}, 3000);
console.log("all done!");
/* /*
let r1, r2, r3; let r1, r2, r3;

View File

@ -160,10 +160,12 @@ export const Switch =({children}:{children:Children})=>
export const Case =({children, value}:{children:Children, value?:string, default?:true})=>null; export const Case =({children, value}:{children:Children, value?:string, default?:true})=>null;
export const useRouteVars =()=> React.useContext(SwitchContext).keys; export const useRouteVars =()=> React.useContext(SwitchContext).keys;
type FetchRecord = {URL:string, CacheFor:number, CachedAt:number, Promise?:Promise<FetchRecord>, DelaySSR:boolean, Error?:string, JSON?:object}; type FetchRecord = {URL:string, CacheFor:number, CachedAt:number, CacheOnServer:boolean, Promise?:Promise<FetchRecord>, DelaySSR:boolean, Error?:string, JSON?:object};
export const Fetch = { export const Fetch = {
Cache:new Map() as Map<string, FetchRecord>, Cache:new Map() as Map<string, FetchRecord>,
Request(URL:string, Init?:RequestInit, CacheFor:number = 60, DelaySSR:boolean = true) SSR:false as false|Promise<FetchRecord>[],
Request(URL:string, Init?:RequestInit, CacheFor:number = 60, CacheOnServer:boolean = true, DelaySSR:boolean = true)
{ {
let check = Fetch.Cache.get(URL); let check = Fetch.Cache.get(URL);
@ -183,7 +185,7 @@ export const Fetch = {
if(!check) if(!check)
{ {
console.log(`making new cache record...`); console.log(`making new cache record...`);
return load({URL, CacheFor, CachedAt:0, DelaySSR}); return load({URL, CacheFor, CachedAt:0, CacheOnServer, DelaySSR});
} }
else if(check.CachedAt == 0) else if(check.CachedAt == 0)
{ {
@ -207,23 +209,26 @@ export const Fetch = {
} }
}, },
Use(URL:string, Init?:RequestInit, CacheFor:number = 60, DelaySSR:boolean = true) Use(URL:string, Init?:RequestInit, CacheFor:number = 60, CacheOnServer:boolean = true, DelaySSR:boolean = true)
{ {
type FetchHookState = [Data:undefined|object, Updating:boolean]; type FetchHookState = [Data:undefined|object, Updating:boolean];
const [cacheGet, cacheSet] = React.useState([undefined, true] as FetchHookState); const [cacheGet, cacheSet] = React.useState([undefined, true] as FetchHookState);
if(Fetch.SSR && DelaySSR)
{
const receipt = Fetch.Request(URL, Init, CacheFor, CacheOnServer, DelaySSR);
if(receipt.CachedAt === 0 && receipt.Promise)
{
Fetch.SSR.push(receipt.Promise);
}
return [receipt.JSON, receipt.CachedAt === 0];
}
React.useEffect(()=> React.useEffect(()=>
{ {
const receipt = Fetch.Request(URL, Init, CacheFor, DelaySSR); const receipt = Fetch.Request(URL, Init, CacheFor, CacheOnServer, DelaySSR);
receipt.Promise?.then(()=>{ receipt.Promise?.then(()=>cacheSet([receipt.JSON, receipt.CachedAt === 0]));
++count;
if(count > 10){return;}
console.log(`updating state`, receipt);
cacheSet([receipt.JSON, receipt.CachedAt === 0]);
});
} }
, []); , []);
return cacheGet; return cacheGet;
} }
}; };
let count = 0;

View File

@ -221,7 +221,18 @@ FileListen("${url.pathname}", reloadHandler);`;
} }
else else
{ {
const results = Twind.extract(SSR(<Iso.Router.Provider url={url}><App/></Iso.Router.Provider>), TwindInst); let bake = "";
Iso.Fetch.SSR = [];
bake = SSR(<Iso.Router.Provider url={url}><App/></Iso.Router.Provider>);
while(Iso.Fetch.SSR.length)
{
await Promise.all(Iso.Fetch.SSR);
Iso.Fetch.SSR = [];
bake = SSR(<Iso.Router.Provider url={url}><App/></Iso.Router.Provider>);
}
const results = Twind.extract(bake, TwindInst);
type = `text/html`; type = `text/html`;
body = `<!doctype html> body = `<!doctype html>