UNPKG

synapse-react-client

Version:

[![npm version](https://badge.fury.io/js/synapse-react-client.svg)](https://badge.fury.io/js/synapse-react-client) [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettie

169 lines (168 loc) 4.61 kB
import { useCRDTModelView as g } from "./useCRDTModelView.js"; import { useReducer as P, useRef as d, useCallback as N, useMemo as T, useEffect as u } from "react"; import { useEstablishWebsocketConnection as w } from "../../synapse-queries/grid/useEstablishWebsocketConnection.js"; import { useDocumentVisibility as _ } from "@react-hookz/web"; function A(e, n) { switch (n.type) { case "CONNECT_REQUESTED": { const s = e.connectionParams && e.connectionParams.replicaId === n.payload.replicaId && e.connectionParams.sessionId === n.payload.sessionId; return { ...e, connectionParams: { replicaId: n.payload.replicaId, sessionId: n.payload.sessionId }, hasCompletedInitialSync: s ? e.hasCompletedInitialSync : !1, model: s ? e.model : null, isConnected: !1, isConnecting: !1, connectionAttemptId: n.payload.attemptId, connectionError: null }; } case "CONNECTION_ESTABLISHED": return { ...e, websocketInstance: n.payload, isConnecting: !0 }; case "CONNECTION_OPENED": return { ...e, isConnected: !0, isConnecting: !1 }; case "CONNECTION_CLOSED": return { ...e, isConnected: !1, isConnecting: !1 }; case "GRID_READY": return { ...e, hasCompletedInitialSync: !0 }; case "MODEL_CREATED": return { ...e, model: n.payload }; case "CONNECTION_ERROR": return { ...e, connectionError: n.payload, isConnecting: !1 }; default: return e; } } const W = { model: null, hasCompletedInitialSync: !1, isConnected: !1, isConnecting: !1, connectionParams: null, websocketInstance: null, connectionAttemptId: null, connectionError: null }; function v() { const [e, n] = P(A, W), s = d(0), i = d(null), r = g(e.model), C = _(), { mutateAsync: m, isPending: p, error: a, presignedUrl: O, reset: I, clearPresignedUrl: E } = w(), f = N( (o) => { n({ type: "MODEL_CREATED", payload: o }); }, [n] ), b = T( () => ({ onGridReady: () => n({ type: "GRID_READY" }), onStatusChange: (o) => n({ type: o ? "CONNECTION_OPENED" : "CONNECTION_CLOSED" }), onModelCreate: f }), [f] ), h = N( (o, t) => { I(), (!e.connectionParams || e.connectionParams.replicaId !== o || e.connectionParams.sessionId !== t) && E(), s.current += 1; const l = s.current; n({ type: "CONNECT_REQUESTED", payload: { replicaId: o, sessionId: t, attemptId: l } }); }, [ n, e.connectionParams, I, E, s ] ); u(() => { const { connectionParams: o, connectionAttemptId: t } = e; !o || t === null || e.isConnected || e.isConnecting || p || a || e.connectionError || !C || (i.current = t, m({ replicaId: o.replicaId, sessionId: o.sessionId, websocketOptions: { ...b, model: e.model // Current model at connection time } }).then((c) => { if (i.current !== t) { c.disconnect(); return; } n({ type: "CONNECTION_ESTABLISHED", payload: c }); }).catch((c) => { n({ type: "CONNECTION_ERROR", payload: c }), console.error("Failed to establish WebSocket", c); })); }, [ e, e.connectionParams, e.connectionAttemptId, e.isConnected, e.isConnecting, p, a, C, m, b ]); const y = d(null); u(() => { const o = y.current, t = e.connectionParams; o && t && (o.replicaId !== t.replicaId || o.sessionId !== t.sessionId) && e.websocketInstance?.disconnect(), y.current = t; }, [e.connectionParams, e.websocketInstance]), u(() => () => { e.websocketInstance?.disconnect(), i.current = null; }, [e.websocketInstance]); function R(o) { if (!o || !o.api.getSnapshot() || !r) return !1; const { columnNames: t, columnOrder: c, rows: l } = r, D = t.length >= 1, S = c.length >= 1, k = l.length >= 0; return D && S && k; } return { isConnected: e.isConnected, websocketInstance: e.websocketInstance, hasCompletedInitialSync: e.hasCompletedInitialSync, model: e.model, modelSnapshot: r, connect: h, presignedUrl: O, errorEstablishingWebsocketConnection: e.connectionError ?? a, hasSufficientData: R(e.model) }; } export { W as initialWebSocketState, v as useDataGridWebSocket }; //# sourceMappingURL=useDataGridWebsocket.js.map