budibase/packages/client/src/websocket.js

31 lines
765 B
JavaScript
Raw Normal View History

import {
builderStore,
environmentStore,
notificationStore,
} from "./stores/index.js"
2022-08-19 12:09:20 +02:00
import { get } from "svelte/store"
import { createWebsocket } from "@budibase/frontend-core"
2022-08-19 12:09:20 +02:00
let socket
2022-08-19 12:09:20 +02:00
export const initWebsocket = () => {
const { inBuilder, location } = get(builderStore)
const { cloud } = get(environmentStore)
2022-08-22 19:24:34 +02:00
// Only connect when we're inside the builder preview, for now
if (!inBuilder || !location || cloud || socket) {
2022-08-19 12:09:20 +02:00
return
}
2022-08-22 19:24:34 +02:00
// Initialise connection
socket = createWebsocket("/socket/client", {
heartbeat: false,
})
2022-08-22 19:24:34 +02:00
// Event handlers
2022-08-19 12:09:20 +02:00
socket.on("plugin-update", data => {
builderStore.actions.updateUsedPlugin(data.name, data.hash)
notificationStore.actions.info(`"${data.name}" plugin reloaded`)
2022-08-19 12:09:20 +02:00
})
}