consumeContext vs getContext
# package-development
w
What's the difference between
this.consumeContext
or
this.getContext
when extending a class from
UmbEntityActionBase<any>
?
j
Good question!
getContext
is a one-off that you can await to get the latest context right away.
consumeContext
keeps watching for new contexts (it's an observer with a callback).
You would use consumeContext in your constructor, whereas getContext can be used inside an async method directly.
d
Ooo, this is really good to know. Gotta remember this
w
Yeh I used consumeContext in ctor and now wondering why the entity action in core I looked at used getContext in the main execute method. Not sure what the benefit is or preference is to use?!
j
It's overkill to consume the notificationContext for instance, since you would probably only show a single notification anyway, so might as well just request it just before you are about to show the message
currently doing the unpublish modal, which has to show tracked dependencies, but only in some cases will it prevent the document being unpublished, so in that case I only make the network request to the configuration endpoint to ask the server if that is allowed if I detect tracked references
Copy code
// If there are tracked references, we also want to check if we are allowed to unpublish the document:
        if (this._hasTrackedReferences) {
            const documentConfigurationContext = await this.getContext(UMB_DOCUMENT_CONFIGURATION_CONTEXT);
            this._hasUnpublishPermission =
                (await documentConfigurationContext.getDocumentConfiguration())?.disableUnpublishWhenReferenced === false ??
                true;
        }
w
This was the only difference I could spot as to why my entity action would not trigger from the content tree menu items
And Niels just uses getContext as opposed to consumeContext which I do... 🤔
2 Views