Skip to content

Commit 9e30abb

Browse files
committed
Use HasEventId for file persistence to make it more abstract
Signed-off-by: Sasha Bogicevic <[email protected]>
1 parent 43aeaa0 commit 9e30abb

File tree

1 file changed

+9
-15
lines changed

1 file changed

+9
-15
lines changed

hydra-node/src/Hydra/Events/FileBased.hs

+9-15
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,22 @@ import Hydra.Prelude
77

88
import Conduit (mapMC, (.|))
99
import Control.Concurrent.Class.MonadSTM (newTVarIO, writeTVar)
10-
import Hydra.Chain.ChainState (IsChainState)
11-
import Hydra.Events (EventSink (..), EventSource (..), StateEvent (..))
10+
import Hydra.Events ( EventSink (..), EventSource (..), HasEventId (..))
1211
import Hydra.Persistence (PersistenceIncremental (..))
1312

1413
-- | A basic file based event source and sink defined using an
1514
-- 'PersistenceIncremental' handle.
16-
--
17-
-- A new implementation for an 'EventSource' with a compatible 'EventSink' could
18-
-- be defined more generically with constraints:
19-
--
20-
-- (ToJSON e, FromJSON e, HasEventId) e => (EventSource e m, EventSink e m)
2115
eventPairFromPersistenceIncremental ::
22-
(IsChainState tx, MonadSTM m) =>
23-
PersistenceIncremental (StateEvent tx) m ->
24-
m (EventSource (StateEvent tx) m, EventSink (StateEvent tx) m)
16+
(ToJSON e, FromJSON e, HasEventId e, MonadSTM m) =>
17+
PersistenceIncremental e m ->
18+
m (EventSource e m, EventSink e m)
2519
eventPairFromPersistenceIncremental PersistenceIncremental{append, source} = do
2620
eventIdV <- newTVarIO Nothing
2721
let
2822
getLastSeenEventId = readTVar eventIdV
2923

30-
setLastSeenEventId StateEvent{eventId} = do
31-
writeTVar eventIdV (Just eventId)
24+
setLastSeenEventId evt = do
25+
writeTVar eventIdV (Just $ getEventId evt)
3226

3327
-- Keep track of the last seen event id when loading
3428
sourceEvents =
@@ -40,11 +34,11 @@ eventPairFromPersistenceIncremental PersistenceIncremental{append, source} = do
4034
)
4135

4236
-- Filter events that are already stored
43-
putEvent e@StateEvent{eventId} = do
37+
putEvent evt = do
4438
atomically getLastSeenEventId >>= \case
45-
Nothing -> store e
39+
Nothing -> store evt
4640
Just lastSeenEventId
47-
| eventId > lastSeenEventId -> store e
41+
| getEventId evt > lastSeenEventId -> store evt
4842
| otherwise -> pure ()
4943

5044
store e = do

0 commit comments

Comments
 (0)