fix: store rooms in map

This commit is contained in:
Akhilesh Rangani 2024-07-15 14:56:37 -04:00
parent c262fb2a31
commit 6f6926a621

View File

@ -105,6 +105,16 @@ export default function CodeEditor({
const [provider, setProvider] = useState<TypedLiveblocksProvider>() const [provider, setProvider] = useState<TypedLiveblocksProvider>()
const userInfo = useSelf((me) => me.info) const userInfo = useSelf((me) => me.info)
// Liveblocks providers map to prevent reinitializing providers
type ProviderData = {
provider: LiveblocksProvider<never, never, never, never>;
yDoc: Y.Doc;
yText: Y.Text;
binding?: MonacoBinding;
onSync: (isSynced: boolean) => void;
};
const providersMap = useRef(new Map<string, ProviderData>());
// Refs for libraries / features // Refs for libraries / features
const editorContainerRef = useRef<HTMLDivElement>(null) const editorContainerRef = useRef<HTMLDivElement>(null)
const monacoRef = useRef<typeof monaco | null>(null) const monacoRef = useRef<typeof monaco | null>(null)
@ -332,10 +342,15 @@ export default function CodeEditor({
if (!editorRef || !tab || !model) return if (!editorRef || !tab || !model) return
const yDoc = new Y.Doc() let providerData: ProviderData;
const yText = yDoc.getText(tab.id)
const yProvider: any = new LiveblocksProvider(room, yDoc)
// When a file is opened for the first time, create a new provider and store in providersMap.
if (!providersMap.current.has(tab.id)) {
const yDoc = new Y.Doc();
const yText = yDoc.getText(tab.id);
const yProvider = new LiveblocksProvider(room, yDoc);
// Inserts the file content into the editor once when the tab is changed.
const onSync = (isSynced: boolean) => { const onSync = (isSynced: boolean) => {
if (isSynced) { if (isSynced) {
const text = yText.toString() const text = yText.toString()
@ -353,22 +368,51 @@ export default function CodeEditor({
yProvider.on("sync", onSync) yProvider.on("sync", onSync)
setProvider(yProvider) // Save the provider to the map.
providerData = { provider: yProvider, yDoc, yText, onSync };
providersMap.current.set(tab.id, providerData);
} else {
// When a tab is opened that has been open before, reuse the existing provider.
providerData = providersMap.current.get(tab.id)!;
}
const binding = new MonacoBinding( const binding = new MonacoBinding(
yText, providerData.yText,
model, model,
new Set([editorRef]), new Set([editorRef]),
yProvider.awareness as Awareness providerData.provider.awareness as unknown as Awareness
) );
providerData.binding = binding;
setProvider(providerData.provider);
return () => { return () => {
yDoc.destroy() // Cleanup logic
yProvider.destroy() if (binding) {
binding.destroy() binding.destroy();
yProvider.off("sync", onSync)
} }
}, [editorRef, room, activeFileContent]) if (providerData.binding) {
providerData.binding = undefined;
}
};
}, [editorRef, room, activeFileContent, activeFileId, tabs]);
// Added this effect to clean up when the component unmounts
useEffect(() => {
return () => {
// Clean up all providers when the component unmounts
providersMap.current.forEach((data) => {
if (data.binding) {
data.binding.destroy();
}
data.provider.disconnect();
data.yDoc.destroy();
});
providersMap.current.clear();
};
}, []);
// Connection/disconnection effect // Connection/disconnection effect
useEffect(() => { useEffect(() => {