feat: feature/mvp-sprint-1 Added reactivity to any state change independant of parent object reference value

This commit is contained in:
Nikolaj Frey 2023-08-09 12:19:57 +10:00
parent 7aecc2ff08
commit 52f604e2d4
2 changed files with 36 additions and 14 deletions

View File

@ -71,7 +71,6 @@ const initialThingtime = smarts.merge(initialValues, force)
// const localIsValid = !parsed.version || parsed.version >= force.version // const localIsValid = !parsed.version || parsed.version >= force.version
// if (localIsValid) { // if (localIsValid) {
// const newThingtime = smarts.merge(force, parsed) // const newThingtime = smarts.merge(force, parsed)
// console.log("nik comm newThingtime", newThingtime)
// thingtimeToUse = newThingtime // thingtimeToUse = newThingtime
// } else { // } else {
// const withVersionUpdates = smarts.merge(newVersionData, parsed) // const withVersionUpdates = smarts.merge(newVersionData, parsed)
@ -114,8 +113,6 @@ export const ThingtimeProvider = (props: any): JSX.Element => {
const paths = smarts.parsePropertyPath(path) const paths = smarts.parsePropertyPath(path)
console.log("nik paths", paths)
// find first parent where a path is undefined // find first parent where a path is undefined
// paths is array of path parts such as ["path1", "path2", "path3"] // paths is array of path parts such as ["path1", "path2", "path3"]
// we want to create a new reference at the first object which has an undefined part of the path // we want to create a new reference at the first object which has an undefined part of the path
@ -161,8 +158,6 @@ export const ThingtimeProvider = (props: any): JSX.Element => {
smarts.setsmart(newThingtime, path, value) smarts.setsmart(newThingtime, path, value)
console.log("nik set the newThingtime", newThingtime)
set(newThingtime) set(newThingtime)
}, },
[thingtime, set] [thingtime, set]
@ -221,7 +216,6 @@ export const ThingtimeProvider = (props: any): JSX.Element => {
try { try {
const thingtimeFromLocalStorage = window.localStorage.getItem("thingtime") const thingtimeFromLocalStorage = window.localStorage.getItem("thingtime")
console.log("nik thingtimeFromLocalStorage", thingtimeFromLocalStorage)
if (thingtimeFromLocalStorage) { if (thingtimeFromLocalStorage) {
const parsed = parse(thingtimeFromLocalStorage) const parsed = parse(thingtimeFromLocalStorage)
if (parsed) { if (parsed) {
@ -238,11 +232,9 @@ export const ThingtimeProvider = (props: any): JSX.Element => {
"nik setting new thingtime from localStorage", "nik setting new thingtime from localStorage",
newThingtime newThingtime
) )
console.log("nik localIsValid", localIsValid)
set(newThingtime) set(newThingtime)
} }
} else { } else {
console.log("nik setting initialThingtime", initialThingtime)
set(initialThingtime) set(initialThingtime)
} }
} catch (err) { } catch (err) {
@ -261,8 +253,6 @@ export const ThingtimeProvider = (props: any): JSX.Element => {
// nothing // nothing
} }
console.log("nik thingtime changed in ThingtimeProvider.tsx", thingtime)
if (stateRef.current.initialized) { if (stateRef.current.initialized) {
if (thingtime.thingtime !== thingtime || thingtime.tt !== thingtime) { if (thingtime.thingtime !== thingtime || thingtime.tt !== thingtime) {
if (!(stateRef?.current?.c >= 10)) { if (!(stateRef?.current?.c >= 10)) {

View File

@ -36,6 +36,31 @@ export const Thingtime = (props) => {
const contentEditableRef = React.useRef(null) const contentEditableRef = React.useRef(null)
const editValueRef = React.useRef({}) const editValueRef = React.useRef({})
const childrenRef = React.useRef([])
const [thingDep, setThingDep] = React.useState(childrenRef.current)
const createDependancies = () => {
// push all children into childrenRef.current
try {
const values = Object.values(props?.thing)
// if childrenRef.current does not shallow equal values then replace with array of values
const valuesNotEqual =
values?.length !== childrenRef.current?.length ||
!values?.every?.((value, idx) => {
return childrenRef.current[idx] === value
})
if (valuesNotEqual) {
childrenRef.current = values
setThingDep(childrenRef.current)
}
} catch {
// nothing
}
}
createDependancies()
const depth = React.useMemo(() => { const depth = React.useMemo(() => {
return props?.depth || 1 return props?.depth || 1
}, [props?.depth]) }, [props?.depth])
@ -55,7 +80,12 @@ export const Thingtime = (props) => {
const thing = React.useMemo(() => { const thing = React.useMemo(() => {
return props.thing return props.thing
}, [props.thing]) }, [props.thing, childrenRef.current])
React.useEffect(() => {
console.log("thingtime changed in path", props?.fullPath)
createDependancies()
}, [thingtime, props?.fullPath, childrenRef])
const fullPath = React.useMemo(() => { const fullPath = React.useMemo(() => {
return props?.fullPath || props?.path return props?.fullPath || props?.path
@ -95,7 +125,7 @@ export const Thingtime = (props) => {
} else { } else {
return [] return []
} }
}, [thing, validKeyTypes]) }, [thing, thingDep, validKeyTypes])
const type = React.useMemo(() => { const type = React.useMemo(() => {
return typeof thing return typeof thing
@ -162,7 +192,7 @@ export const Thingtime = (props) => {
} else { } else {
return "Something!" return "Something!"
} }
}, [thing, type, keys]) }, [thing, thingDep, type, keys])
const keysToUse = React.useMemo(() => { const keysToUse = React.useMemo(() => {
return keys return keys
@ -225,7 +255,6 @@ export const Thingtime = (props) => {
</Flex> </Flex>
</Safe> </Safe>
) )
console.log("nik root ret", ret)
return ret return ret
} }
} }
@ -238,6 +267,7 @@ export const Thingtime = (props) => {
fullPath, fullPath,
depth, depth,
thing, thing,
thingDep,
props, props,
valuePl, valuePl,
pl, pl,
@ -411,8 +441,10 @@ export const Thingtime = (props) => {
renderableValue, renderableValue,
pl, pl,
type, type,
AtomicWrapper,
props?.edit, props?.edit,
thing, thing,
thingDep,
updateValue, updateValue,
]) ])