Skip to content

Commit 272e6e1

Browse files
pyphiliakim
andauthored
fix: destroy document only on empty connections (#1988)
Co-authored-by: kim <kim.phanhoang@epfl.ch>
1 parent 7c7c0b1 commit 272e6e1

2 files changed

Lines changed: 38 additions & 1 deletion

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { WebSocket } from 'ws';
2+
3+
import { MOCK_LOGGER } from '../../../../../test/app';
4+
import { WSDoc } from './WSDoc';
5+
6+
const conn1 = {
7+
on: jest.fn(),
8+
close: jest.fn(),
9+
} as unknown as WebSocket;
10+
11+
const conn2 = {
12+
on: jest.fn(),
13+
close: jest.fn(),
14+
} as unknown as WebSocket;
15+
16+
describe('WSDoc', () => {
17+
it('closeConn destroy observers when no one is connected to it', () => {
18+
const doc = new WSDoc(null as never, 'test', false, MOCK_LOGGER);
19+
const initNbObservers = doc._observers.size;
20+
expect(initNbObservers).toBeGreaterThan(0);
21+
22+
doc.addConnection(conn1);
23+
doc.addConnection(conn2);
24+
25+
doc.closeConn(conn1);
26+
27+
// observers should still exist
28+
expect(doc._observers.size).toEqual(initNbObservers);
29+
30+
doc.closeConn(conn2);
31+
32+
// observer should be removed
33+
expect(doc._observers.size).toEqual(0);
34+
});
35+
});

src/services/item/plugins/page/WSDoc.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,9 @@ export class WSDoc extends Y.Doc {
149149
const controlledIds: Set<number> = this.conns.get(conn)!;
150150
this.conns.delete(conn);
151151
awarenessProtocol.removeAwarenessStates(this.awareness, Array.from(controlledIds), null);
152-
this.destroy();
152+
if (this.conns.size === 0) {
153+
this.destroy();
154+
}
153155
}
154156
conn.close(code, reason);
155157
}

0 commit comments

Comments
 (0)