hmr signal hook!

This commit is contained in:
Seth Trowbridge 2023-10-16 12:33:07 -04:00
parent b0f87d20c5
commit 7824b47634
3 changed files with 43 additions and 33 deletions

View File

@ -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 Map<string, Array<(module:unknown)=>void>>;
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")})

View File

@ -1,6 +1,4 @@
import * as ReactParts from "react-original";
import { Process } from "./hmr-listen.tsx";
/*

View File

@ -1,18 +1,20 @@
import * as SignalsParts from "signals-original";
type Entry = [signal:SignalsParts.Signal<unknown>, initArg:unknown];
type Entry<T> = [signal:SignalsParts.Signal<T>, initArg:T];
let recordEntry:Entry[] = [];
let recordEntryNew:Entry[] = [];
let recordIndex = 0;
export const recordIndexReset =()=> recordIndex = 0;
export const recordEntrySwap =()=>
function ProxyGroup<T>(inFunc:(initArg:T)=>SignalsParts.Signal<T>)
{
let recordEntry:Entry<T>[] = [];
let recordEntryNew:Entry<T>[] = [];
let recordIndex = 0;
const reset =()=> recordIndex = 0;
const swap =()=>
{
recordEntry = recordEntryNew;
recordEntryNew = [] as Entry[];
};
const proxySignal =(arg)=>
{
recordEntryNew = [] as Entry<T>[];
};
const proxy =(arg:T)=>
{
const lookupOld = recordEntry[recordIndex];
if(lookupOld && lookupOld[1] === arg)
{
@ -22,16 +24,23 @@ const proxySignal =(arg)=>
}
else
{
const sig = SignalsParts.signal(arg);
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};
export { proxySignal as signal, proxySignalHook as useSignal };
export default {...SignalsParts, signal:proxySignal, useSignal:proxySignalHook};