Skip to content Skip to sidebar Skip to footer

How Can I Prevent Vuex From Interfering With My Class Instance?

I am trying to store an instance of a class in Vuex (the EditorState from ProseMirror). This class is more or less immutable from the outside, meaning that whenever I want to make

Solution 1:

I fixed it by taking the suggestion from the top answer on this question and freezing my class instance before giving it to Vuex.

const store = newStore<AppState>({
    state: {
        editor: Object.freeze(editorState), // freeze because Vue reactivity messes it upfilename: null,
        metadata: {}
    },
    mutations: {
        updateDocument(context, transaction: Transaction) {
            console.log("updateDocument called");

            // freeze again
            context.editor = Object.freeze(context.editor.apply(transaction));
        }
    },
    strict: process.env.NODE_ENV === "development"
});

Since Object.freeze is not recursive, this doesn't have any effect on the internal workings of ProseMirror, but it discourages Vue from trying to modify the object.

Post a Comment for "How Can I Prevent Vuex From Interfering With My Class Instance?"