Files
Oxicloud/frontend/src/lib/composables/useReconnect.svelte.ts
T
Edouard Vanbelle 5083eaeaba feat(config): add server config + can disable message-bus
- server now provide it's config via /api/config (possibility to feature flag)
- client use /api/config to enable / disable some features
- capability to disable the message bus, somme OPS may not want this feature and
  consume persistent connections from server (websocket):
  OXICLOUD_MESSAGEBUS_ENABLE (true by default)
2026-09-11 12:14:50 +02:00

32 lines
1.3 KiB
TypeScript

// Svelte 5 rune wrapper around `messageBus.onReconnect`.
//
// Fires the given callback the first time the WS reconnects after a
// prior disconnect (server restart, network blip, sleep/wake).
// **Not** called on the initial connect — the caller's own load path
// is already fetching then. Bridges the "events published during the
// disconnect window are lost" gap; consumers typically pass
// `reload()` so the view catches up with the server after the outage.
//
// See `client.svelte.ts::onReconnect` for lifecycle details and
// `project_message_bus_reconnect_gap` memory for the gap it closes.
import { messageBus } from '$lib/message-bus/client.svelte';
import { serverConfig } from '$lib/stores/serverConfig.svelte';
/**
* Register `cb` as a reconnect handler for the lifetime of the
* calling component. Auto-unregisters on destroy via `$effect`
* cleanup. Passing `null`/`undefined` is a no-op — convenient for
* conditional wiring (`useReconnect(handlers.onReconnect)`).
*
* Also a no-op when the server has the message bus disabled — the
* WS never opens, so a reconnect callback can never fire.
*/
export function useReconnect(cb: (() => void) | null | undefined): void {
$effect(() => {
if (!cb || !serverConfig.features.message_bus) return;
const release = messageBus.onReconnect(cb);
return () => release();
});
}