Skip to content

fix: client setInterval memory leaks #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions src/y-socket-io/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ export class SocketIOProvider extends Observable {
this.roomName = roomName
this.doc = doc
this.awareness = awareness
this.released = false

this._broadcastChannel = `${url}/${roomName}`
this.disableBc = disableBc
Expand Down Expand Up @@ -318,17 +319,20 @@ export class SocketIOProvider extends Observable {
])
)
}
const timeout = () => {
if (this.released) return
if (this.socket.disconnected) return
this.socket.emit(
'sync-step-1',
Y.encodeStateVector(this.doc),
(/** @type {Uint8Array} */ update) => {
Y.applyUpdate(this.doc, new Uint8Array(update), this)
}
)
setTimeout(timeout, resyncInterval)
}
if (resyncInterval > 0) {
this.resyncInterval = setInterval(() => {
if (this.socket.disconnected) return
this.socket.emit(
'sync-step-1',
Y.encodeStateVector(this.doc),
(/** @type {Uint8Array} */ update) => {
Y.applyUpdate(this.doc, new Uint8Array(update), this)
}
)
}, resyncInterval)
setTimeout(timeout, resyncInterval)
}
}

Expand Down Expand Up @@ -377,7 +381,7 @@ export class SocketIOProvider extends Observable {
* @type {() => void}
*/
destroy () {
if (this.resyncInterval != null) clearInterval(this.resyncInterval)
this.released = true
this.disconnect()
if (typeof window !== 'undefined') { window.removeEventListener('beforeunload', this.beforeUnloadHandler) } else if (typeof process !== 'undefined') { process.off('exit', this.beforeUnloadHandler) }
this.awareness.off('update', this.awarenessUpdate)
Expand Down
Loading