Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[New SDK]: 'wasp/client/webSocket'. #1707

Merged
merged 1 commit into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion waspc/data/Generator/templates/react-app/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
{=/ setupFn.isDefined =}

{=# areWebSocketsUsed =}
import { WebSocketProvider } from 'wasp/webSocket/WebSocketProvider'
import { WebSocketProvider } from 'wasp/client/webSocket/WebSocketProvider'
{=/ areWebSocketsUsed =}

startApp()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import config from 'wasp/core/config'

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

// 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 =} })

function refreshAuthToken() {
function refreshAuthToken() {
// NOTE: When we figure out how `auth: true` works for Operations, we should
// mirror that behavior here for WebSockets. Ref: https://github.com/wasp-lang/wasp/issues/1133
socket.auth = {
Expand All @@ -29,11 +30,13 @@ refreshAuthToken()
apiEventsEmitter.on('sessionId.set', refreshAuthToken)
apiEventsEmitter.on('sessionId.clear', refreshAuthToken)

// PRIVATE API
export const WebSocketContext = createContext({
socket,
isConnected: false,
});

// PRIVATE API
export function WebSocketProvider({ children }: { children: JSX.Element }) {
const [isConnected, setIsConnected] = useState(socket.connected)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@ import type {
ServerToClientEvents,
} from 'wasp/server/webSocket'

// PUBLIC API
export type ServerToClientPayload<Event extends keyof ServerToClientEvents> =
Parameters<ServerToClientEvents[Event]>[0]
// PUBLIC API
export type ClientToServerPayload<Event extends keyof ClientToServerEvents> =
Parameters<ClientToServerEvents[Event]>[0]

// PUBLIC API
export function useSocket() {
return useContext(WebSocketContext)
}

// PUBLIC API
export function useSocketListener<Event extends keyof ServerToClientEvents>(
event: Event,
handler: ServerToClientEvents[Event]
Expand Down
11 changes: 6 additions & 5 deletions waspc/data/Generator/templates/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,12 @@
"./server/jobs/pgBoss/types": "./dist/server/jobs/pgBoss/types.js",
{=! Used by users, documented. =}
"./server/webSocket": "./dist/server/webSocket/index.js",
{=! Used by users, documented. =}
"./webSocket": "./dist/webSocket/index.js",
{=! Used by our code, uncodumented (but accessible) for users. =}
"./webSocket/WebSocketProvider": "./dist/webSocket/WebSocketProvider.jsx",

{=! Used by the framework client code, reconsider during refactoring. =}
"./client/webSocket/WebSocketProvider": "./dist/client/webSocket/WebSocketProvider.jsx",
{=! Still needed, reconsider during refactoring. =}
"./server/types": "./dist/server/types/index.js",
{=! Still used by the server code, reconsider during refactoring. =}
{=! Used by the framework server code, reconsider during refactoring. =}
"./server/middleware": "./dist/server/middleware/index.js",

{=! ================= NEW API HERE =================== =}
Expand All @@ -109,6 +107,9 @@
{=! Public: { api } =}
{=! Private: [sdk] =}
"./client/api": "./dist/api/index.js",
{=! Public: { type ServerToClientPayload, type ClientToServerPayload, useSocket, useSocketListener } =}
{=! Private: [] =}
"./client/webSocket": "./dist/client/webSocket/index.js",
"./auth": "./dist/auth/index.js",
"./client/auth": "./dist/client/auth/index.js",
"./server/auth": "./dist/server/auth/index.js",
Expand Down
2 changes: 1 addition & 1 deletion waspc/examples/todo-typescript/src/ChatPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
useSocket,
useSocketListener,
ServerToClientPayload,
} from 'wasp/webSocket'
} from 'wasp/client/webSocket'

async function fetchCustomRoute() {
const res = await api.get('/foo/bar')
Expand Down
4 changes: 2 additions & 2 deletions waspc/src/Wasp/Generator/SdkGenerator/WebSocketGenerator.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ genWebSockets spec
| AS.WS.areWebSocketsUsed spec =
sequence
[ genWebSocketServerIndex spec,
genFileCopy [relfile|webSocket/index.ts|],
genFileCopy [relfile|client/webSocket/index.ts|],
genWebSocketProvider spec
]
| otherwise = return []
Expand All @@ -46,7 +46,7 @@ genWebSocketServerIndex spec = return $ C.mkTmplFdWithData [relfile|server/webSo
mayebWebSocketFn = AS.App.WS.fn <$> maybeWebSocket

genWebSocketProvider :: AppSpec -> Generator FileDraft
genWebSocketProvider spec = return $ C.mkTmplFdWithData [relfile|webSocket/WebSocketProvider.tsx|] tmplData
genWebSocketProvider spec = return $ C.mkTmplFdWithData [relfile|client/webSocket/WebSocketProvider.tsx|] tmplData
where
maybeWebSocket = AS.App.webSocket $ snd $ getApp spec
shouldAutoConnect = (AS.App.WS.autoConnect <$> maybeWebSocket) /= Just (Just False)
Expand Down
Loading