From 7824b47634e11b37d31cf3c68d3df551954a9a71 Mon Sep 17 00:00:00 2001 From: Seth Trowbridge Date: Mon, 16 Oct 2023 12:33:07 -0400 Subject: [PATCH] hmr signal hook! --- hmr-listen.tsx | 9 ++++--- hmr-react.tsx | 2 -- hmr-signal.tsx | 65 ++++++++++++++++++++++++++++---------------------- 3 files changed, 43 insertions(+), 33 deletions(-) diff --git a/hmr-listen.tsx b/hmr-listen.tsx index 87b326d..cd029b3 100644 --- a/hmr-listen.tsx +++ b/hmr-listen.tsx @@ -1,5 +1,5 @@ import { HMR } from "./hmr-react.tsx"; -import { recordEntrySwap, recordIndexReset } from "./hmr-signal.tsx"; +import { GroupSignal, GroupSignalHook } from "./hmr-signal.tsx"; const FileListeners = new Map() as Mapvoid>>; export const FileListen =(inPath:string, inHandler:()=>void)=> @@ -15,13 +15,16 @@ Socket.addEventListener('message', async(event:{data:string})=> // When a file changes, dynamically re-import it to get the updated members // send the updated members to any listeners for that file - recordIndexReset(); + GroupSignal.reset(); const reImport = await import(document.location.origin+event.data+"?reload="+Math.random()); FileListeners.get(event.data)?.forEach(reExport=>reExport(reImport)); - recordEntrySwap(); + GroupSignal.swap(); + + GroupSignalHook.reset(); HMR.update(); + GroupSignalHook.reset(); }); Socket.addEventListener("error", ()=>{clearInterval(SocketTimer); console.log("HMR socket lost")}) diff --git a/hmr-react.tsx b/hmr-react.tsx index 13dae0d..9f1f6a9 100644 --- a/hmr-react.tsx +++ b/hmr-react.tsx @@ -1,6 +1,4 @@ import * as ReactParts from "react-original"; -import { Process } from "./hmr-listen.tsx"; - /* diff --git a/hmr-signal.tsx b/hmr-signal.tsx index 7b9a9ca..28703a9 100644 --- a/hmr-signal.tsx +++ b/hmr-signal.tsx @@ -1,37 +1,46 @@ import * as SignalsParts from "signals-original"; -type Entry = [signal:SignalsParts.Signal, initArg:unknown]; +type Entry = [signal:SignalsParts.Signal, initArg:T]; -let recordEntry:Entry[] = []; -let recordEntryNew:Entry[] = []; -let recordIndex = 0; -export const recordIndexReset =()=> recordIndex = 0; -export const recordEntrySwap =()=> +function ProxyGroup(inFunc:(initArg:T)=>SignalsParts.Signal) { - recordEntry = recordEntryNew; - recordEntryNew = [] as Entry[]; -}; -const proxySignal =(arg)=> -{ - const lookupOld = recordEntry[recordIndex]; - if(lookupOld && lookupOld[1] === arg) + let recordEntry:Entry[] = []; + let recordEntryNew:Entry[] = []; + let recordIndex = 0; + const reset =()=> recordIndex = 0; + const swap =()=> { - recordEntryNew[recordIndex] = lookupOld; - recordIndex++; - return lookupOld[0]; - } - else + recordEntry = recordEntryNew; + recordEntryNew = [] as Entry[]; + }; + const proxy =(arg:T)=> { - const sig = SignalsParts.signal(arg); - recordEntryNew[recordIndex] = [sig, arg]; - recordEntry[recordIndex] = [sig, arg]; - recordIndex++; - return sig; - } -}; + const lookupOld = recordEntry[recordIndex]; + if(lookupOld && lookupOld[1] === arg) + { + recordEntryNew[recordIndex] = lookupOld; + recordIndex++; + return lookupOld[0]; + } + else + { + const sig = inFunc(arg); + recordEntryNew[recordIndex] = [sig, arg]; + recordEntry[recordIndex] = [sig, arg]; + recordIndex++; + return sig; + } + }; + return {reset, swap, proxy}; +} +export const GroupSignal = ProxyGroup(SignalsParts.signal); +export const GroupSignalHook = ProxyGroup(SignalsParts.useSignal); + + +const proxySignal = GroupSignal.proxy; +const proxySignalHook = GroupSignalHook.proxy; export * from "signals-original"; -export { proxySignal as signal }; -// ? export {ProxyCreate as createElement, ProxyState as useState, ProxyReducer as useReducer }; -// ? export default {...ReactParts, createElement:ProxyCreate, useState:ProxyState, useReducer:ProxyReducer}; \ No newline at end of file +export { proxySignal as signal, proxySignalHook as useSignal }; +export default {...SignalsParts, signal:proxySignal, useSignal:proxySignalHook}; \ No newline at end of file