Plugin destroy()

I’m working on updating from 0.10.x → 0.15.x.

I have a plugin that reacts to selection changes as well as window resize and scroll events. I add the listeners in plugin.state.init. I’d like to remove the listeners, but it seems I would have to manually do that in the editor view destroy.

Would it be possible for to have plugin.view.init/destroy? Other tips for plugins with views / listeners?

There’s a conceptual mismatch here — plugins are associated with a state, not a view. States are never destroyed (they are immutable) and their lifetime isn’t tied to that of a view.

It’d be possible to introduce a separate concept of view plugins, or to have views automatically manage the lifetimes of view components for plugins by creating/destroying them depending on the set of plugins in their current state.

But so far, I’ve been addressing situations like this by simply having the code that manages the creation and cleanup of the editor view also initialize and destroy separate UI components. That doesn’t require any extra machinery in ProseMirror. Would this work for you?

:point_up: This is where I landed.

A view plugin concept would be nice… some way to manage views that respond to actions. In the lint demo you would not have to patch into the main view onAction.

I’ve added a feature where plugins take a view option, which is a function that may produce an object that’ll have its update method called whenever the view updates, and its destroy method called when the view is destroyed or the plugin detached from the view. See this patch.

1 Like