Skip to content

Commit

Permalink
allow passing custom initialState to WSApp
Browse files Browse the repository at this point in the history
  • Loading branch information
mpscholten committed Jan 19, 2025
1 parent e930d9a commit ab42fdb
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
10 changes: 5 additions & 5 deletions IHP/ControllerSupport.hs
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ prepareRLSIfNeeded modelContext = do
Nothing -> pure modelContext

{-# INLINE startWebSocketApp #-}
startWebSocketApp :: forall webSocketApp application. (?applicationContext :: ApplicationContext, ?context :: RequestContext, InitControllerContext application, ?application :: application, Typeable application, WebSockets.WSApp webSocketApp) => IO ResponseReceived -> Network.Wai.Application
startWebSocketApp onHTTP request respond = do
startWebSocketApp :: forall webSocketApp application. (?applicationContext :: ApplicationContext, ?context :: RequestContext, InitControllerContext application, ?application :: application, Typeable application, WebSockets.WSApp webSocketApp) => webSocketApp -> IO ResponseReceived -> Network.Wai.Application
startWebSocketApp initialState onHTTP request respond = do
let ?modelContext = ?applicationContext.modelContext
requestContext <- createRequestContext ?applicationContext request respond
let ?requestContext = requestContext
Expand All @@ -156,7 +156,7 @@ startWebSocketApp onHTTP request respond = do
try (initContext @application) >>= \case
Left (exception :: SomeException) -> putStrLn $ "Unexpected exception in initContext, " <> tshow exception
Right context -> do
WebSockets.startWSApp @webSocketApp connection
WebSockets.startWSApp initialState connection

let connectionOptions = WebSockets.connectionOptions @webSocketApp

Expand All @@ -166,8 +166,8 @@ startWebSocketApp onHTTP request respond = do
Just response -> respond response
Nothing -> onHTTP
{-# INLINE startWebSocketAppAndFailOnHTTP #-}
startWebSocketAppAndFailOnHTTP :: forall webSocketApp application. (?applicationContext :: ApplicationContext, ?context :: RequestContext, InitControllerContext application, ?application :: application, Typeable application, WebSockets.WSApp webSocketApp) => Network.Wai.Application
startWebSocketAppAndFailOnHTTP = startWebSocketApp @webSocketApp @application (respond $ responseLBS HTTP.status400 [(hContentType, "text/plain")] "This endpoint is only available via a WebSocket")
startWebSocketAppAndFailOnHTTP :: forall webSocketApp application. (?applicationContext :: ApplicationContext, ?context :: RequestContext, InitControllerContext application, ?application :: application, Typeable application, WebSockets.WSApp webSocketApp) => webSocketApp -> Network.Wai.Application
startWebSocketAppAndFailOnHTTP initialState = startWebSocketApp @webSocketApp @application initialState (respond $ responseLBS HTTP.status400 [(hContentType, "text/plain")] "This endpoint is only available via a WebSocket")
where
respond = ?context.respond

Expand Down
5 changes: 3 additions & 2 deletions IHP/RouterSupport.hs
Original file line number Diff line number Diff line change
Expand Up @@ -784,7 +784,7 @@ webSocketAppWithCustomPath :: forall webSocketApp application.
webSocketAppWithCustomPath path = do
Attoparsec.char '/'
string path
pure (startWebSocketAppAndFailOnHTTP @webSocketApp)
pure (startWebSocketAppAndFailOnHTTP (WS.initialState @webSocketApp))
{-# INLINABLE webSocketAppWithCustomPath #-}

webSocketAppWithCustomPathAndHTTPFallback :: forall webSocketApp application.
Expand All @@ -800,7 +800,8 @@ webSocketAppWithCustomPathAndHTTPFallback :: forall webSocketApp application.
webSocketAppWithCustomPathAndHTTPFallback path = do
Attoparsec.char '/'
string path
pure (startWebSocketApp @webSocketApp (runActionWithNewContext (WS.initialState @webSocketApp)))
let action = WS.initialState @webSocketApp
pure (startWebSocketApp action (runActionWithNewContext action))
{-# INLINABLE webSocketAppWithCustomPathAndHTTPFallback #-}


Expand Down
6 changes: 3 additions & 3 deletions IHP/WebSocket.hs
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ class WSApp state where
connectionOptions :: WebSocket.ConnectionOptions
connectionOptions = WebSocket.defaultConnectionOptions

startWSApp :: forall state. (WSApp state, ?applicationContext :: ApplicationContext, ?requestContext :: RequestContext, ?context :: ControllerContext, ?modelContext :: ModelContext) => Websocket.Connection -> IO ()
startWSApp connection = do
state <- newIORef (initialState @state)
startWSApp :: forall state. (WSApp state, ?applicationContext :: ApplicationContext, ?requestContext :: RequestContext, ?context :: ControllerContext, ?modelContext :: ModelContext) => state -> Websocket.Connection -> IO ()
startWSApp initialState connection = do
state <- newIORef initialState
let ?state = state

result <- Exception.try ((withPingPong defaultPingPongOptions connection (\connection -> let ?connection = connection in run @state)) `Exception.finally` (let ?connection = connection in onClose @state))
Expand Down

0 comments on commit ab42fdb

Please sign in to comment.