one less generic type to parse around

This commit is contained in:
Niels Lyngsø
2023-08-24 10:18:29 +02:00
parent bb708d737a
commit 6dcdb8523e
9 changed files with 38 additions and 46 deletions

View File

@@ -16,8 +16,8 @@ export interface UmbClassMixinInterface extends UmbControllerHost, UmbController
unique?: string
): UmbObserverController<T>;
provideContext<R = unknown>(alias: string | UmbContextToken<R>, instance: R): UmbContextProviderController<R>;
consumeContext<BaseType = unknown, DiscriminatedType extends BaseType = never, ResultType extends BaseType = BaseType>(
alias: string | UmbContextToken<BaseType, DiscriminatedType, ResultType>,
consumeContext<BaseType = unknown, ResultType extends BaseType = BaseType>(
alias: string | UmbContextToken<BaseType, ResultType>,
callback: UmbContextCallback<ResultType>
): UmbContextConsumerController<BaseType, DiscriminatedType, ResultType>;
): UmbContextConsumerController<BaseType, ResultType>;
}

View File

@@ -29,14 +29,13 @@ declare class UmbClassMixinDeclaration implements UmbClassMixinInterface {
): UmbObserverController<T>;
provideContext<
BaseType = unknown,
DiscriminatedType extends BaseType = never,
ResultType extends BaseType = BaseType,
InstanceType extends ResultType = ResultType
>(alias: string | UmbContextToken<BaseType, DiscriminatedType, ResultType>, instance: InstanceType): UmbContextProviderController<BaseType, DiscriminatedType, ResultType, InstanceType>;
consumeContext<BaseType = unknown, DiscriminatedType extends BaseType = never, ResultType extends BaseType = BaseType>(
alias: string | UmbContextToken<BaseType, DiscriminatedType, ResultType>,
>(alias: string | UmbContextToken<BaseType, ResultType>, instance: InstanceType): UmbContextProviderController<BaseType, ResultType, InstanceType>;
consumeContext<BaseType = unknown, ResultType extends BaseType = BaseType>(
alias: string | UmbContextToken<BaseType, ResultType>,
callback: UmbContextCallback<ResultType>
): UmbContextConsumerController<BaseType, DiscriminatedType, ResultType>;
): UmbContextConsumerController<BaseType, ResultType>;
hasController(controller: UmbController): boolean;
getControllers(filterMethod: (ctrl: UmbController) => boolean): UmbController[];
addController(controller: UmbController): void;
@@ -90,15 +89,14 @@ export const UmbClassMixin = <T extends ClassConstructor>(superClass: T) => {
provideContext
<
BaseType = unknown,
DiscriminatedType extends BaseType = never,
ResultType extends BaseType = keyof DiscriminatedType extends BaseType ? DiscriminatedType : BaseType,
ResultType extends BaseType = BaseType,
InstanceType extends ResultType = ResultType
>
(
contextAlias: string | UmbContextToken<BaseType, DiscriminatedType, ResultType>,
contextAlias: string | UmbContextToken<BaseType, ResultType>,
instance: InstanceType
): UmbContextProviderController {
return new UmbContextProviderController<BaseType, DiscriminatedType, ResultType, InstanceType>(this, contextAlias, instance);
return new UmbContextProviderController<BaseType, ResultType, InstanceType>(this, contextAlias, instance);
}
/**
@@ -108,10 +106,10 @@ export const UmbClassMixin = <T extends ClassConstructor>(superClass: T) => {
* @return {UmbContextConsumerController} Reference to a Context Consumer Controller instance
* @memberof UmbElementMixin
*/
consumeContext<BaseType = unknown, DiscriminatedType extends BaseType = never, ResultType extends BaseType = BaseType>(
contextAlias: string | UmbContextToken<BaseType, DiscriminatedType, ResultType>,
consumeContext<BaseType = unknown, ResultType extends BaseType = BaseType>(
contextAlias: string | UmbContextToken<BaseType, ResultType>,
callback: UmbContextCallback<ResultType>
): UmbContextConsumerController<BaseType, DiscriminatedType, ResultType> {
): UmbContextConsumerController<BaseType, ResultType> {
return new UmbContextConsumerController(this, contextAlias, callback);
}
}

View File

@@ -6,9 +6,8 @@ import type { UmbControllerHost, UmbController } from '@umbraco-cms/backoffice/c
export class UmbContextConsumerController<
BaseType = unknown,
DiscriminatedType extends BaseType = never,
ResultType extends BaseType = keyof DiscriminatedType extends BaseType ? DiscriminatedType : BaseType
> extends UmbContextConsumer<BaseType, DiscriminatedType, ResultType> implements UmbController {
ResultType extends BaseType = BaseType
> extends UmbContextConsumer<BaseType, ResultType> implements UmbController {
#controllerAlias = Symbol();
#host: UmbControllerHost;
@@ -16,7 +15,7 @@ export class UmbContextConsumerController<
return this.#controllerAlias;
}
constructor(host: UmbControllerHost, contextAlias: string | UmbContextToken<BaseType, DiscriminatedType, ResultType>, callback: UmbContextCallback<ResultType>) {
constructor(host: UmbControllerHost, contextAlias: string | UmbContextToken<BaseType, ResultType>, callback: UmbContextCallback<ResultType>) {
super(host.getHostElement(), contextAlias, callback);
this.#host = host;
host.addController(this);

View File

@@ -13,8 +13,7 @@ import { UmbContextRequestEventImplementation, UmbContextCallback } from './cont
*/
export class UmbContextConsumer<
BaseType = unknown,
DiscriminatedType extends BaseType = never,
ResultType extends BaseType = keyof DiscriminatedType extends BaseType ? DiscriminatedType : BaseType> {
ResultType extends BaseType = BaseType> {
#callback?: UmbContextCallback<ResultType>;
#promise?: Promise<ResultType>;
#promiseResolver?: (instance: ResultType) => void;
@@ -26,7 +25,7 @@ ResultType extends BaseType = keyof DiscriminatedType extends BaseType ? Discrim
#contextAlias: string;
#discriminator?: UmbContextDiscriminator<BaseType, DiscriminatedType>;
#discriminator?: UmbContextDiscriminator<BaseType, ResultType>;
/**
* Creates an instance of UmbContextConsumer.
@@ -37,7 +36,7 @@ ResultType extends BaseType = keyof DiscriminatedType extends BaseType ? Discrim
*/
constructor(
protected hostElement: EventTarget,
contextAlias: string | UmbContextToken<BaseType, DiscriminatedType, ResultType>,
contextAlias: string | UmbContextToken<BaseType, ResultType>,
callback?: UmbContextCallback<ResultType>
) {
this.#contextAlias = contextAlias.toString();

View File

@@ -10,7 +10,7 @@ export type UmbContextCallback<T> = (instance: T) => void;
* @interface UmbContextRequestEvent
*/
export interface UmbContextRequestEvent<ResultType = unknown> extends Event {
readonly contextAlias: string | UmbContextToken<unknown, any, ResultType>;
readonly contextAlias: string | UmbContextToken<unknown, ResultType>;
readonly callback: UmbContextCallback<ResultType>;
}
@@ -22,7 +22,7 @@ export interface UmbContextRequestEvent<ResultType = unknown> extends Event {
*/
export class UmbContextRequestEventImplementation<ResultType = unknown> extends Event implements UmbContextRequestEvent<ResultType> {
public constructor(
public readonly contextAlias: string | UmbContextToken<any, any, ResultType>,
public readonly contextAlias: string | UmbContextToken<any, ResultType>,
public readonly callback: UmbContextCallback<ResultType>
) {
super(umbContextRequestEventType, { bubbles: true, composed: true, cancelable: true });

View File

@@ -4,17 +4,16 @@ import type { UmbControllerHost, UmbController } from '@umbraco-cms/backoffice/c
export class UmbContextProviderController<
BaseType = unknown,
DiscriminatedType extends BaseType = never,
ResultType extends BaseType = keyof DiscriminatedType extends BaseType ? DiscriminatedType : BaseType,
ResultType extends BaseType = BaseType,
InstanceType extends ResultType = ResultType
> extends UmbContextProvider<BaseType, DiscriminatedType, ResultType> implements UmbController {
> extends UmbContextProvider<BaseType, ResultType> implements UmbController {
#host: UmbControllerHost;
public get controllerAlias() {
return this._contextAlias.toString();
}
constructor(host: UmbControllerHost, contextAlias: string | UmbContextToken<BaseType, DiscriminatedType, ResultType>, instance: InstanceType) {
constructor(host: UmbControllerHost, contextAlias: string | UmbContextToken<BaseType, ResultType>, instance: InstanceType) {
super(host.getHostElement(), contextAlias, instance);
this.#host = host;

View File

@@ -13,7 +13,7 @@ import {
* @export
* @class UmbContextProvider
*/
export class UmbContextProvider<BaseType = unknown, DiscriminatedType extends BaseType = BaseType, ResultType extends BaseType = keyof DiscriminatedType extends BaseType ? DiscriminatedType : BaseType> {
export class UmbContextProvider<BaseType = unknown, ResultType extends BaseType = BaseType> {
protected hostElement: EventTarget;
protected _contextAlias: string;
@@ -35,7 +35,7 @@ export class UmbContextProvider<BaseType = unknown, DiscriminatedType extends Ba
* @param {*} instance
* @memberof UmbContextProvider
*/
constructor(hostElement: EventTarget, contextAlias: string | UmbContextToken<BaseType, DiscriminatedType, ResultType>, instance: ResultType) {
constructor(hostElement: EventTarget, contextAlias: string | UmbContextToken<BaseType, ResultType>, instance: ResultType) {
this.hostElement = hostElement;
this._contextAlias = contextAlias.toString();
this.#instance = instance;

View File

@@ -3,10 +3,9 @@ export type UmbContextDiscriminator<BaseType, DiscriminatorResult extends BaseTy
export class UmbContextToken<
BaseType = unknown,
DiscriminatedType extends BaseType = never,
ResultType extends BaseType = keyof DiscriminatedType extends BaseType ? DiscriminatedType : BaseType> {
ResultType extends BaseType = BaseType> {
#discriminator: UmbContextDiscriminator<BaseType, DiscriminatedType> | undefined;
#discriminator: UmbContextDiscriminator<BaseType, ResultType> | undefined;
/**
* Get the type of the token
*
@@ -21,11 +20,11 @@ ResultType extends BaseType = keyof DiscriminatedType extends BaseType ? Discrim
/**
* @param alias Unique identifier for the token
*/
constructor(protected alias: string, discriminator?: UmbContextDiscriminator<BaseType, DiscriminatedType>) {
constructor(protected alias: string, discriminator?: UmbContextDiscriminator<BaseType, ResultType>) {
this.#discriminator = discriminator;
}
getDiscriminator(): UmbContextDiscriminator<BaseType, DiscriminatedType> | undefined {
getDiscriminator(): UmbContextDiscriminator<BaseType, ResultType> | undefined {
return this.#discriminator;
}

View File

@@ -26,14 +26,13 @@ export declare class UmbElement extends UmbControllerHostElement {
): UmbObserverController<T>;
provideContext<
BaseType = unknown,
DiscriminatedType extends BaseType = never,
ResultType extends BaseType = BaseType,
InstanceType extends ResultType = ResultType
>(alias: string | UmbContextToken<BaseType, DiscriminatedType, ResultType>, instance: InstanceType): UmbContextProviderController<BaseType, DiscriminatedType, ResultType, InstanceType>;
consumeContext<BaseType = unknown, DiscriminatedType extends BaseType = never, ResultType extends BaseType = BaseType>(
alias: string | UmbContextToken<BaseType, DiscriminatedType, ResultType>,
>(alias: string | UmbContextToken<BaseType, ResultType>, instance: InstanceType): UmbContextProviderController<BaseType, ResultType, InstanceType>;
consumeContext<BaseType = unknown, ResultType extends BaseType = BaseType>(
alias: string | UmbContextToken<BaseType, ResultType>,
callback: UmbContextCallback<ResultType>
): UmbContextConsumerController<BaseType, DiscriminatedType, ResultType>;
): UmbContextConsumerController<BaseType, ResultType>;
}
export const UmbElementMixin = <T extends HTMLElementConstructor>(superClass: T) => {
@@ -60,11 +59,10 @@ export const UmbElementMixin = <T extends HTMLElementConstructor>(superClass: T)
*/
provideContext<
BaseType = unknown,
DiscriminatedType extends BaseType = never,
ResultType extends BaseType = BaseType,
InstanceType extends ResultType = ResultType
>
(alias: string | UmbContextToken<BaseType, DiscriminatedType, ResultType>, instance: InstanceType): UmbContextProviderController<BaseType, DiscriminatedType, ResultType, InstanceType> {
(alias: string | UmbContextToken<BaseType, ResultType>, instance: InstanceType): UmbContextProviderController<BaseType, ResultType, InstanceType> {
return new UmbContextProviderController(this, alias, instance);
}
@@ -75,10 +73,10 @@ export const UmbElementMixin = <T extends HTMLElementConstructor>(superClass: T)
* @return {UmbContextConsumerController} Reference to a Context Consumer Controller instance
* @memberof UmbElementMixin
*/
consumeContext<BaseType = unknown, DiscriminatedType extends BaseType = never, ResultType extends BaseType = BaseType>(
alias: string | UmbContextToken<BaseType, DiscriminatedType, ResultType>,
consumeContext<BaseType = unknown, ResultType extends BaseType = BaseType>(
alias: string | UmbContextToken<BaseType, ResultType>,
callback: UmbContextCallback<ResultType>
): UmbContextConsumerController<BaseType, DiscriminatedType, ResultType> {
): UmbContextConsumerController<BaseType, ResultType> {
return new UmbContextConsumerController(this, alias, callback);
}
}