mark contextToken as default type = unknown

This commit is contained in:
Jacob Overgaard
2023-01-17 11:05:40 +01:00
parent f04b152d2d
commit 14e63f7abb
7 changed files with 34 additions and 32 deletions

View File

@@ -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<T>
extends UmbContextConsumer<T, UmbControllerHostInterface>
implements UmbControllerInterface<T>
export class UmbContextConsumerController<T = unknown>
extends UmbContextConsumer<UmbControllerHostInterface, T>
implements UmbControllerInterface
{
public get unique() {
return this._contextAlias;
return this._contextAlias?.toString();
}
constructor(

View File

@@ -6,7 +6,7 @@ import { UmbContextRequestEventImplementation, UmbContextCallback } from './cont
* @export
* @class UmbContextConsumer
*/
export class UmbContextConsumer<ContextType, HostType extends EventTarget = EventTarget> {
export class UmbContextConsumer<HostType extends EventTarget = EventTarget, T = unknown> {
private _instance?: unknown;
get instance(): unknown | undefined {
return this._instance;
@@ -25,11 +25,11 @@ export class UmbContextConsumer<ContextType, HostType extends EventTarget = Even
*/
constructor(
protected host: HostType,
protected _contextAlias: string | ContextToken<ContextType>,
private _callback: UmbContextCallback<ContextType>
protected _contextAlias: string | ContextToken,
private _callback: UmbContextCallback<T>
) {}
private _onResponse = (instance: ContextType) => {
private _onResponse = (instance: any) => {
this._instance = instance;
this._callback(instance);
};

View File

@@ -2,15 +2,15 @@ import { ContextToken } from '../context-token';
export const umbContextRequestEventType = 'umb:context-request';
export type UmbContextCallback<T> = (instance: T) => void;
export type UmbContextCallback<T = unknown> = (instance: T) => void;
/**
* @export
* @interface UmbContextRequestEvent
*/
export interface UmbContextRequestEvent<T> extends Event {
readonly contextAlias: string | ContextToken<T>;
readonly callback: UmbContextCallback<T>;
export interface UmbContextRequestEvent extends Event {
readonly contextAlias: string | ContextToken;
readonly callback: UmbContextCallback;
}
/**
@@ -19,15 +19,15 @@ export interface UmbContextRequestEvent<T> extends Event {
* @extends {Event}
* @implements {UmbContextRequestEvent}
*/
export class UmbContextRequestEventImplementation<T> extends Event implements UmbContextRequestEvent<T> {
export class UmbContextRequestEventImplementation extends Event implements UmbContextRequestEvent {
public constructor(
public readonly contextAlias: string | ContextToken<T>,
public readonly callback: UmbContextCallback<T>
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<Event> => {
export const isUmbContextRequestEvent = (event: Event): event is UmbContextRequestEventImplementation => {
return event.type === umbContextRequestEventType;
};

View File

@@ -1,4 +1,4 @@
export class ContextToken<T> {
export class ContextToken<T = unknown> {
/**
* @param _desc Description for the token,
* used only for debugging purposes,

View File

@@ -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 });
}
}

View File

@@ -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<UmbControllerHostInterface> implements UmbControllerInterface {
import type { UmbControllerHostInterface, UmbControllerInterface } from '@umbraco-cms/controller';
export class UmbContextProviderController<T = unknown>
extends UmbContextProvider<UmbControllerHostInterface>
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<T>, 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<UmbControll
this.host.removeController(this);
}
}
}

View File

@@ -1,15 +1,15 @@
import { umbContextRequestEventType, isUmbContextRequestEvent } from '../consume/context-request.event';
import { ContextToken } from '../context-token';
import { UmbContextProvideEventImplementation } from './context-provide.event';
/**
* @export
* @class UmbContextProvider
*/
export class UmbContextProvider<HostType extends EventTarget = EventTarget > {
export class UmbContextProvider<HostType extends EventTarget = EventTarget> {
protected host: HostType;
protected _contextAlias: string;
protected _contextAlias: string | ContextToken;
#instance: unknown;
/**
@@ -19,7 +19,7 @@ export class UmbContextProvider<HostType extends EventTarget = EventTarget > {
* @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;