From 14e63f7abbbefd75ac06a847e803cd37e0a475f2 Mon Sep 17 00:00:00 2001 From: Jacob Overgaard <752371+iOvergaard@users.noreply.github.com> Date: Tue, 17 Jan 2023 11:05:40 +0100 Subject: [PATCH] mark contextToken as default type = unknown --- .../consume/context-consumer.controller.ts | 10 +++++----- .../core/context-api/consume/context-consumer.ts | 8 ++++---- .../context-api/consume/context-request.event.ts | 16 ++++++++-------- .../src/core/context-api/context-token.ts | 2 +- .../context-api/provide/context-provide.event.ts | 6 ++++-- .../provide/context-provider.controller.ts | 16 ++++++++-------- .../core/context-api/provide/context-provider.ts | 8 ++++---- 7 files changed, 34 insertions(+), 32 deletions(-) diff --git a/src/Umbraco.Web.UI.Client/src/core/context-api/consume/context-consumer.controller.ts b/src/Umbraco.Web.UI.Client/src/core/context-api/consume/context-consumer.controller.ts index 1d4188c99c..9d1633840a 100644 --- a/src/Umbraco.Web.UI.Client/src/core/context-api/consume/context-consumer.controller.ts +++ b/src/Umbraco.Web.UI.Client/src/core/context-api/consume/context-consumer.controller.ts @@ -2,14 +2,14 @@ import { ContextToken } from '../context-token'; import { UmbContextConsumer } from './context-consumer'; import { UmbContextCallback } from './context-request.event'; -import type { UmbControllerHostInterface, UmbControllerInterface } from '@umbraco-cms/controllers'; +import type { UmbControllerHostInterface, UmbControllerInterface } from '@umbraco-cms/controller'; -export class UmbContextConsumerController - extends UmbContextConsumer - implements UmbControllerInterface +export class UmbContextConsumerController + extends UmbContextConsumer + implements UmbControllerInterface { public get unique() { - return this._contextAlias; + return this._contextAlias?.toString(); } constructor( diff --git a/src/Umbraco.Web.UI.Client/src/core/context-api/consume/context-consumer.ts b/src/Umbraco.Web.UI.Client/src/core/context-api/consume/context-consumer.ts index 5af6274d8b..006690cf9c 100644 --- a/src/Umbraco.Web.UI.Client/src/core/context-api/consume/context-consumer.ts +++ b/src/Umbraco.Web.UI.Client/src/core/context-api/consume/context-consumer.ts @@ -6,7 +6,7 @@ import { UmbContextRequestEventImplementation, UmbContextCallback } from './cont * @export * @class UmbContextConsumer */ -export class UmbContextConsumer { +export class UmbContextConsumer { private _instance?: unknown; get instance(): unknown | undefined { return this._instance; @@ -25,11 +25,11 @@ export class UmbContextConsumer, - private _callback: UmbContextCallback + protected _contextAlias: string | ContextToken, + private _callback: UmbContextCallback ) {} - private _onResponse = (instance: ContextType) => { + private _onResponse = (instance: any) => { this._instance = instance; this._callback(instance); }; diff --git a/src/Umbraco.Web.UI.Client/src/core/context-api/consume/context-request.event.ts b/src/Umbraco.Web.UI.Client/src/core/context-api/consume/context-request.event.ts index fca575eb45..52e44d8257 100644 --- a/src/Umbraco.Web.UI.Client/src/core/context-api/consume/context-request.event.ts +++ b/src/Umbraco.Web.UI.Client/src/core/context-api/consume/context-request.event.ts @@ -2,15 +2,15 @@ import { ContextToken } from '../context-token'; export const umbContextRequestEventType = 'umb:context-request'; -export type UmbContextCallback = (instance: T) => void; +export type UmbContextCallback = (instance: T) => void; /** * @export * @interface UmbContextRequestEvent */ -export interface UmbContextRequestEvent extends Event { - readonly contextAlias: string | ContextToken; - readonly callback: UmbContextCallback; +export interface UmbContextRequestEvent extends Event { + readonly contextAlias: string | ContextToken; + readonly callback: UmbContextCallback; } /** @@ -19,15 +19,15 @@ export interface UmbContextRequestEvent extends Event { * @extends {Event} * @implements {UmbContextRequestEvent} */ -export class UmbContextRequestEventImplementation extends Event implements UmbContextRequestEvent { +export class UmbContextRequestEventImplementation extends Event implements UmbContextRequestEvent { public constructor( - public readonly contextAlias: string | ContextToken, - public readonly callback: UmbContextCallback + public readonly contextAlias: string | ContextToken, + public readonly callback: UmbContextCallback ) { super(umbContextRequestEventType, { bubbles: true, composed: true, cancelable: true }); } } -export const isUmbContextRequestEvent = (event: Event): event is UmbContextRequestEventImplementation => { +export const isUmbContextRequestEvent = (event: Event): event is UmbContextRequestEventImplementation => { return event.type === umbContextRequestEventType; }; diff --git a/src/Umbraco.Web.UI.Client/src/core/context-api/context-token.ts b/src/Umbraco.Web.UI.Client/src/core/context-api/context-token.ts index 5f3086d211..060206316e 100644 --- a/src/Umbraco.Web.UI.Client/src/core/context-api/context-token.ts +++ b/src/Umbraco.Web.UI.Client/src/core/context-api/context-token.ts @@ -1,4 +1,4 @@ -export class ContextToken { +export class ContextToken { /** * @param _desc Description for the token, * used only for debugging purposes, diff --git a/src/Umbraco.Web.UI.Client/src/core/context-api/provide/context-provide.event.ts b/src/Umbraco.Web.UI.Client/src/core/context-api/provide/context-provide.event.ts index c6c38a80e7..763c2d5a06 100644 --- a/src/Umbraco.Web.UI.Client/src/core/context-api/provide/context-provide.event.ts +++ b/src/Umbraco.Web.UI.Client/src/core/context-api/provide/context-provide.event.ts @@ -1,3 +1,5 @@ +import { ContextToken } from '../context-token'; + export const umbContextProvideEventType = 'umb:context-provide'; /** @@ -5,7 +7,7 @@ export const umbContextProvideEventType = 'umb:context-provide'; * @interface UmbContextProvideEvent */ export interface UmbContextProvideEvent extends Event { - readonly contextAlias: string; + readonly contextAlias: string | ContextToken; } /** @@ -15,7 +17,7 @@ export interface UmbContextProvideEvent extends Event { * @implements {UmbContextProvideEvent} */ export class UmbContextProvideEventImplementation extends Event implements UmbContextProvideEvent { - public constructor(public readonly contextAlias: string) { + public constructor(public readonly contextAlias: string | ContextToken) { super(umbContextProvideEventType, { bubbles: true, composed: true }); } } diff --git a/src/Umbraco.Web.UI.Client/src/core/context-api/provide/context-provider.controller.ts b/src/Umbraco.Web.UI.Client/src/core/context-api/provide/context-provider.controller.ts index 17afb97861..d7e35c992a 100644 --- a/src/Umbraco.Web.UI.Client/src/core/context-api/provide/context-provider.controller.ts +++ b/src/Umbraco.Web.UI.Client/src/core/context-api/provide/context-provider.controller.ts @@ -1,15 +1,16 @@ +import { ContextToken } from '../context-token'; import { UmbContextProvider } from './context-provider'; -import type { UmbControllerInterface } from 'src/core/controller/controller.interface'; -import { UmbControllerHostInterface } from '@umbraco-cms/controller'; - - -export class UmbContextProviderController extends UmbContextProvider implements UmbControllerInterface { +import type { UmbControllerHostInterface, UmbControllerInterface } from '@umbraco-cms/controller'; +export class UmbContextProviderController + extends UmbContextProvider + implements UmbControllerInterface +{ public get unique() { - return this._contextAlias; + return this._contextAlias.toString(); } - constructor(host:UmbControllerHostInterface, contextAlias: string, instance: unknown) { + constructor(host: UmbControllerHostInterface, contextAlias: string | ContextToken, instance: T) { super(host, contextAlias, instance); // TODO: What if this API is already provided with this alias? maybe handle this in the controller: @@ -23,5 +24,4 @@ export class UmbContextProviderController extends UmbContextProvider { - +export class UmbContextProvider { protected host: HostType; - protected _contextAlias: string; + protected _contextAlias: string | ContextToken; #instance: unknown; /** @@ -19,7 +19,7 @@ export class UmbContextProvider { * @param {*} instance * @memberof UmbContextProvider */ - constructor(host: HostType, contextAlias: string, instance: unknown) { + constructor(host: HostType, contextAlias: string | ContextToken, instance: unknown) { this.host = host; this._contextAlias = contextAlias; this.#instance = instance;