From 68503424e6fe9d1a1d89a4424ba567e11ab1d42a Mon Sep 17 00:00:00 2001 From: Seth Trowbridge Date: Mon, 19 Jun 2023 12:17:40 -0400 Subject: [PATCH] useReducer proxy started! --- _lib_/react.tsx | 32 ++++++++++++++++++++++++++++---- example/app.tsx | 16 +++++++++++++--- 2 files changed, 41 insertions(+), 7 deletions(-) diff --git a/_lib_/react.tsx b/_lib_/react.tsx index 9232813..b8c9f8f 100644 --- a/_lib_/react.tsx +++ b/_lib_/react.tsx @@ -85,7 +85,6 @@ const ProxyState =(argNew:StateType)=> //const passedFunction = inArg; stateSet((oldState:StateType)=> { - console.log("function setter intercepted"); const output = inArg(oldState); stateUser.state = output; HMR.statesNew.set(id, stateUser); @@ -102,7 +101,32 @@ const ProxyState =(argNew:StateType)=> }; +type Storelike = Record +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]; +}; + export * from "react-original"; -export {ProxyCreate as createElement, ProxyState as useState }; -export const isProxy = true; -export default {...ReactParts, createElement:ProxyCreate, useState:ProxyState, isProxy:true}; \ No newline at end of file +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 diff --git a/example/app.tsx b/example/app.tsx index 0bfecb5..d0029d4 100644 --- a/example/app.tsx +++ b/example/app.tsx @@ -1,11 +1,10 @@ -import { VNode } from "https://esm.sh/v118/preact@10.15.1/src/index.js"; import React from "react"; const CTXString = React.createContext("lol"); type StateBinding = [get:T, set:React.StateUpdater]; const CTXState = React.createContext(null) as React.Context|null>; -const Outer =(props:{children:VNode})=> +const Outer =(props:{children:Preact.VNode})=> { const binding = React.useState(11); return @@ -19,12 +18,23 @@ const Inner =()=> }; +type Store = {name:string, age:number} +const reducer =(inState:Store, inAction:number)=> +{ + return {...inState, age:inState.age+inAction}; +} + export default ()=> { + + const [Store, Dispatch] = React.useReducer(reducer, {name:"seth", age:24} as Store) return

Title!

-

subtitle

+

subtitle!

+

+ +