UNPKG

partysocket

Version:
144 lines (137 loc) 4.33 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var _chunk2ESZ2MDZjs = require("./chunk-2ESZ2MDZ.js"); // src/use-handlers.ts var _react = require("react"); var useAttachWebSocketEventHandlers = (socket, options) => { const handlersRef = _react.useRef.call(void 0, options); handlersRef.current = options; _react.useEffect.call( void 0, () => { const onOpen = (event) => { var _a, _b; return (_b = (_a = handlersRef.current) == null ? void 0 : _a.onOpen) == null ? void 0 : _b.call(_a, event); }; const onMessage = (event) => { var _a, _b; return (_b = (_a = handlersRef.current) == null ? void 0 : _a.onMessage) == null ? void 0 : _b.call(_a, event); }; const onClose = (event) => { var _a, _b; return (_b = (_a = handlersRef.current) == null ? void 0 : _a.onClose) == null ? void 0 : _b.call(_a, event); }; const onError = (event) => { var _a, _b; return (_b = (_a = handlersRef.current) == null ? void 0 : _a.onError) == null ? void 0 : _b.call(_a, event); }; socket.addEventListener("open", onOpen); socket.addEventListener("close", onClose); socket.addEventListener("error", onError); socket.addEventListener("message", onMessage); return () => { socket.removeEventListener("open", onOpen); socket.removeEventListener("close", onClose); socket.removeEventListener("error", onError); socket.removeEventListener("message", onMessage); }; }, [socket] ); }; // src/use-socket.ts var getOptionsThatShouldCauseRestartWhenChanged = (options) => [ options.startClosed, options.minUptime, options.maxRetries, options.connectionTimeout, options.maxEnqueuedMessages, options.maxReconnectionDelay, options.minReconnectionDelay, options.reconnectionDelayGrowFactor, options.debug ]; function useStableSocket({ options, createSocket, createSocketMemoKey: createOptionsMemoKey }) { const shouldReconnect = createOptionsMemoKey(options); const socketOptions = _react.useMemo.call( void 0, () => { return options; }, [shouldReconnect] ); const [socket, setSocket] = _react.useState.call(void 0, () => // only connect on first mount createSocket({ ...socketOptions, startClosed: true }) ); const socketInitializedRef = _react.useRef.call(void 0, null); const createSocketRef = _react.useRef.call(void 0, createSocket); createSocketRef.current = createSocket; _react.useEffect.call( void 0, () => { if (socketInitializedRef.current === socket) { const newSocket = createSocketRef.current({ ...socketOptions, // when reconnecting because of options change, we always reconnect // (startClosed only applies to initial mount) startClosed: false }); setSocket(newSocket); } else { if ( !socketInitializedRef.current && socketOptions.startClosed !== true ) { socket.reconnect(); } socketInitializedRef.current = socket; return () => { socket.close(); }; } }, [socket, socketOptions] ); return socket; } // src/use-ws.ts function useWebSocket(url, protocols, options = {}) { const socket = useStableSocket({ options, createSocket: (options2) => new (0, _chunk2ESZ2MDZjs.ReconnectingWebSocket)(url, protocols, options2), createSocketMemoKey: (options2) => JSON.stringify([ // will reconnect if url or protocols are specified as a string. // if they are functions, the WebSocket will handle reconnection url, protocols, ...getOptionsThatShouldCauseRestartWhenChanged(options2) ]) }); useAttachWebSocketEventHandlers(socket, options); return socket; } exports.useAttachWebSocketEventHandlers = useAttachWebSocketEventHandlers; exports.getOptionsThatShouldCauseRestartWhenChanged = getOptionsThatShouldCauseRestartWhenChanged; exports.useStableSocket = useStableSocket; exports.useWebSocket = useWebSocket; //# sourceMappingURL=chunk-LREFFSFP.js.map