Skip to content

Commit

Permalink
Adds client config fn for websockets
Browse files Browse the repository at this point in the history
  • Loading branch information
infomiho committed Jul 23, 2024
1 parent ecf1f82 commit b72be8e
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,27 @@ import { config } from 'wasp/client'

import type { ClientToServerEvents, ServerToClientEvents } from 'wasp/server/webSocket';

export type WebSocketContextValue = {
socket: typeof socket
isConnected: boolean
}
{=# configFn.isDefined =}
{=& configFn.importStatement =}
{=/ configFn.isDefined =}

// PUBLIC API
export type WebSocketClientConfigFn = () => IoConfig

type IoConfig = Parameters<typeof io>[1]

{=# configFn.isDefined =}
const ioConfig = {= configFn.importIdentifier =}()
{=/ configFn.isDefined =}
{=^ configFn.isDefined =}
const ioConfig = {
autoConnect: {= autoConnect =},
transports: ['websocket'],
} satisfies IoConfig;
{=/ configFn.isDefined =}

// PRIVATE API
// TODO: In the future, it would be nice if users could pass more
// options to `io`, likely via some `configFn`.
export const socket: Socket<ServerToClientEvents, ClientToServerEvents> = io(config.apiUrl, { autoConnect: {= autoConnect =} })
export const socket: Socket<ServerToClientEvents, ClientToServerEvents> = io(config.apiUrl, ioConfig)

function refreshAuthToken() {
// NOTE: When we figure out how `auth: true` works for Operations, we should
Expand All @@ -35,6 +47,12 @@ refreshAuthToken()
apiEventsEmitter.on('sessionId.set', refreshAuthToken)
apiEventsEmitter.on('sessionId.clear', refreshAuthToken)

// PRIVATE API
export type WebSocketContextValue = {
socket: typeof socket
isConnected: boolean
}

// PRIVATE API
export const WebSocketContext: Context<WebSocketContextValue> = createContext<WebSocketContextValue>({
socket,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import type {
ServerToClientEvents,
} from 'wasp/server/webSocket'

// PUBLIC API
export type { WebSocketClientConfigFn } from './WebSocketProvider'

// PUBLIC API
export type ServerToClientPayload<Event extends keyof ServerToClientEvents> =
Parameters<ServerToClientEvents[Event]>[0]
Expand Down
3 changes: 2 additions & 1 deletion waspc/examples/todoApp/main.wasp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ app todoApp {
title: "ToDo App",
// head: [],
webSocket: {
fn: import { webSocketFn } from "@src/webSocket",
fn: import { webSocketFn } from "@src/webSocket/index.js",
clientConfigFn: import { getConfig } from "@src/webSocket/clientConfigFn.js",
// autoConnect: false
},
auth: {
Expand Down
5 changes: 5 additions & 0 deletions waspc/examples/todoApp/src/webSocket/clientConfigFn.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { WebSocketClientConfigFn } from 'wasp/client/webSocket'

export const getConfig: WebSocketClientConfigFn = () => ({
transports: ['polling'],
})
File renamed without changes.
1 change: 1 addition & 0 deletions waspc/src/Wasp/AppSpec/App/WebSocket.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Wasp.AppSpec.ExtImport (ExtImport)

data WebSocket = WebSocket
{ fn :: ExtImport,
clientConfigFn :: Maybe ExtImport,
autoConnect :: Maybe Bool
}
deriving (Show, Eq, Data)
5 changes: 5 additions & 0 deletions waspc/src/Wasp/Generator/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module Wasp.Generator.Common
GeneratedSrcDir,
makeJsArrayFromHaskellList,
dropExtensionFromImportPath,
makeJsBoolFromHaskellBool,
)
where

Expand Down Expand Up @@ -77,6 +78,10 @@ makeJsArrayFromHaskellList list = "[" ++ intercalate ", " listOfJsStrings ++ "]"
where
listOfJsStrings = map (\s -> "'" ++ s ++ "'") list

makeJsBoolFromHaskellBool :: Bool -> String
makeJsBoolFromHaskellBool True = "true"
makeJsBoolFromHaskellBool False = "false"

dropExtensionFromImportPath :: Path Posix (Rel r) (File f) -> Path Posix (Rel r) (File f)
dropExtensionFromImportPath = fromJust . SP.parseRelFileP . dropExtension . SP.fromRelFileP
where
Expand Down
10 changes: 7 additions & 3 deletions waspc/src/Wasp/Generator/SdkGenerator/WebSocketGenerator.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ module Wasp.Generator.SdkGenerator.WebSocketGenerator
where

import Data.Aeson (object, (.=))
import Data.Char (toLower)
import StrongPath (relfile)
import Wasp.AppSpec (AppSpec)
import qualified Wasp.AppSpec as AS
import qualified Wasp.AppSpec.App as AS.App
import qualified Wasp.AppSpec.App.Dependency as AS.Dependency
import qualified Wasp.AppSpec.App.WebSocket as AS.App.WS
import Wasp.AppSpec.Valid (getApp, isAuthEnabled)
import Wasp.Generator.Common (makeJsonWithEntityData)
import Wasp.Generator.Common (makeJsBoolFromHaskellBool, makeJsonWithEntityData)
import Wasp.Generator.FileDraft (FileDraft)
import qualified Wasp.Generator.JsImport as GJI
import Wasp.Generator.Monad (Generator)
Expand Down Expand Up @@ -50,7 +49,12 @@ genWebSocketProvider spec = return $ C.mkTmplFdWithData [relfile|client/webSocke
where
maybeWebSocket = AS.App.webSocket $ snd $ getApp spec
shouldAutoConnect = (AS.App.WS.autoConnect <$> maybeWebSocket) /= Just (Just False)
tmplData = object ["autoConnect" .= map toLower (show shouldAutoConnect)]
maybeClientConfigFn = extImportToJsImport <$> (maybeWebSocket >>= AS.App.WS.clientConfigFn)
tmplData =
object
[ "autoConnect" .= makeJsBoolFromHaskellBool shouldAutoConnect,
"configFn" .= GJI.jsImportToImportJson maybeClientConfigFn
]

depsRequiredByWebSockets :: AppSpec -> [AS.Dependency.Dependency]
depsRequiredByWebSockets spec
Expand Down

0 comments on commit b72be8e

Please sign in to comment.