Skip to content

Commit

Permalink
save7
Browse files Browse the repository at this point in the history
  • Loading branch information
AntoLC committed Dec 23, 2024
1 parent 56f16cc commit 14d4b6c
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ import { useRouter } from 'next/router';
import { useCallback, useEffect, useRef, useState } from 'react';
import * as Y from 'yjs';

import { useUpdateDoc } from '@/features/docs/doc-management/';
import { toBase64, useUpdateDoc } from '@/features/docs/doc-management/';
import { KEY_LIST_DOC_VERSIONS } from '@/features/docs/doc-versioning';
import { isFirefox } from '@/utils/userAgent';

import { toBase64 } from '../utils';

const useSaveDoc = (docId: string, doc: Y.Doc, canSave: boolean) => {
const { mutate: updateDoc } = useUpdateDoc({
listInvalideQueries: [KEY_LIST_DOC_VERSIONS],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,3 @@ function hslToHex(h: number, s: number, l: number) {
};
return `#${f(0)}${f(8)}${f(4)}`;
}

export const toBase64 = (str: Uint8Array) =>
Buffer.from(str).toString('base64');
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import * as Y from 'yjs';
import { useCollaborationUrl } from '@/core/config';
import { useBroadcastStore } from '@/stores';

import { toBase64 } from '../../doc-editor';
import { syncDocPolling } from '../api/syncDocPolling';
import { useProviderStore } from '../stores/useProviderStore';
import { Base64 } from '../types';
import { base64ToYDoc } from '../utils';
import { base64ToYDoc, toBase64 } from '../utils';

export const useCollaboration = (room?: string, initialContent?: Base64) => {
const collaborationUrl = useCollaborationUrl(room);
Expand All @@ -19,26 +18,26 @@ export const useCollaboration = (room?: string, initialContent?: Base64) => {
const intervalRef = useRef<NodeJS.Timeout>();

useEffect(() => {
if (!room || !collaborationUrl || provider) {
if (!room || !collaborationUrl?.wsUrl || provider) {
return;
}

console.log('Create provider');

const newProvider = createProvider(collaborationUrl, room, initialContent);
const newProvider = createProvider(
collaborationUrl.wsUrl,
room,
initialContent,
);
setBroadcastProvider(newProvider);
}, [
provider,
collaborationUrl,
collaborationUrl?.wsUrl,
room,
initialContent,
createProvider,
setBroadcastProvider,
]);

useEffect(() => {
console.log('isProviderFailure:', isProviderFailure);
console.log('intervalRef.current:', intervalRef.current);
const clearCurrentInterval = () => {
if (intervalRef.current) {
clearInterval(intervalRef.current);
Expand Down Expand Up @@ -67,14 +66,10 @@ export const useCollaboration = (room?: string, initialContent?: Base64) => {
.then((response) => {
const { yDoc64 } = response;

console.log('Poll triggered');

if (!yDoc64) {
return;
}

console.log('document updated');

const yDoc = base64ToYDoc(yDoc64);
Y.applyUpdate(provider.document, Y.encodeStateAsUpdate(yDoc));
})
Expand All @@ -84,7 +79,6 @@ export const useCollaboration = (room?: string, initialContent?: Base64) => {
}, pollingInterval);

return () => {
console.log('Clearing interval');
clearCurrentInterval();
};
}, [
Expand All @@ -96,7 +90,6 @@ export const useCollaboration = (room?: string, initialContent?: Base64) => {

useEffect(() => {
return () => {
console.log('Destroying provider');
destroyProvider();
};
}, [destroyProvider]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ import { HocuspocusProvider } from '@hocuspocus/provider';
import * as Y from 'yjs';
import { create } from 'zustand';

import { CollaborationUrl } from '@/core/config';
import { Base64 } from '@/features/docs/doc-management';
import { isFirefox } from '@/utils';

export interface UseCollaborationStore {
createProvider: (
providerUrl: CollaborationUrl,
providerUrl: string,
storeId: string,
initialDoc?: Base64,
) => HocuspocusProvider;
Expand All @@ -28,7 +26,7 @@ const defaultValues = {

export const useProviderStore = create<UseCollaborationStore>((set, get) => ({
...defaultValues,
createProvider: (providerUrl, storeId, initialDoc) => {
createProvider: (wsUrl, storeId, initialDoc) => {
const doc = new Y.Doc({
guid: storeId,
});
Expand All @@ -37,28 +35,16 @@ export const useProviderStore = create<UseCollaborationStore>((set, get) => ({
Y.applyUpdate(doc, Buffer.from(initialDoc, 'base64'));
}

console.log('navigator.userAgent', navigator.userAgent);
console.log('providerUrl', providerUrl);
const withWS = isFirefox();
//const withWS = true;
const wsUrl = withWS
? providerUrl.wsUrl //'ws://localhost:4444/collaboration/ws/?room=' + storeId
: 'ws://localhost:6666';

const provider = new HocuspocusProvider({
url: wsUrl,
name: storeId,
document: doc,
onConnect: () => {
console.log('Provider connected');
set({
failureCount: 0,
isProviderFailure: false,
});
},
onAuthenticationFailed: () => {
console.error('Authentication failed');
},
onClose: () => {
set({
failureCount: get().failureCount + 1,
Expand All @@ -68,18 +54,10 @@ export const useProviderStore = create<UseCollaborationStore>((set, get) => ({
!get().isProviderFailure &&
get().failureCount > get().maxFailureCount
) {
console.error('Max failure count reached');
set({
isProviderFailure: true,
});
}
console.log('Provider closed');
},
onDisconnect: () => {
console.log('Provider disconnected');
},
onDestroy() {
console.log('Provider destroyed');
},
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ export const currentDocRole = (abilities: Doc['abilities']): Role => {
: Role.READER;
};

export const toBase64 = (str: Uint8Array) =>
Buffer.from(str).toString('base64');

export const base64ToYDoc = (base64: string) => {
const uint8Array = Buffer.from(base64, 'base64');
const ydoc = new Y.Doc();
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/servers/y-provider/src/handlers/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from './collaborationResetConnectionsHandler';
export * from './collaborationHandler';
export * from './collaborationConnectionsHandler';
export * from './convertMarkdownHandler';

0 comments on commit 14d4b6c

Please sign in to comment.