From e134e67e31f09a43fea10bac6dedaab38548d6d1 Mon Sep 17 00:00:00 2001 From: cliff Date: Thu, 23 Jan 2025 09:27:14 -0500 Subject: [PATCH 1/2] fe setup to listen to it --- src/client.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/client.ts b/src/client.ts index f83efb3..78de764 100644 --- a/src/client.ts +++ b/src/client.ts @@ -28,6 +28,15 @@ const INITIAL_WAIT_TIME = 250 const STYLE_TAG_ERROR = `Purview: expected element with ID ${STYLE_TAG_ID} to be a style tag generated by purview` +export type PurviewEvent = { + type: 'websocket:open' | 'websocket:close' | 'websocket:message' | 'websocket:error' | 'component:update'; + detail?: any; +} + +export const dispatchPurviewEvent = (event: PurviewEvent) => { + window.dispatchEvent(new CustomEvent('purview', { detail: event })); +}; + export function connectWebSocket(location: Location): WebSocket { // For more context, see comment in src/browser.ts where we wait for the DOM to load. if (document.readyState === "loading") { From 4c0ae70f79b9ccc0a8903c269a2046d82218e1db Mon Sep 17 00:00:00 2001 From: cliff Date: Thu, 23 Jan 2025 09:39:28 -0500 Subject: [PATCH 2/2] pureview events --- src/client.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/client.ts b/src/client.ts index 78de764..45f1a20 100644 --- a/src/client.ts +++ b/src/client.ts @@ -83,6 +83,7 @@ function addWebSocketHandlers(state: WebSocketState, location: Location): void { let interval: ReturnType | null = null ws.addEventListener("open", () => { + dispatchPurviewEvent({ type: 'websocket:open' }); const rootElems = Array.from(document.querySelectorAll("[data-root]")) const rootIDs = rootElems.map(elem => { return elem.getAttribute("data-component-id") as string @@ -137,11 +138,19 @@ function addWebSocketHandlers(state: WebSocketState, location: Location): void { } }) + ws.addEventListener("error", (error) => { + dispatchPurviewEvent({ + type: 'websocket:error', + detail: { error } + }); + }); + ws.addEventListener("close", () => { if (interval) { clearInterval(interval) interval = null } + dispatchPurviewEvent({ type: 'websocket:close', detail: { retries: state.numRetries } }); if (state.numRetries === MAX_RETRIES) { location.reload()