Commit bc9246a4 authored by Przemyslaw Kaminski's avatar Przemyslaw Kaminski

[Graph] use key for more effective graph reinitialization

Instead of tracking data via refs and reinitializing the graph, combine
graphVersion state with key derived from graph nodeId.
parent 808b3155
...@@ -75,8 +75,12 @@ appCpt = R.hooksComponent "G.C.App.app" cpt where ...@@ -75,8 +75,12 @@ appCpt = R.hooksComponent "G.C.App.app" cpt where
withSession sid $ withSession sid $
\session -> \session ->
simpleLayout $ simpleLayout $
explorerLayout { graphId, mCurrentRoute, session explorerLayout { frontends
, sessions: (fst sessions), frontends , graphId
, key: show graphId
, mCurrentRoute
, session
, sessions: (fst sessions)
, showLogin } , showLogin }
forestLayout :: Frontends -> Sessions -> AppRoute -> R2.Setter Boolean -> R.Element -> R.Element forestLayout :: Frontends -> Sessions -> AppRoute -> R2.Setter Boolean -> R.Element -> R.Element
......
...@@ -38,45 +38,63 @@ import Reactix.DOM.HTML as RH ...@@ -38,45 +38,63 @@ import Reactix.DOM.HTML as RH
type GraphId = Int type GraphId = Int
type LayoutProps = type LayoutProps =
( graphId :: GraphId ( frontends :: Frontends
, frontends :: Frontends , graphId :: GraphId
, mCurrentRoute :: AppRoute , mCurrentRoute :: AppRoute
, session :: Session , session :: Session
, sessions :: Sessions , sessions :: Sessions
, showLogin :: R.State Boolean , showLogin :: R.State Boolean
) )
type LayoutPropsWithKey =
(
key :: String
| LayoutProps
)
type LayoutPropsWithGraphVersion =
(
graphVersion :: R.State Int
| LayoutProps
)
type Props = ( type Props = (
graph :: SigmaxT.SGraph graph :: SigmaxT.SGraph
, graphVersion :: R.State Int
, mMetaData :: Maybe GET.MetaData , mMetaData :: Maybe GET.MetaData
| LayoutProps | LayoutPropsWithGraphVersion
)
type PropsWithKey =
(
key :: String
| Props
) )
-------------------------------------------------------------- --------------------------------------------------------------
explorerLayout :: Record LayoutProps -> R.Element explorerLayout :: Record LayoutPropsWithKey -> R.Element
explorerLayout props = R.createElement explorerLayoutCpt props [] explorerLayout props = R.createElement explorerLayoutCpt props []
explorerLayoutCpt :: R.Component LayoutProps explorerLayoutCpt :: R.Component LayoutPropsWithKey
explorerLayoutCpt = R.hooksComponent "G.C.GraphExplorer.explorerLayout" cpt explorerLayoutCpt = R.hooksComponent "G.C.GE.explorerLayout" cpt
where where
cpt props _ = do cpt {frontends, graphId, mCurrentRoute, session, sessions, showLogin} _ = do
graphVersion <- R.useState' 0 graphVersion <- R.useState' 0
pure $ explorerLayoutView graphVersion props pure $ explorerLayoutView {frontends, graphId, graphVersion, mCurrentRoute, session, sessions, showLogin}
explorerLayoutView :: R.State Int -> Record LayoutProps -> R.Element explorerLayoutView :: Record LayoutPropsWithGraphVersion -> R.Element
explorerLayoutView graphVersion p = R.createElement el p [] explorerLayoutView p = R.createElement el p []
where where
el = R.hooksComponent "G.C.GE.explorerLayoutView" cpt el = R.hooksComponent "G.C.GE.explorerLayoutView" cpt
cpt {frontends, graphId, mCurrentRoute, session, sessions, showLogin } _ = do cpt {frontends, graphId, graphVersion, mCurrentRoute, session, sessions, showLogin } _ = do
useLoader graphId (getNodes session graphVersion) handler useLoader {graphId, graphVersion: fst graphVersion, session} getNodes handler
where where
handler loaded = handler loaded =
explorer { frontends explorer { frontends
, graph , graph
, graphId , graphId
, graphVersion , graphVersion
, key: show $ fst graphVersion
, mCurrentRoute , mCurrentRoute
, mMetaData , mMetaData
, session , session
...@@ -85,37 +103,37 @@ explorerLayoutView graphVersion p = R.createElement el p [] ...@@ -85,37 +103,37 @@ explorerLayoutView graphVersion p = R.createElement el p []
where (Tuple mMetaData graph) = convert loaded where (Tuple mMetaData graph) = convert loaded
-------------------------------------------------------------- --------------------------------------------------------------
explorer :: Record Props -> R.Element explorer :: Record PropsWithKey -> R.Element
explorer props = R.createElement explorerCpt props [] explorer props = R.createElement explorerCpt props []
explorerCpt :: R.Component Props explorerCpt :: R.Component PropsWithKey
explorerCpt = R.hooksComponent "G.C.GraphExplorer.explorer" cpt explorerCpt = R.hooksComponent "G.C.GE.explorer" cpt
where where
cpt {frontends, graph, graphId, graphVersion, mCurrentRoute, mMetaData, session, sessions, showLogin } _ = do cpt {frontends, graph, graphId, graphVersion, mCurrentRoute, mMetaData, session, sessions, showLogin } _ = do
dataRef <- R.useRef graph
graphRef <- R.useRef null graphRef <- R.useRef null
graphVersionRef <- R.useRef (fst graphVersion)
controls <- Controls.useGraphControls graph controls <- Controls.useGraphControls graph
multiSelectEnabledRef <- R.useRef $ fst controls.multiSelectEnabled multiSelectEnabledRef <- R.useRef $ fst controls.multiSelectEnabled
R.useEffect' $ do -- NOTE This is not needed anymore, we just use 'key' in props to rerender
let readData = R.readRef dataRef -- the component
let gv = R.readRef graphVersionRef
if SigmaxT.eqGraph readData graph then -- dataRef <- R.useRef graph
pure unit -- R.useEffect' $ do
else do -- let readData = R.readRef dataRef
-- Graph data changed, reinitialize sigma. -- if SigmaxT.eqGraph readData graph then
let rSigma = R.readRef controls.sigmaRef -- pure unit
Sigmax.cleanupSigma rSigma "explorerCpt" -- else do
R.setRef dataRef graph -- -- Graph data changed, reinitialize sigma.
R.setRef graphVersionRef (fst graphVersion) -- let rSigma = R.readRef controls.sigmaRef
-- Reinitialize bunch of state as well. -- Sigmax.cleanupSigma rSigma "explorerCpt"
snd controls.removedNodeIds $ const SigmaxT.emptyNodeIds -- R.setRef dataRef graph
snd controls.selectedNodeIds $ const SigmaxT.emptyNodeIds -- -- Reinitialize bunch of state as well.
snd controls.showEdges $ const SigmaxT.EShow -- snd controls.removedNodeIds $ const SigmaxT.emptyNodeIds
snd controls.forceAtlasState $ const SigmaxT.InitialRunning -- snd controls.selectedNodeIds $ const SigmaxT.emptyNodeIds
snd controls.graphStage $ const Graph.Init -- snd controls.showEdges $ const SigmaxT.EShow
snd controls.showSidePanel $ const GET.InitialClosed -- snd controls.forceAtlasState $ const SigmaxT.InitialRunning
-- snd controls.graphStage $ const Graph.Init
-- snd controls.showSidePanel $ const GET.InitialClosed
pure $ pure $
RH.div RH.div
...@@ -280,8 +298,17 @@ modeGraphType Types.Sources = "star" ...@@ -280,8 +298,17 @@ modeGraphType Types.Sources = "star"
modeGraphType Types.Terms = "def" modeGraphType Types.Terms = "def"
getNodes :: Session -> R.State Int -> GraphId -> Aff GET.GraphData type GetNodesProps =
getNodes session (graphVersion /\ _) graphId = get session $ NodeAPI Types.Graph (Just graphId) ("?version=" <> show graphVersion) (
graphId :: GraphId
, graphVersion :: Int
, session :: Session
)
getNodes :: Record GetNodesProps -> Aff GET.GraphData
getNodes {graphId, graphVersion, session} =
get session $ NodeAPI Types.Graph (Just graphId) ("?version=" <> show graphVersion)
transformGraph :: Record Controls.Controls -> SigmaxT.SGraph -> SigmaxT.SGraph transformGraph :: Record Controls.Controls -> SigmaxT.SGraph -> SigmaxT.SGraph
......
...@@ -34,6 +34,8 @@ type Props = ( ...@@ -34,6 +34,8 @@ type Props = (
, session :: Session , session :: Session
) )
-- TODO Remove key. This requires removal of inner state derived from props in
-- corpusLayoutView
type PropsWithKey = ( type PropsWithKey = (
key :: String key :: String
| Props | Props
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment