adobe / lit-mobx

Mixin and base class for using mobx with lit-element
Apache License 2.0
269 stars 16 forks source link

AsyncDirectives prevent mobx observables retriggering update #164

Open benjamind opened 1 year ago

benjamind commented 1 year ago

Expected Behaviour

Using async directives should 'just work' with the rendering component updating whenever an observable inside of the async directive is updated. The async directive of course should do what it always does.

Actual Behaviour

Mobx observables are not tracked within the callbacks of renders invoked by async directives like when and until.

Couple of possible approaches to fixing/workaround this:

dnsflnv commented 10 months ago

Use runInAction() in async.


    @action
    async deleteComment(id: number): Promise<void> {
        try {
            await api.delete(`comments/${id}`);
        } catch (error) {
            console.error(error);

            return;
        }

        const changedCommentThreads = { ...this.commentThreads };

        changedCommentThreads.data[id] = null;
        runInAction(() => {
            this.commentThreads = { ...changedCommentThreads };
        });
    }
benjamind commented 10 months ago

Not quite the same thing, thats an async action. In an async directive like asyncAppend that will invoke the template and update the DOM in an asynchronous callback that is outside of the reaction that is being used to capture template usage of mobx observables. Therefore observables inside of the asyncAppend callback will not cause further updates to the template. This can be quite surprising given the magic that happens elsewhere.

kamathln commented 5 months ago

Thanks a lot Benjamin, I was about to lose my mind.