How do I consume multiple contexts & observables?
# package-development
w
Is there a way to get multiple contexts together as my condition depends on stuff from two separate different contexts and the stuff I want to observe resolves at different times & thus in my flow of logic something is undefined. How can I ensure all my stuff has been observed & set from 2 contexts before I attempt to run my code logic?
Again total brain fog today...
This is what I have at the moment, where I wait until both contexts are ready with
Promise.all()
and using the method
.asPromise()
on the Consume Contexts like so...
Copy code
ts
constructor(host: UmbControllerHost, args: UmbConditionControllerArguments<RemoveUnpublishConditionConfig>) {
    super(host, args);

    // Need to ensure both contexts have been consumed before we can check if we are permitted
    // Hence each context is consumed as a promise and then once all resolved we can check if we are permitted
    Promise.all([
        this.consumeContext(UMB_CURRENT_USER_CONTEXT, (currentUserCtx) =>{
            // Observe the current user object
            this.observe(currentUserCtx.currentUser, (currentUser) => {
                this.#currentUser = currentUser;
            });
        }).asPromise(),
        this.consumeContext(UMB_DOCUMENT_WORKSPACE_CONTEXT, (workspaceCtx) => {
            // Observe the current document type alias & the document guid/key
            this.observe(observeMultiple([workspaceCtx.structure.ownerContentType, workspaceCtx.unique]), ([currentDocType, unique]) => {
                this.#unique = unique;
                this.#currentDocTypeAlias = currentDocType?.alias;
            });
        }).asPromise()
    ]).then(() => {
        console.log('🎉🎉 All contexts consumed & observers?! - check if we are permitted 🎉🎉');

        // Checks user permissions & sets bool
        this.#checkUserPermissions();

        // Checks if current doctype should be excluded or not
        this.#checkForDocType();

        // Once both set above then we can perform final check
        this.#checkIfPemitted();
    });
}
Is there a neater/better way to do this or.... ?
Also this uses the
observeMultiple
Copy code
ts
this.observe(observeMultiple([workspaceCtx.structure.ownerContentType, workspaceCtx.unique]), ([currentDocType, unique]) => {
  this.#unique = unique;
  this.#currentDocTypeAlias = currentDocType?.alias;
});
Any thoughts cleverer people than me? @Markus Johansson @Jacob Overgaard @Niels Lyngsø or @nathanwoulfe
n
I've been known to use Promise.all...
And observeMultiple. As for which is better?
w
It works just a bit funky of a mess to read
n
Yeah, I would say the thing to be aware about is: What would you like to happen if one of the contexts got replaced in a later moment? If you need your code to be triggerede again in such sace, then the Promise All is not enough, as it is only triggered the first time you consumed something. We typically have this low pratical way of solving it:
Copy code
this.consumeContext(CONTEXT_TOKEN_A, (context) =>{
        this.observe(..., () => {
            this.#myvalueA = ....
            this.gotSomething();
        });
    }),
    this.consumeContext(CONTEXT_TOKEN_B, (context) => {
        this.observe(..., () => {
            this.#myvalueB = ....
            this.gotSomething();
        });
    })

...
       
    gotSomething() {
        if(!this.#myValueA || !this.myValueB) return;
        console.log('I have my values and can do my thing');
    }
A side note... please do not observe a BIG model like this, if you only need the Alias:
workspaceCtx.structure.ownerContentType
Then we should expose a observable just for the alias.
w
Got any other smarter ways for me to get the current doctype alias from a context/observable part??
And if your in giving feedback mode Niels - I will happily share the entire file with you and you can rip it apart and tell me what I should rework
n
That sounds like an episode of Umbraco co-labs
w
I have been asking Lotte for a while for something like that or bring your problems to a HQ member for a code review 😛
OK right for now off to rework it back to how it was before the promise.all
11 Views