Repository navigation
Expand file tree
/
Copy pathModuleCache.purs
More file actions
54 lines (46 loc) 路 1.75 KB
/
Copy pathModuleCache.purs
File metadata and controls
54 lines (46 loc) 路 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
-- | Optional, versioned storage. Eligibility keys are supplied by the caller.
module Phpurs.ModuleCache
( CacheHooks
, CacheRequest
, loadModuleState
, saveModuleState
) where
import Prelude
import Data.Maybe (Maybe(..))
import Data.Newtype (unwrap)
import Effect (Effect)
import Effect.Aff (Aff)
import Phpurs.CacheKey (Fingerprint, fingerprintString)
import Phpurs.ModuleState (Emission, ModuleState, supportsEmission)
import PureScript.Backend.Optimizer.CoreFn (ModuleName)
type CacheHooks =
{ load :: ModuleName -> Aff (Maybe ModuleState)
, store :: ModuleState -> Aff Unit
}
type CacheRequest =
{ directory :: String
, key :: Fingerprint
, moduleName :: ModuleName
, emission :: Emission
}
foreign import loadModuleStateImpl
:: (ModuleState -> Maybe ModuleState)
-> Maybe ModuleState
-> String
-> String
-> String
-> Effect (Maybe ModuleState)
foreign import saveModuleStateImpl :: String -> String -> ModuleState -> Effect Boolean
-- | Missing, incompatible or corrupt optional cache data is a miss. Validate
-- | the complete entry before allowing the driver to publish any of its state.
loadModuleState :: CacheRequest -> Effect (Maybe ModuleState)
loadModuleState request = do
result <- loadModuleStateImpl Just Nothing request.directory (fingerprintString request.key)
(unwrap request.moduleName)
pure case result of
Just state | supportsEmission request.emission state -> Just state
_ -> Nothing
-- | Atomically replace one entry. False means the optional cache could not be
-- | written; generated-output failures belong to publishModuleState instead.
saveModuleState :: String -> Fingerprint -> ModuleState -> Effect Boolean
saveModuleState directory key = saveModuleStateImpl directory (fingerprintString key)