|
| 1 | +import { HocuspocusProvider } from "@hocuspocus/provider"; |
| 2 | +import { useContext, useEffect, useMemo, useRef } from "react"; |
| 3 | + |
| 4 | +import { HocuspocusContext, HocuspocusRoomContext } from "./context.ts"; |
| 5 | +import type { HocuspocusRoomProps } from "./types.ts"; |
| 6 | + |
| 7 | +/** |
| 8 | + * HocuspocusRoom manages the connection to a specific document. |
| 9 | + * |
| 10 | + * It uses the shared WebSocket from HocuspocusProviderComponent and creates a document-specific |
| 11 | + * provider that connects on mount and disconnects on unmount. |
| 12 | + * |
| 13 | + * This component handles React's StrictMode gracefully by using deferred destruction, |
| 14 | + * preventing unnecessary reconnections during development double-mounts. |
| 15 | + * |
| 16 | + * @example |
| 17 | + * ```tsx |
| 18 | + * <HocuspocusProviderComponent url="ws://localhost:1234"> |
| 19 | + * <HocuspocusRoom name="document-1"> |
| 20 | + * <Editor /> |
| 21 | + * </HocuspocusRoom> |
| 22 | + * </HocuspocusProviderComponent> |
| 23 | + * ``` |
| 24 | + */ |
| 25 | +export function HocuspocusRoom({ |
| 26 | + children, |
| 27 | + name, |
| 28 | + document, |
| 29 | + token, |
| 30 | +}: HocuspocusRoomProps) { |
| 31 | + const hocuspocusContext = useContext(HocuspocusContext); |
| 32 | + |
| 33 | + if (!hocuspocusContext) { |
| 34 | + throw new Error( |
| 35 | + "HocuspocusRoom must be used within a HocuspocusProviderComponent", |
| 36 | + ); |
| 37 | + } |
| 38 | + |
| 39 | + const { websocketProvider } = hocuspocusContext; |
| 40 | + |
| 41 | + const providerRef = useRef<HocuspocusProvider | null>(null); |
| 42 | + const destroyTimeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null); |
| 43 | + |
| 44 | + // Store current props in a ref to access in cleanup without triggering re-creation |
| 45 | + const propsRef = useRef({ name, document, token }); |
| 46 | + propsRef.current = { name, document, token }; |
| 47 | + |
| 48 | + // Create or retrieve provider |
| 49 | + // We use a ref to prevent recreation on every render |
| 50 | + if (!providerRef.current) { |
| 51 | + providerRef.current = new HocuspocusProvider({ |
| 52 | + name, |
| 53 | + websocketProvider, |
| 54 | + document, |
| 55 | + token, |
| 56 | + }); |
| 57 | + } |
| 58 | + |
| 59 | + const provider = providerRef.current; |
| 60 | + |
| 61 | + useEffect(() => { |
| 62 | + // Cancel any pending destruction (handles StrictMode double-mount) |
| 63 | + if (destroyTimeoutRef.current) { |
| 64 | + clearTimeout(destroyTimeoutRef.current); |
| 65 | + destroyTimeoutRef.current = null; |
| 66 | + } |
| 67 | + |
| 68 | + return () => { |
| 69 | + // Deferred destruction - wait for potential remount in StrictMode |
| 70 | + // Using setTimeout(0) allows React to remount before we destroy |
| 71 | + destroyTimeoutRef.current = setTimeout(() => { |
| 72 | + if (providerRef.current) { |
| 73 | + providerRef.current.destroy(); |
| 74 | + providerRef.current = null; |
| 75 | + } |
| 76 | + }, 0); |
| 77 | + }; |
| 78 | + }, []); |
| 79 | + |
| 80 | + // Handle document name changes - need to recreate provider |
| 81 | + useEffect(() => { |
| 82 | + // Skip on initial mount since we already created the provider |
| 83 | + if ( |
| 84 | + providerRef.current && |
| 85 | + providerRef.current.configuration.name !== name |
| 86 | + ) { |
| 87 | + // Name changed, need to recreate provider |
| 88 | + providerRef.current.destroy(); |
| 89 | + providerRef.current = new HocuspocusProvider({ |
| 90 | + name, |
| 91 | + websocketProvider, |
| 92 | + document: propsRef.current.document, |
| 93 | + token: propsRef.current.token, |
| 94 | + }); |
| 95 | + } |
| 96 | + }, [name, websocketProvider]); |
| 97 | + |
| 98 | + const contextValue = useMemo( |
| 99 | + () => ({ |
| 100 | + provider, |
| 101 | + }), |
| 102 | + [provider], |
| 103 | + ); |
| 104 | + |
| 105 | + return ( |
| 106 | + <HocuspocusRoomContext.Provider value={contextValue}> |
| 107 | + {children} |
| 108 | + </HocuspocusRoomContext.Provider> |
| 109 | + ); |
| 110 | +} |
0 commit comments