Cache objects between Widgets updates

In a Widget’s constructor, I’m initializing a class (a UI library) that is slow to compute, so I’d like to cache it between updates. Unfortunately, the Widget’s constructor is called on every update, so that object is disposed, even if the Widget is equal.

The only thing I see is retained between updates is the Widget’s DOM, so something like this works:

class MyWidget extends WidgetType {
    updateDOM(dom: HTMLElement, view: EditorView): boolean {
        const my_property = dom.my_property;
        return true;

    toDOM(view: EditorView): HTMLElement {
        const container = createDiv();
        container.my_property = {};
        return container;

I understand Widgets are supposed to be cheap to create, but is there a proper way to retain data inside a Widget?


Not really, except closing over it in the code run in toDOM. I don’t know why you need to retain this value, so I can’t really give specific advice from what you’ve shown.