From 866eb9b26dc51f647285bb8d8fa87de35eeb5d62 Mon Sep 17 00:00:00 2001 From: Seth Trowbridge Date: Thu, 20 Apr 2023 21:07:26 -0400 Subject: [PATCH] ssr data batches --- .vscode/launch.json | 23 +++++++++++++++++++++++ fetch.test.tsx | 12 ++++++------ lib/iso.tsx | 31 ++++++++++++++++++------------- server.tsx | 13 ++++++++++++- 4 files changed, 59 insertions(+), 20 deletions(-) create mode 100644 .vscode/launch.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..179eb6d --- /dev/null +++ b/.vscode/launch.json @@ -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 + } + ] +} \ No newline at end of file diff --git a/fetch.test.tsx b/fetch.test.tsx index 8487a95..1a16c21 100644 --- a/fetch.test.tsx +++ b/fetch.test.tsx @@ -10,13 +10,13 @@ const delay =async(inHandler:()=>void, inDelay:number):Promise=> }); }; -const cacheRecord = {Promise:delay(()=>{}, 1000)}; - -cacheRecord.Promise.then(()=>{console.log(`handler one`)}); -cacheRecord.Promise.then(()=>{console.log(`handler two`)}); - -cacheRecord.Promise = delay(()=>{}, 3000); +const queue = [1, 2, 3]; +while(queue.length) +{ + await(delay(()=>{console.log(queue.pop())}, 1000)) +} +console.log("all done!"); /* let r1, r2, r3; diff --git a/lib/iso.tsx b/lib/iso.tsx index bdd2f90..a9030bc 100644 --- a/lib/iso.tsx +++ b/lib/iso.tsx @@ -160,10 +160,12 @@ export const Switch =({children}:{children:Children})=> export const Case =({children, value}:{children:Children, value?:string, default?:true})=>null; export const useRouteVars =()=> React.useContext(SwitchContext).keys; -type FetchRecord = {URL:string, CacheFor:number, CachedAt:number, Promise?:Promise, DelaySSR:boolean, Error?:string, JSON?:object}; +type FetchRecord = {URL:string, CacheFor:number, CachedAt:number, CacheOnServer:boolean, Promise?:Promise, DelaySSR:boolean, Error?:string, JSON?:object}; + export const Fetch = { Cache:new Map() as Map, - Request(URL:string, Init?:RequestInit, CacheFor:number = 60, DelaySSR:boolean = true) + SSR:false as false|Promise[], + Request(URL:string, Init?:RequestInit, CacheFor:number = 60, CacheOnServer:boolean = true, DelaySSR:boolean = true) { let check = Fetch.Cache.get(URL); @@ -183,7 +185,7 @@ export const Fetch = { if(!check) { 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) { @@ -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]; 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(()=> { - const receipt = Fetch.Request(URL, Init, CacheFor, DelaySSR); - receipt.Promise?.then(()=>{ - ++count; - if(count > 10){return;} - console.log(`updating state`, receipt); - cacheSet([receipt.JSON, receipt.CachedAt === 0]); - }); + const receipt = Fetch.Request(URL, Init, CacheFor, CacheOnServer, DelaySSR); + receipt.Promise?.then(()=>cacheSet([receipt.JSON, receipt.CachedAt === 0])); } , []); return cacheGet; } -}; -let count = 0; \ No newline at end of file +}; \ No newline at end of file diff --git a/server.tsx b/server.tsx index 300d9c6..986c043 100644 --- a/server.tsx +++ b/server.tsx @@ -221,7 +221,18 @@ FileListen("${url.pathname}", reloadHandler);`; } else { - const results = Twind.extract(SSR(), TwindInst); + let bake = ""; + Iso.Fetch.SSR = []; + bake = SSR(); + + while(Iso.Fetch.SSR.length) + { + await Promise.all(Iso.Fetch.SSR); + Iso.Fetch.SSR = []; + bake = SSR(); + } + + const results = Twind.extract(bake, TwindInst); type = `text/html`; body = `