able-baker/_lib_/react.tsx

132 lines
4.3 KiB
TypeScript
Raw Normal View History

2023-06-06 22:48:45 -04:00
import * as ReactParts from "react-original";
2023-06-15 23:08:24 -04:00
import { HMR } from "./hmr.tsx";
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
console.log("check: ui-invoked")
}
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>
const ProxyReducer =(inReducer:(inState:Storelike, inAction:string)=>Storelike, inState:Storelike)=>
{
const check = MapIndex(HMR.statesOld, HMR.statesNew.size);
const argOld = check ? check[1].state : inState;
const intercept =(inInterceptState:Storelike, inInterceptAction:string)=>
{
const capture = inReducer(inInterceptState, inInterceptAction);
const stateUser = {state:capture, set:()=>{}, reload:HMR.reloads};
HMR.statesNew.set(id, stateUser);
console.log("interepted reducer", 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};