2022-09-30 12:38:12 +02:00
|
|
|
import {
|
|
|
|
builderStore,
|
|
|
|
environmentStore,
|
|
|
|
notificationStore,
|
|
|
|
} from "./stores/index.js"
|
2022-08-19 12:09:20 +02:00
|
|
|
import { get } from "svelte/store"
|
2023-05-30 12:21:37 +02:00
|
|
|
import { createWebsocket } from "@budibase/frontend-core"
|
2022-08-19 12:09:20 +02:00
|
|
|
|
2022-09-30 13:01:56 +02:00
|
|
|
let socket
|
|
|
|
|
2022-08-19 12:09:20 +02:00
|
|
|
export const initWebsocket = () => {
|
|
|
|
const { inBuilder, location } = get(builderStore)
|
2022-09-22 16:04:20 +02:00
|
|
|
const { cloud } = get(environmentStore)
|
2022-08-22 19:24:34 +02:00
|
|
|
|
|
|
|
// Only connect when we're inside the builder preview, for now
|
2022-09-30 13:01:56 +02:00
|
|
|
if (!inBuilder || !location || cloud || socket) {
|
2022-08-19 12:09:20 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-22 19:24:34 +02:00
|
|
|
// Initialise connection
|
2023-06-01 18:14:32 +02:00
|
|
|
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)
|
2022-09-30 12:38:12 +02:00
|
|
|
notificationStore.actions.info(`"${data.name}" plugin reloaded`)
|
2022-08-19 12:09:20 +02:00
|
|
|
})
|
|
|
|
}
|