able-baker/hmr-react.tsx

178 lines
5.9 KiB
TypeScript
Raw Normal View History

2023-06-06 22:48:45 -04:00
import * as ReactParts from "react-original";
2023-10-14 12:09:06 -04:00
import { Process } from "./hmr-listen.tsx";
/*
Each custom component is secretly modified to have an extra state and id.
When there is an HMR update, this state is changed, forcing it to re-render.
Each *user-created* React.useState is secretly modified and accompanied by an ID.
Every time its state is set, the HMR.statesNew map for this ID is set to contain the new state and updater.
When a component is removed, any of it's states in HMR.statesNew are also removed.
(HMR.statesNew is the "running total" of all states currently at play).
---
When a state is interacted with:
- statesNew for this id is set
- the internal state is also set in the traditional way
When there is an HMR update:
- All custom components are re-rendered...
for each useState(value) call that then happens in the re-render:
- if there is a "statesOld" value for this state, use that and ignore the passed value, otherwise use the passed value
- if this state has not been interacted with since the last reload (statesNew is empty at this id), set statesNew<id> with whatever is in statesOld<id>
- statesNew is moved into *statesOld*
- statesNew is cleared.
*/
2023-10-16 10:27:22 -04:00
export const HMR =
2023-10-14 12:09:06 -04:00
{
reloads:1,
RegisteredComponents: new Map() as Map<string, ()=>void>,
statesNew: new Map() as Map<string, StateCapture>,
statesOld: new Map() as Map<string, StateCapture>,
wireframe: false,
RegisterComponent(reactID:string, value:()=>void):void
{
this.RegisteredComponents.set(reactID, value);
},
update()
{
this.reloads++;
this.RegisteredComponents.forEach(handler=>handler());
this.RegisteredComponents.clear();
this.statesOld = this.statesNew;
this.statesNew = new Map();
}
};
2023-06-15 23:08:24 -04:00
export type StateType = boolean|number|string|Record<string, string>
export type StateCapture = {state:StateType, set:ReactParts.StateUpdater<StateType>, reload:number};
2023-06-16 14:59:27 -04:00
type FuncArgs = [element:keyof ReactParts.JSX.IntrinsicElements, props:Record<string, string>, children:ReactParts.JSX.Element[]];
2023-06-15 23:08:24 -04:00
2023-06-18 14:37:13 -04:00
2023-06-16 14:59:27 -04:00
const H = ReactParts.createElement;
2023-06-18 07:36:55 -04:00
const MapIndex =(inMap:Map<string, StateCapture>, inIndex:number)=>
2023-06-15 23:08:24 -04:00
{
let index = 0;
for(const kvp of inMap)
{
if(index == inIndex)
{
return kvp;
}
index++;
}
return false;
};
2023-06-06 22:48:45 -04:00
2023-06-18 14:37:13 -04:00
const ProxyCreate =(...args:FuncArgs)=> (typeof args[0] == "string") ? H(...args) : H(ProxyElement, {__args:args, ...args[1]});
2023-06-17 14:48:39 -04:00
2023-06-16 14:59:27 -04:00
const ProxyElement = (props:{__args:FuncArgs})=>
2023-06-06 22:48:45 -04:00
{
const [stateGet, stateSet] = ReactParts.useState(0);
2023-06-17 14:48:39 -04:00
const id = ReactParts.useId();
HMR.RegisterComponent(id, ()=>stateSet(stateGet+1));
2023-06-06 22:48:45 -04:00
const child = H(...props.__args);
if(HMR.wireframe)
{
return H("div", {style:{padding:"10px", border:"2px solid red"}},
H("p", null, stateGet),
child
);
}
else
{
2023-06-17 14:48:39 -04:00
return child;
2023-06-06 22:48:45 -04:00
}
};
2023-06-18 14:37:13 -04:00
const ProxyState =(argNew:StateType)=>
2023-06-06 22:48:45 -04:00
{
// does statesOld have an entry for this state? use that instead of the passed arg
2023-06-18 07:36:55 -04:00
const check = MapIndex(HMR.statesOld, HMR.statesNew.size);
2023-06-18 14:37:13 -04:00
const argOld = check ? check[1].state : argNew;
2023-06-06 22:48:45 -04:00
2023-06-18 14:37:13 -04:00
const id = ReactParts.useId();
2023-06-18 07:36:55 -04:00
const [stateGet, stateSet] = ReactParts.useState(argOld);
2023-06-18 14:37:13 -04:00
// state updates due to clicks, interactivity, etc. since the last reload may already be in statesNew for this slot.
// DONT overwrite it.
if(!HMR.statesNew.get(id))
{
HMR.statesNew.set(id, {state:stateGet, set:stateSet, reload:HMR.reloads});
}
2023-06-18 07:36:55 -04:00
2023-06-18 14:37:13 -04:00
const lastKnowReloads = HMR.reloads;
ReactParts.useEffect(()=>{
2023-06-06 22:48:45 -04:00
return ()=>{
2023-06-18 14:37:13 -04:00
if(HMR.reloads == lastKnowReloads)/*i have no idea what this does. this may have to be re-introduced when routing is added*/
2023-06-06 22:48:45 -04:00
{
// this is a switch/ui change, not a HMR reload change
2023-06-18 07:36:55 -04:00
const oldState = MapIndex(HMR.statesOld, HMR.statesNew.size-1);
2023-06-18 14:37:13 -04:00
oldState && HMR.statesOld.set(oldState[0], {...oldState[1], state:argNew});
2023-06-16 14:59:27 -04:00
}
2023-06-18 14:37:13 -04:00
2023-06-16 14:59:27 -04:00
HMR.statesNew.delete(id);
2023-06-06 22:48:45 -04:00
}
}, []);
2023-06-18 14:37:13 -04:00
// do we need to account for the function set?
2023-06-19 09:20:16 -04:00
function proxySetter ( inArg:StateType|((old:StateType)=>StateType) )
{
const stateUser = {state:inArg as StateType, set:stateSet, reload:HMR.reloads};
if(typeof inArg == "function")
{
//const passedFunction = inArg;
stateSet((oldState:StateType)=>
{
const output = inArg(oldState);
stateUser.state = output;
HMR.statesNew.set(id, stateUser);
return output;
});
}
else
{
HMR.statesNew.set(id, stateUser);
stateSet(inArg);
}
2023-06-06 22:48:45 -04:00
}
return [stateGet, proxySetter];
};
2023-06-19 12:17:40 -04:00
type Storelike = Record<string, string>
2023-06-19 13:31:22 -04:00
const ProxyReducer =(inReducer:(inState:Storelike, inAction:string)=>Storelike, inState:Storelike, inInit?:(inState:Storelike)=>Storelike)=>
2023-06-19 12:17:40 -04:00
{
const check = MapIndex(HMR.statesOld, HMR.statesNew.size);
2023-06-19 13:31:22 -04:00
const argOld = check ? check[1].state : (inInit ? inInit(inState) : inState);
2023-06-19 12:17:40 -04:00
const intercept =(inInterceptState:Storelike, inInterceptAction:string)=>
{
const capture = inReducer(inInterceptState, inInterceptAction);
const stateUser = {state:capture, set:()=>{}, reload:HMR.reloads};
HMR.statesNew.set(id, stateUser);
return capture;
};
const id = ReactParts.useId();
const [state, dispatch] = ReactParts.useReducer(intercept, argOld as Storelike);
if(!HMR.statesNew.get(id))
{
HMR.statesNew.set(id, {state:state, set:()=>{}, reload:HMR.reloads});
}
return [state, dispatch];
};
2023-06-06 22:48:45 -04:00
export * from "react-original";
2023-06-19 12:17:40 -04:00
export {ProxyCreate as createElement, ProxyState as useState, ProxyReducer as useReducer };
export default {...ReactParts, createElement:ProxyCreate, useState:ProxyState, useReducer:ProxyReducer};