codex-team / codex-team/editor.js

Missing null check in API events off

Open
#1,508 1 comment 0 reactions 0 assignees View on GitHub
bug
Dominant language
TypeScript
Stars
31.9k
Forks
2.2k
Avg merge
1d 1h
Merged PRs (30d)
1

Description

I'm doing some stuff with event handling using the `api.events.off` method of the `Events` module. Seems like in an old version (2.17) the destroy() method has not been called. Now with 2.19.1 it is called and sets the `subscribers` list to null. If some clean-up calls the off method, this fails as there is no null check.

Probably the subscribers should be set to `{}` instead of null or implement a null check in`off` method.
https://github.com/codex-team/editor.js/blob/4cea66f8caadcd3324994798a919d87ce4bccc47/src/components/modules/events.ts#L91

Need to do a try/catch hack to avoid TypeErrors:
```
Uncaught TypeError: Cannot read property 'dummy' of null
```

Example (using React.useEffect):

```
useEffect(() => {
const eventID = 'dummy'
api.events.on(eventID, callback)
return () => {
try {
// calling this may fail
api.events.off(eventID, callback)
} catch (TypeError) {
// event subscriber may be null due to call of destroy() method (e.g on page change)
// avoid error, as null check is missing (we have no control of this private editorJS method)
// https://github.com/codex-team/editor.js/blob/4cea66f8caadcd3324994798a919d87ce4bccc47/src/components/modules/events.ts#L91
}
}
}, [callback, api.events])
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.