refactor: reimplement realtime-communication

This commit refactors a lot of things that are not easy to separate.
It replaces the binary protocol of y-protocols with json.
It introduces event based message processing.
It implements our own code mirror plugins for synchronisation of content and remote cursors

Signed-off-by: Tilman Vatteroth <git@tilmanvatteroth.de>
This commit is contained in:
Tilman Vatteroth 2023-03-22 20:21:40 +01:00
parent 67cf1432b2
commit 3a06f84af1
110 changed files with 3920 additions and 2201 deletions

View file

@ -4,33 +4,37 @@
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { store } from '..'
import type { AddOnlineUserAction, OnlineUser, RemoveOnlineUserAction } from './types'
import { RealtimeActionType } from './types'
import type { SetRealtimeSyncStatusAction, SetRealtimeUsersAction, SetRealtimeConnectionStatusAction } from './types'
import { RealtimeStatusActionType } from './types'
import type { RealtimeUser } from '@hedgedoc/commons'
/**
* Dispatches an event to add a user
*
* @param clientId The clientId of the user to add
* @param user The user to add.
*/
export const addOnlineUser = (clientId: number, user: OnlineUser): void => {
const action: AddOnlineUserAction = {
type: RealtimeActionType.ADD_ONLINE_USER,
clientId,
user
export const setRealtimeUsers = (users: RealtimeUser[]): void => {
const action: SetRealtimeUsersAction = {
type: RealtimeStatusActionType.SET_REALTIME_USERS,
users
}
store.dispatch(action)
}
/**
* Dispatches an event to remove a user from the online users list.
*
* @param clientId The yjs client id of the user to remove from the online users list.
*/
export const removeOnlineUser = (clientId: number): void => {
const action: RemoveOnlineUserAction = {
type: RealtimeActionType.REMOVE_ONLINE_USER,
clientId
}
store.dispatch(action)
export const setRealtimeConnectionState = (status: boolean): void => {
store.dispatch({
type: RealtimeStatusActionType.SET_REALTIME_CONNECTION_STATUS,
isConnected: status
} as SetRealtimeConnectionStatusAction)
}
export const setRealtimeSyncedState = (status: boolean): void => {
store.dispatch({
type: RealtimeStatusActionType.SET_REALTIME_SYNCED_STATUS,
isSynced: status
} as SetRealtimeSyncStatusAction)
}
export const resetRealtimeStatus = (): void => {
store.dispatch({
type: RealtimeStatusActionType.RESET_REALTIME_STATUS
})
}