Skip to content

Commit

Permalink
computeWithCleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
soulomoon committed Apr 30, 2024
1 parent fccc789 commit 297c442
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions hls-graph/src/Development/IDE/Graph/Internal/Database.hs
Original file line number Diff line number Diff line change
Expand Up @@ -146,30 +146,33 @@ isDirty me = any (\(_,dep) -> resultBuilt me < resultChanged dep)
refreshDeps :: KeySet -> Database -> Stack -> Key -> Result -> [KeySet] -> AIO (IO Result)
refreshDeps visited db stack key result = \case
-- no more deps to refresh
[] -> pure $ compute db stack key RunDependenciesSame (Just result)
[] -> computeWithCleanup db stack key RunDependenciesSame (Just result)
(dep:deps) -> do
let newVisited = dep <> visited
res <- builder db stack (toListKeySet (dep `differenceKeySet` visited))
case res of
Left res -> if isDirty result res
-- restart the computation if any of the deps are dirty
then pure $ compute db stack key RunDependenciesChanged (Just result)
then computeWithCleanup db stack key RunDependenciesChanged (Just result)
-- else kick the rest of the deps
else refreshDeps newVisited db stack key result deps
Right iores -> do
res <- liftIO iores
if isDirty result res
then pure $ compute db stack key RunDependenciesChanged (Just result)
then computeWithCleanup db stack key RunDependenciesChanged (Just result)
else refreshDeps newVisited db stack key result deps

computeWithCleanup :: Database -> Stack -> Key -> RunMode -> Maybe Result -> AIO (IO Result)
computeWithCleanup db stack key a b = asyncWithCleanUp $ liftIO $ compute db stack key a b

-- | Refresh a key:
refresh :: Database -> Stack -> Key -> Maybe Result -> AIO (IO Result)
-- refresh _ st k _ | traceShow ("refresh", st, k) False = undefined
refresh db stack key result = case (addStack key stack, result) of
(Left e, _) -> throw e
(Right stack, Just me@Result{resultDeps = ResultDeps deps}) -> fmap join $ asyncWithCleanUp $ refreshDeps mempty db stack key me (reverse deps)
(Right stack, Just me@Result{resultDeps = ResultDeps deps}) -> refreshDeps mempty db stack key me (reverse deps)
(Right stack, _) ->
asyncWithCleanUp $ liftIO $ compute db stack key RunDependenciesChanged result
computeWithCleanup db stack key RunDependenciesChanged result

-- | Compute a key.
compute :: Database -> Stack -> Key -> RunMode -> Maybe Result -> IO Result
Expand Down

0 comments on commit 297c442

Please sign in to comment.