From 6e3ba4f86bf3f25d27af66cfcd4f5739e767b208 Mon Sep 17 00:00:00 2001 From: "Thomas F. K. Jorna" Date: Thu, 15 Jul 2021 22:48:14 +0200 Subject: prevent data writing every update --- app/screens/graph/graph-screen.tsx | 127 +++++++++++++++++++------------------ 1 file changed, 65 insertions(+), 62 deletions(-) (limited to 'app/screens/graph') diff --git a/app/screens/graph/graph-screen.tsx b/app/screens/graph/graph-screen.tsx index e6fbc19..da3734a 100644 --- a/app/screens/graph/graph-screen.tsx +++ b/app/screens/graph/graph-screen.tsx @@ -9,7 +9,7 @@ import { color } from "../../theme" import { Graph } from "../../components" import { Tweaks } from "../../components" -import genRandomTree from "../../data/randomdata"; +import genRandomTree from "../../data/randomdata" import AsyncStorage from "@react-native-async-storage/async-storage" @@ -24,79 +24,82 @@ export const GraphScreen = observer(function GraphScreen() { // Pull in navigation via hook // const navigation = useNavigation() - const [charge, setCharge] = useState(-30); - const [collision, setCollision] = useState(false); - const [linkStrength, setLinkStrength] = useState(1); - const [linkIts, setLinkIts] = useState(1); - - const [physics, setPhysics] = useState({}); + const [charge, setCharge] = useState(-30) + const [collision, setCollision] = useState(false) + const [linkStrength, setLinkStrength] = useState(1) + const [linkIts, setLinkIts] = useState(1) + const [physics, setPhysics] = useState({}) + const physicsInit = { + charge: -30, + collision: false, + linkStrength: 1, + linkIts: 1, + collapse: false, + threedim: false, + particles: 2, + linkOpacity: 1, + linkWidth: 1, + particleWidth: 1, + nodeRel: 1, + } const getData = async () => { try { - const value = await AsyncStorage.getItem('@physics') - if (value !== null) { - return JSON.parse(value); + const value = await AsyncStorage.getItem("@physics") + if (value !== null || keys(value) === keys(physicsInit)) { + return JSON.parse(value) } else { - return ( - { - charge: -30, - collision: false, - linkStrength: 1, - linkIts: 1, - collapse: false, - threedim: false, - particles: 2, - }); + console.log(physicsInit) + return physicsInit } } catch (e) { - console.log(e); + console.log(e) } - }; + } - useEffect(() => { - getData() - .then(data => setPhysics(data)); - }, []); + useEffect(() => { + getData().then((data) => setPhysics(data)) + }, []) - const storeData = async (value) => { - try { - const jsonValue = JSON.stringify(value); - await AsyncStorage.setItem('@physics', jsonValue); - console.log("Writing " + jsonValue); - } catch(e) { - console.log(e); - } - } + const storeData = async (value) => { + try { + const jsonValue = JSON.stringify(value) + await AsyncStorage.setItem("@physics", jsonValue) + console.log("Writing " + jsonValue) + } catch (e) { + console.log(e) + } + } /* const [physics, setPhysics] = useState( -* { -* charge: -30, -* collision: false, -* linkStrength: 1, -* linkIts: 1, -* collapse: false, -* threedim: false, -* particles: 2, -* }); */ - useEffect(() => { - console.log("Physics changed"); - storeData(physics); - const test = getData(); - console.log(test); - }, [physics]); + * { + * charge: -30, + * collision: false, + * linkStrength: 1, + * linkIts: 1, + * collapse: false, + * threedim: false, + * particles: 2, + * }); */ + useEffect(() => { + if (timer) { + clearTimeout(timer) + console.log("clear timer") + } + const timer = setTimeout(() => { + console.log("Physics changed") + storeData(physics) + const test = getData() + console.log(test) + }, 1000) + return () => clearTimeout(timer) + }, [physics]) - const gData = genRandomTree(); + const gData = genRandomTree() return ( - - - + + - ); -}); + ) +}) -- cgit v1.2.3