Skip to content

Avoid Yjs unobserve warnings when calling destroy() after observers are already removed #28

@rahulCoder9417

Description

@rahulCoder9417

Checklist

Are you reporting a bug?

Try to report your issue in the correct repository.

Describe the bug

Calling destroy() on a MonacoBinding instance causes Yjs to print warnings like:

[yjs] Tried to remove event handler that doesn't exist.

This happens when ytext.unobserve(this._ytextObserver) is executed after the observer has already been removed somewhere else.
The warning is harmless but noisy, and it appears frequently in projects where cleanup is triggered multiple times (e.g. hot reload, component unmounts, or integration with reactive frameworks).

To Reproduce

Create a MonacoBinding instance with a Y.Text.

Call destroy() more than once or manually call ytext.unobserve somewhere before the binding’s destroy logic runs.

Open the console.

Observe the Yjs warning:

[yjs] Tried to remove event handler that doesn't exist.

Expected behavior

destroy() should clean up safely without throwing Yjs warnings, even if the Y.Text observer was removed earlier or if destroy is called multiple times.
The method should either check before unobserving or handle the error silently.

Screenshots

N/A — console output only.

Environment Information

Browser / Node.js: (e.g. Chrome 129 / Node.js 18)

Yjs version: output of npm ls yjs

y-monaco / y-protocols / y-websocket versions

Additional context

A small defensive guard fixes the issue. Example:

destroy (skipYjsUnobserve = false) {
this._monacoChangeHandler.dispose()
this._monacoDisposeHandler.dispose()

if (!skipYjsUnobserve) {
try { this.ytext.unobserve(this._ytextObserver) } catch {}
}

this.doc.off('beforeAllTransactions', this._beforeTransaction)

if (this.awareness) {
this.awareness.off('change', this._rerenderDecorations)
}
}

This avoids the warning while preserving existing behavior. No functional change occurs unless the observer was already removed.

y-monaco.js

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions