Skip to content

Commit

Permalink
Fix loading YJS doc/provider (#4549)
Browse files Browse the repository at this point in the history
  • Loading branch information
ruggi authored Nov 22, 2023
1 parent e6e8fd2 commit ec0135c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
5 changes: 2 additions & 3 deletions editor/src/components/editor/editor-component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ import { generateUUID } from '../../utils/utils'
import { isLiveblocksEnabled } from './liveblocks-utils'
import type { Storage, Presence, RoomEvent, UserMeta } from '../../../liveblocks.config'
import LiveblocksProvider from '@liveblocks/yjs'
import { projectIdToRoomId } from '../../core/shared/multiplayer'
import { isRoomId, projectIdToRoomId } from '../../core/shared/multiplayer'
import { useDisplayOwnershipWarning } from './project-owner-hooks'

const liveModeToastId = 'play-mode-toast'
Expand Down Expand Up @@ -298,11 +298,10 @@ export const EditorComponentInner = React.memo((props: EditorProps) => {
)

React.useEffect(() => {
if (yDoc != null) {
if (yDoc != null && isRoomId(room.id)) {
const yProvider = new LiveblocksProvider<Presence, Storage, UserMeta, RoomEvent>(room, yDoc)

return () => {
yDoc.destroy()
yProvider.destroy()
}
}
Expand Down
8 changes: 7 additions & 1 deletion editor/src/core/shared/multiplayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,12 @@ export function canFollowTarget(
return !followChain.has(selfId)
}

const roomIdPrefix = `project-room-`

export function projectIdToRoomId(projectId: string): string {
return `project-room-${projectId}`
return `${roomIdPrefix}${projectId}`
}

export function isRoomId(s: string): boolean {
return s.startsWith(roomIdPrefix)
}

0 comments on commit ec0135c

Please sign in to comment.