Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
01ea259
make consume return undefined
nielslyngsoe Apr 21, 2025
0b93561
make consume return undefined
nielslyngsoe Apr 21, 2025
fca17b8
Merge branch 'v16/dev' into v16/feature/consume-can-be-undefined
nielslyngsoe Apr 22, 2025
b072a4a
a few more undefined context handlings
nielslyngsoe Apr 22, 2025
bca3940
unprovide context
nielslyngsoe Apr 22, 2025
548b146
rename
nielslyngsoe Apr 22, 2025
a24058a
jsdocs
nielslyngsoe Apr 22, 2025
a445324
refactor UmbContextBase to not use generic types
nielslyngsoe Apr 22, 2025
3baf6fa
reset target on disconnect
nielslyngsoe Apr 22, 2025
f2a22bc
posible undefined context
nielslyngsoe Apr 22, 2025
37c9d20
callback with undefined when disconnected
nielslyngsoe Apr 22, 2025
e3eec6c
update comment
nielslyngsoe Apr 23, 2025
0566560
correct types
nielslyngsoe Apr 23, 2025
33ed248
correct error handling
nielslyngsoe Apr 23, 2025
3c6cb48
do not throw an error when missing
nielslyngsoe Apr 23, 2025
1c93d1e
Merge branch 'v16/dev' into v16/feature/consume-can-be-undefined
nielslyngsoe Apr 23, 2025
ede48c1
Merge branch 'v16/dev' into v16/feature/consume-can-be-undefined
nielslyngsoe Apr 23, 2025
049fad3
always return permitted to onChange callback
nielslyngsoe Apr 23, 2025
20a3e08
fix not existing store
nielslyngsoe Apr 23, 2025
c171882
fix resetting structure manager
nielslyngsoe Apr 23, 2025
79ea5c1
fix requestAuditLogs
nielslyngsoe Apr 23, 2025
e017167
support gone context
nielslyngsoe Apr 23, 2025
8fe9599
support context not begin present
nielslyngsoe Apr 23, 2025
ac81973
use UMB_ENTITY_WORKSPACE_CONTEXT for right typing
nielslyngsoe Apr 23, 2025
ba33de4
correct type to use UMB_SUBMITTABLE_WORKSPACE_CONTEXT
nielslyngsoe Apr 23, 2025
0ea16b4
correct context consumption
nielslyngsoe Apr 23, 2025
60af1e8
Merge branch 'v16/dev' into v16/feature/consume-can-be-undefined
nielslyngsoe Apr 23, 2025
d9a0f3a
fix tests
nielslyngsoe Apr 23, 2025
af0abf2
fix tests
nielslyngsoe Apr 23, 2025
7062042
catch modal registration that has been destroyed
nielslyngsoe Apr 23, 2025
c0bd937
catch
nielslyngsoe Apr 23, 2025
339c852
Merge branch 'v16/dev' into v16/feature/consume-can-be-undefined
nielslyngsoe Apr 23, 2025
e4424ff
handle context unprovide
nielslyngsoe Apr 23, 2025
f181c2f
Merge branch 'v16/dev' into v16/feature/consume-can-be-undefined
nielslyngsoe Apr 23, 2025
3fc1c3a
more clean up
nielslyngsoe Apr 23, 2025
a384807
fix context consumption
nielslyngsoe Apr 23, 2025
a917858
Merge branch 'v16/dev' into v16/feature/consume-can-be-undefined
madsrasmussen Apr 23, 2025
af00423
Update repository-details.manager.ts
madsrasmussen Apr 23, 2025
5b05094
enable store to be undefined
nielslyngsoe Apr 23, 2025
5b54e8e
enable UmbRelationTypeDetailRepository store to be undefined
nielslyngsoe Apr 23, 2025
5adc470
remove log
nielslyngsoe Apr 23, 2025
b69ab3a
make tree store optional
nielslyngsoe Apr 23, 2025
148935a
Merge branch 'v16/dev' into v16/feature/consume-can-be-undefined
nielslyngsoe Apr 24, 2025
275478c
Merge branch 'v16/dev' into v16/feature/consume-can-be-undefined
madsrasmussen Apr 24, 2025
a36dbcd
change to use api property instead of context
madsrasmussen Apr 24, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class CustomValidationValidator extends UmbControllerBase implements UmbV
this.consumeContext(UMB_CONTENT_WORKSPACE_CONTEXT, async (context) => {
this.#workspaceContext = context;
this.observe(
await context.propertyValueByAlias<UmbPropertyEditorRteValueType>(propertyAlias, this.#variantId),
await context?.propertyValueByAlias<UmbPropertyEditorRteValueType>(propertyAlias, this.#variantId),
(value) => {
this.#value = value;
this.#checkValidation();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@
const EXAMPLE_CUSTOM_VALIDATION_PROPERTY_ALIAS = 'tiptap';

// The Example Workspace Context Controller:
export class CustomValidationWorkspaceContext extends UmbContextBase<
CustomValidationWorkspaceContext,
typeof EXAMPLE_CUSTOM_VALIDATION_CONTEXT
> {
export class CustomValidationWorkspaceContext extends UmbContextBase {
#validators?: Array<CustomValidationValidator>;

constructor(host: UmbControllerHost) {
Expand All @@ -30,12 +27,12 @@
if (propertyType.variesByCulture) {
// Because this property exists in multiple cultures we should observe cultures an create a custom validator for each culture value:
this.observe(
context.variantOptions,
context?.variantOptions,
(variantOptions) => {
// clean up old validators:
this.#validators?.forEach((x) => x.destroy());

this.#validators = variantOptions.map((option) => {
this.#validators = variantOptions?.map((option) => {

Check warning on line 35 in src/Umbraco.Web.UI.Client/examples/custom-validation-workspace-context/custom-validation-workspace-context.ts

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (v16/dev)

❌ New issue: Complex Method

CustomValidationWorkspaceContext.constructor has a cyclomatic complexity of 9, threshold = 9. This function has many conditional statements (e.g. if, for, while), leading to lower code health. Avoid adding more conditionals and code to it without refactoring.
return new CustomValidationValidator(
this,
EXAMPLE_CUSTOM_VALIDATION_PROPERTY_ALIAS,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class UmbExampleValidationContextDashboardElement extends UmbLitElement {

this.consumeContext(UMB_VALIDATION_CONTEXT, (validationContext) => {
this.observe(
validationContext.messages.messages,
validationContext?.messages.messages,
(messages) => {
this.messages = messages;
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api';
import { UmbNumberState } from '@umbraco-cms/backoffice/observable-api';

// The Example Workspace Context Controller:
export class WorkspaceContextCounterElement extends UmbContextBase<
WorkspaceContextCounterElement,
typeof EXAMPLE_COUNTER_CONTEXT
> {
export class WorkspaceContextCounterElement extends UmbContextBase {
// We always keep our states private, and expose the values as observables:
#counter = new UmbNumberState(0);
readonly counter = this.#counter.asObservable();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { EXAMPLE_COUNTER_CONTEXT } from './counter-workspace-context.js';
import { UmbTextStyles } from '@umbraco-cms/backoffice/style';
import { css, html, customElement, state, LitElement } from '@umbraco-cms/backoffice/external/lit';
import { UmbElementMixin } from '@umbraco-cms/backoffice/element-api';
import { EXAMPLE_COUNTER_CONTEXT } from './counter-workspace-context';

@customElement('example-counter-workspace-view')
export class ExampleCounterWorkspaceView extends UmbElementMixin(LitElement) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class UmbAppAuthController extends UmbControllerBase {

// Observe the user's authorization state and start the authorization flow if the user is not authorized
this.observe(
context.timeoutSignal,
context?.timeoutSignal,
() => {
this.makeAuthorizationRequest('timedOut');
},
Expand Down
4 changes: 2 additions & 2 deletions src/Umbraco.Web.UI.Client/src/apps/app/app-logo.element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ export class UmbAppLogoElement extends UmbLitElement {
super();

this.consumeContext(UMB_SERVER_CONTEXT, (instance) => {
this._serverUrl = instance.getServerUrl();
this._serverUrl = instance?.getServerUrl();
});

this.consumeContext(UMB_THEME_CONTEXT, (context) => {
this.observe(
context.theme,
context?.theme,
(theme) => {
this._theme = theme;
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { UMB_AUTH_CONTEXT } from '@umbraco-cms/backoffice/auth';
import { UMB_CURRENT_USER_CONTEXT } from '@umbraco-cms/backoffice/current-user';
import { UmbSysinfoRepository } from '@umbraco-cms/backoffice/sysinfo';

export class UmbBackofficeContext extends UmbContextBase<UmbBackofficeContext> {
export class UmbBackofficeContext extends UmbContextBase {
#activeSectionAlias = new UmbStringState(undefined);
public readonly activeSectionAlias = this.#activeSectionAlias.asObservable();

Expand All @@ -28,15 +28,15 @@ export class UmbBackofficeContext extends UmbContextBase<UmbBackofficeContext> {

// TODO: We need to ensure this request is called every time the user logs in, but this should be done somewhere across the app and not here [JOV]
this.consumeContext(UMB_AUTH_CONTEXT, (authContext) => {
this.observe(authContext.isAuthorized, (isAuthorized) => {
this.observe(authContext?.isAuthorized, (isAuthorized) => {
if (!isAuthorized) return;
this.#getVersion();
});
});

this.consumeContext(UMB_CURRENT_USER_CONTEXT, (userContext) => {
this.observe(
userContext.allowedSections,
userContext?.allowedSections,
(allowedSections) => {
if (!allowedSections) return;
// TODO: Please be aware that we re-initialize this initializer based on user permissions. I suggest we should solve this specific case should be improved by the ability to change the filter [NL]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export class UmbBackofficeElement extends UmbLitElement {

// TODO: We need to ensure this request is called every time the user logs in, but this should be done somewhere across the app and not here [JOV]
this.consumeContext(UMB_AUTH_CONTEXT, (authContext) => {
this.observe(authContext.isAuthorized, (isAuthorized) => {
this.observe(authContext?.isAuthorized, (isAuthorized) => {
if (!isAuthorized) return;
serverExtensions.registerPrivateExtensions();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class UmbBackofficeHeaderLogoElement extends UmbLitElement {

this.consumeContext(UMB_BACKOFFICE_CONTEXT, (context) => {
this.observe(
context.version,
context?.version,
(version) => {
if (!version) return;
this._version = version;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { UmbContextBase } from '@umbraco-cms/backoffice/class-api';
* Context API for the installer
* @class UmbInstallerContext
*/
export class UmbInstallerContext extends UmbContextBase<UmbInstallerContext, typeof UMB_INSTALLER_CONTEXT> {
export class UmbInstallerContext extends UmbContextBase {
private _data = new UmbObjectState<InstallRequestModelReadable>({
user: { name: '', email: '', password: '', subscribeToNewsletter: false },
database: { id: '', providerName: '', useIntegratedAuthentication: false, trustServerCertificate: false },
Expand Down
4 changes: 2 additions & 2 deletions src/Umbraco.Web.UI.Client/src/apps/preview/preview.context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { UMB_SERVER_CONTEXT } from '@umbraco-cms/backoffice/server';

const UMB_LOCALSTORAGE_SESSION_KEY = 'umb:previewSessions';

export class UmbPreviewContext extends UmbContextBase<UmbPreviewContext> {
export class UmbPreviewContext extends UmbContextBase {
#culture?: string | null;
#serverUrl: string = '';
#webSocket?: WebSocket;
Expand All @@ -26,7 +26,7 @@ export class UmbPreviewContext extends UmbContextBase<UmbPreviewContext> {
super(host, UMB_PREVIEW_CONTEXT);

this.consumeContext(UMB_SERVER_CONTEXT, (instance) => {
this.#serverUrl = instance.getServerUrl();
this.#serverUrl = instance?.getServerUrl() ?? '';

const params = new URLSearchParams(window.location.search);

Expand Down
10 changes: 7 additions & 3 deletions src/Umbraco.Web.UI.Client/src/libs/class-api/class.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type {
UmbContextCallback,
UmbContextConsumerAsPromiseOptionsType,
UmbContextConsumerController,
UmbContextMinimal,
UmbContextProviderController,
UmbContextToken,
} from '@umbraco-cms/backoffice/context-api';
Expand Down Expand Up @@ -48,7 +49,10 @@ export interface UmbClassInterface extends UmbControllerHost {
* @returns {UmbContextProviderController} Reference to the created Context Provider Controller instance
* @memberof UmbClassInterface
*/
provideContext<R = unknown>(alias: string | UmbContextToken<R>, instance: R): UmbContextProviderController<R>;
provideContext<R extends UmbContextMinimal = UmbContextMinimal>(
alias: string | UmbContextToken<R>,
instance: R,
): UmbContextProviderController<R>;

/**
* @description Setup a subscription for a context. The callback is called when the context is resolved.
Expand All @@ -57,7 +61,7 @@ export interface UmbClassInterface extends UmbControllerHost {
* @returns {UmbContextConsumerController} Reference to the created Context Consumer Controller instance
* @memberof UmbClassInterface
*/
consumeContext<BaseType = unknown, ResultType extends BaseType = BaseType>(
consumeContext<BaseType extends UmbContextMinimal = UmbContextMinimal, ResultType extends BaseType = BaseType>(
alias: string | UmbContextToken<BaseType, ResultType>,
callback: UmbContextCallback<ResultType>,
): UmbContextConsumerController<BaseType, ResultType>;
Expand All @@ -68,7 +72,7 @@ export interface UmbClassInterface extends UmbControllerHost {
* @returns {Promise<unknown>} A Promise with the reference to the Context Api Instance
* @memberof UmbClassInterface
*/
getContext<BaseType = unknown, ResultType extends BaseType = BaseType>(
getContext<BaseType extends UmbContextMinimal = UmbContextMinimal, ResultType extends BaseType = BaseType>(
alias: string | UmbContextToken<BaseType, ResultType>,
options?: UmbClassGetContextOptions,
): Promise<ResultType | undefined>;
Expand Down
11 changes: 7 additions & 4 deletions src/Umbraco.Web.UI.Client/src/libs/class-api/class.mixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
type UmbContextCallback,
UmbContextConsumerController,
UmbContextProviderController,
type UmbContextMinimal,
} from '@umbraco-cms/backoffice/context-api';
import { type ObserverCallback, UmbObserverController, simpleHashCode } from '@umbraco-cms/backoffice/observable-api';

Expand Down Expand Up @@ -80,45 +81,47 @@
}

provideContext<
BaseType = unknown,
BaseType extends UmbContextMinimal = UmbContextMinimal,
ResultType extends BaseType = BaseType,
InstanceType extends ResultType = ResultType,
>(
contextAlias: string | UmbContextToken<BaseType, ResultType>,
instance: InstanceType,
): UmbContextProviderController<BaseType, ResultType, InstanceType> {
return new UmbContextProviderController<BaseType, ResultType, InstanceType>(this, contextAlias, instance);
}

consumeContext<BaseType = unknown, ResultType extends BaseType = BaseType>(
consumeContext<BaseType extends UmbContextMinimal = UmbContextMinimal, ResultType extends BaseType = BaseType>(
contextAlias: string | UmbContextToken<BaseType, ResultType>,
callback: UmbContextCallback<ResultType>,
): UmbContextConsumerController<BaseType, ResultType> {
return new UmbContextConsumerController(this, contextAlias, callback);
}

async getContext<BaseType = unknown, ResultType extends BaseType = BaseType>(
async getContext<BaseType extends UmbContextMinimal = UmbContextMinimal, ResultType extends BaseType = BaseType>(
contextAlias: string | UmbContextToken<BaseType, ResultType>,
options?: UmbClassGetContextOptions,
): Promise<ResultType | undefined> {
const controller = new UmbContextConsumerController(this, contextAlias);
if (options) {
if (options.passContextAliasMatches) {
controller.passContextAliasMatches();
}
if (options.skipHost) {
controller.skipHost();
}
}
return controller.asPromise(options);
}

public override destroy(): void {
if (this._host) {
this._host.removeUmbController(this);
this._host = undefined as never;
}
super.destroy();
if (this._host) {
this._host = undefined as never;
}

Check warning on line 124 in src/Umbraco.Web.UI.Client/src/libs/class-api/class.mixin.ts

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (v16/dev)

❌ Getting worse: Complex Method

UmbClassMixin increases in cyclomatic complexity from 14 to 15, threshold = 9. This function has many conditional statements (e.g. if, for, while), leading to lower code health. Avoid adding more conditionals and code to it without refactoring.
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,9 @@ import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api';
* This base provides the necessary for a class to become a context-api controller.
*
*/
export abstract class UmbContextBase<
ContextType,
GivenContextToken extends UmbContextToken<ContextType, ContextType> = UmbContextToken<ContextType, ContextType>,
>
extends UmbControllerBase
implements UmbContext
{
constructor(host: UmbControllerHost, contextToken: GivenContextToken | string) {
export abstract class UmbContextBase extends UmbControllerBase implements UmbContext {
constructor(host: UmbControllerHost, contextToken: UmbContextToken<any> | string) {
super(host, contextToken.toString());
this.provideContext(contextToken, this as unknown as ContextType);
this.provideContext(contextToken, this as any);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import type { UmbController } from '@umbraco-cms/backoffice/controller-api';

// eslint-disable-next-line @typescript-eslint/no-empty-object-type
export interface UmbContext extends UmbController {}
export interface UmbContext extends UmbController {
getHostElement(): Element;
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import type { UmbContextToken } from '../token/context-token.js';
import type { UmbContextMinimal } from '../types.js';
import { UmbContextConsumer } from './context-consumer.js';
import type { UmbContextCallback } from './context-request.event.js';
import type { UmbControllerHost, UmbController } from '@umbraco-cms/backoffice/controller-api';

export class UmbContextConsumerController<BaseType = unknown, ResultType extends BaseType = BaseType>
export class UmbContextConsumerController<
BaseType extends UmbContextMinimal = UmbContextMinimal,
ResultType extends BaseType = BaseType,
>
extends UmbContextConsumer<BaseType, ResultType>
implements UmbController
{
Expand Down
Loading
Loading