Skip to content

Commit a1e2bf7

Browse files
author
Auto Version Bump
committed
chore: rename and reuse types for senBroadcastMessage
1 parent 8191fa6 commit a1e2bf7

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

library/lib/apollon-editor.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ import {
1818
MetadataStoreContext,
1919
PopoverStoreContext,
2020
} from "./store/context"
21-
import { YjsSyncClass } from "@/sync/yjsSyncClass"
21+
import {
22+
MessageType,
23+
SendBroadcastMessage,
24+
YjsSyncClass,
25+
} from "@/sync/yjsSyncClass"
2226
import * as Y from "yjs"
2327
import { StoreApi } from "zustand"
2428
import * as Apollon from "./typings"
@@ -268,8 +272,8 @@ export class ApollonEditor {
268272
}
269273
}
270274

271-
public sendBroadcastMessage(sendFn: (base64Data: string) => void) {
272-
this.syncManager.setSendFunction(sendFn)
275+
public sendBroadcastMessage(sendFn: SendBroadcastMessage) {
276+
this.syncManager.setSendBroadcastMessage(sendFn)
273277
}
274278

275279
public receiveBroadcastedMessage(base64Data: string) {
@@ -313,7 +317,7 @@ export class ApollonEditor {
313317
}
314318

315319
static generateInitialSyncMessage(): string {
316-
const syncMessage = new Uint8Array(new Uint8Array([0]))
320+
const syncMessage = new Uint8Array(new Uint8Array([MessageType.YjsSYNC]))
317321
return YjsSyncClass.uint8ToBase64(syncMessage)
318322
}
319323
}

library/lib/sync/yjsSyncClass.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@ import {
1111
import { Edge, Node } from "@xyflow/react"
1212
import { Assessment } from "@/typings"
1313

14-
enum MessageType {
14+
export enum MessageType {
1515
YjsSYNC = 0,
1616
YjsUpdate = 1,
1717
}
1818

19-
type SendFunction = (data: string) => void
19+
export type SendBroadcastMessage = (base64data: string) => void
2020

2121
export class YjsSyncClass {
2222
private readonly stopYjsObserver: () => void
23-
private sendFunction: SendFunction | null = null
23+
private sendBroadcastMessage: SendBroadcastMessage | null = null
2424
private readonly ydoc: Y.Doc
2525
private readonly diagramStore: StoreApi<DiagramStore>
2626
private readonly metadataStore: StoreApi<MetadataStore>
@@ -40,8 +40,8 @@ export class YjsSyncClass {
4040
this.stopYjsObserver()
4141
}
4242

43-
public setSendFunction = (sendFn: SendFunction) => {
44-
this.sendFunction = sendFn
43+
public setSendBroadcastMessage = (sendFn: SendBroadcastMessage) => {
44+
this.sendBroadcastMessage = sendFn
4545
}
4646

4747
private applyUpdate = (update: Uint8Array, transactionOrigin: string) => {
@@ -57,14 +57,14 @@ export class YjsSyncClass {
5757
const update = decodedData.slice(1)
5858
this.applyUpdate(update, "remote")
5959
} else if (messageType === MessageType.YjsSYNC) {
60-
if (this.sendFunction) {
60+
if (this.sendBroadcastMessage) {
6161
const syncMessage = Y.encodeStateAsUpdate(this.ydoc)
6262
const fullMessage = new Uint8Array(1 + syncMessage.length)
6363
fullMessage[0] = MessageType.YjsUpdate
6464
fullMessage.set(syncMessage, 1)
6565

6666
const base64Message = YjsSyncClass.uint8ToBase64(fullMessage)
67-
this.sendFunction(base64Message)
67+
this.sendBroadcastMessage(base64Message)
6868
}
6969
}
7070
}
@@ -112,13 +112,13 @@ export class YjsSyncClass {
112112
_arg2: Y.Doc,
113113
transaction: Y.Transaction
114114
) => {
115-
if (this.sendFunction && transaction.origin === "store") {
115+
if (this.sendBroadcastMessage && transaction.origin === "store") {
116116
const syncMessage = Y.encodeStateAsUpdate(this.ydoc)
117117
const fullMessage = new Uint8Array(1 + syncMessage.length)
118118
fullMessage[0] = MessageType.YjsUpdate
119119
fullMessage.set(syncMessage, 1)
120120
const base64Message = YjsSyncClass.uint8ToBase64(fullMessage)
121-
this.sendFunction(base64Message)
121+
this.sendBroadcastMessage(base64Message)
122122
}
123123
}
124124

0 commit comments

Comments
 (0)