Update Compartment from ViewUpdate

Hi, im new to Codemirror. Im trying to add a feature on a project that updates the keymap extension inside a compartment dynamically. To do that, i’m listening to changes on the view using a ViewPlugin and, when the change that should trigger the keymap update happens, i want to update the keymap compartment (using Compartment.reconfigure()). The problem is that i cant trigger an update (dispatching the effect generated by the reconfigure()) inside this ViewUpdate. Is there a way to change the compartment in this update?

I got a solution (maybe): i used EditorView.updateListener.of(). This allowed me to update the editor’s compartments (probably because updateListener doesn’t create a update itself, while ViewPlugin.update() does…?)

Here is the code (is it hacky or is it normal?):

function languageKeymap(editor, keymapComp){
    return EditorView.updateListener.of((update)=>{
        if(...){ // verify if keymap change applies here
            let targetLanguageKeymap; // if it does, determine which language to apply keymaps for
            switch(language.name){
                case "markdown":
                    targetLanguageKeymap = markdownKeymaps.keymap;
                break;
                default:
                    targetLanguageKeymap = [];
                break;
            }
            editor.view.dispatch({effects: keymapComp.reconfigure(targetLanguageKeymap)});
        }
    })
}

I think this example will be helpful here. It’s better to use a transaction extender so that you don’t add additional update cycles.

Both worked perfectly, but transactionExtender really seems to be more aproppriate. Thanks a bunch for the help :smiley: