Understanding some Typescript witchcraft.
# package-development
k
A learning question for me on a friday afternoon. looking at conditions, specifically how the
Umb.Condition.WorkspaceAlias
condition works. and i have a 'whats going on here question' in the code: the constructor ( https://github.com/umbraco/Umbraco.CMS.Backoffice/blob/main/src/packages/core/workspace/conditions/workspace-alias.condition.ts#L15-L35) does this :
Copy code
ts
constructor(host: UmbControllerHost, args: UmbConditionControllerArguments<WorkspaceAliasConditionConfig>) {
    super(host, args);

    let permissionCheck: ((context: UmbWorkspaceContext) => boolean) | undefined = undefined;
    if (this.config.match) {
      permissionCheck = (context: UmbWorkspaceContext) => context.workspaceAlias === this.config.match;
    } else if (this.config.oneOf) {
            permissionCheck = (context: UmbWorkspaceContext) => this.config.oneOf!.indexOf(context.workspaceAlias) !== -1;
}
what is this line doing !! 🤯
Copy code
ts
permissionCheck = (context: UmbWorkspaceContext) => context.workspaceAlias === this.config.match
specifically where has
(context: UmbWorkspaceContext)
come from, i can't see anything injected into the class (or its base classes?) its like this code just 'magics' up the context. what is this - and is it something i should know more about ?
m
I could be wrong here Kevin, but I think permissionCheck is a function you call, and context is a parameter. Is there an example of calling permissionCheck() elsewhere in the code base?
k
yes... i think it is. i was just getting my head around that.
so later on...
Copy code
ts
this.consumeContext(UMB_WORKSPACE_CONTEXT, (context) => {
  this.permitted = permissionCheck!(context);
});
m
Oh I see write underneath they are using consumeContext 🙂 LOL
k
which means it is calling the permissionCheck methd.
m
I'm learning all of this too.
k
turns out its they only one written that way... it makes sense if you can decifer it, just a little odd and its all new 🙂
every day is a day at school
2 Views