@@ -26,19 +26,19 @@ import PostgresWebsockets.Broadcast
26
26
{- | Returns a multiplexer from a connection URI, keeps trying to connect in case there is any error.
27
27
This function also spawns a thread that keeps relaying the messages from the database to the multiplexer's listeners
28
28
-}
29
- newHasqlBroadcaster :: Text -> ByteString -> IO Multiplexer
30
- newHasqlBroadcaster ch = newHasqlBroadcasterForConnection . tryUntilConnected
29
+ newHasqlBroadcaster :: IO () -> Text -> ByteString -> IO Multiplexer
30
+ newHasqlBroadcaster onConnectionFailure ch = newHasqlBroadcasterForConnection . tryUntilConnected
31
31
where
32
- newHasqlBroadcasterForConnection = newHasqlBroadcasterForChannel ch
32
+ newHasqlBroadcasterForConnection = newHasqlBroadcasterForChannel onConnectionFailure ch
33
33
34
34
{- | Returns a multiplexer from a connection URI or an error message on the left case
35
35
This function also spawns a thread that keeps relaying the messages from the database to the multiplexer's listeners
36
36
-}
37
- newHasqlBroadcasterOrError :: Text -> ByteString -> IO (Either ByteString Multiplexer )
38
- newHasqlBroadcasterOrError ch =
37
+ newHasqlBroadcasterOrError :: IO () -> Text -> ByteString -> IO (Either ByteString Multiplexer )
38
+ newHasqlBroadcasterOrError onConnectionFailure ch =
39
39
acquire >=> (sequence . mapBoth show (newHasqlBroadcasterForConnection . return ))
40
40
where
41
- newHasqlBroadcasterForConnection = newHasqlBroadcasterForChannel ch
41
+ newHasqlBroadcasterForConnection = newHasqlBroadcasterForChannel onConnectionFailure ch
42
42
43
43
tryUntilConnected :: ByteString -> IO Connection
44
44
tryUntilConnected =
@@ -78,13 +78,12 @@ tryUntilConnected =
78
78
@
79
79
80
80
-}
81
- newHasqlBroadcasterForChannel :: Text -> IO Connection -> IO Multiplexer
82
- newHasqlBroadcasterForChannel ch getCon = do
83
- multi <- newMultiplexer openProducer closeProducer
81
+ newHasqlBroadcasterForChannel :: IO () -> Text -> IO Connection -> IO Multiplexer
82
+ newHasqlBroadcasterForChannel onConnectionFailure ch getCon = do
83
+ multi <- newMultiplexer openProducer $ const onConnectionFailure
84
84
void $ relayMessagesForever multi
85
85
return multi
86
86
where
87
- closeProducer _ = putErrLn " Broadcaster is dead"
88
87
toMsg :: ByteString -> ByteString -> Message
89
88
toMsg c m = case decode (toS m) of
90
89
Just v -> Message (channelDef c v) m
0 commit comments