Skip to content

Commit

Permalink
fix: connection closed awareness remove
Browse files Browse the repository at this point in the history
  • Loading branch information
naporin0624 committed Apr 25, 2024
1 parent 05af1cf commit 021aad9
Showing 1 changed file with 3 additions and 14 deletions.
17 changes: 3 additions & 14 deletions src/yjs/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable no-console */
import { Hono } from "hono";
import { removeAwarenessStates } from "y-protocols/awareness";
import { applyUpdate, encodeStateAsUpdate } from "yjs";
Expand Down Expand Up @@ -33,11 +32,6 @@ export class YDurableObjects<T extends Env> implements DurableObject {
void this.state.blockConcurrencyWhile(async () => {
const doc = await this.storage.getYDoc();
applyUpdate(this.doc, encodeStateAsUpdate(doc));
const clients = await this.state.storage.get<Set<number>>(
"ydoc:awareness_clients",
);
this.awarenessClients = clients ?? new Set<number>();
console.log("set awareness clients", this.awarenessClients.size);

for (const ws of this.state.getWebSockets()) {
this.connect(ws);
Expand All @@ -49,18 +43,13 @@ export class YDurableObjects<T extends Env> implements DurableObject {
});
this.doc.awareness.on(
"update",
async ({ added, removed }: AwarenessChanges) => {
for (const client of added) {
async ({ added, removed, updated }: AwarenessChanges) => {
for (const client of [...added, ...updated]) {
this.awarenessClients.add(client);
}
for (const client of removed) {
this.awarenessClients.delete(client);
}
console.log("awareness update", this.awarenessClients);
await this.state.storage.put(
"ydoc:awareness_clients",
this.awarenessClients,
);
},
);

Expand Down Expand Up @@ -114,7 +103,7 @@ export class YDurableObjects<T extends Env> implements DurableObject {
dispose?.();
this.sessions.delete(ws);
const clientIds = this.awarenessClients;
console.log("disconnect", this.awarenessClients);

removeAwarenessStates(this.doc.awareness, Array.from(clientIds), null);
} catch (e) {
// eslint-disable-next-line no-console
Expand Down

0 comments on commit 021aad9

Please sign in to comment.