allow ContextToken as provide and consume alias
This commit is contained in:
@@ -1,16 +1,22 @@
|
||||
import { ContextToken } from '../context-token';
|
||||
import { UmbContextConsumer } from './context-consumer';
|
||||
import { UmbContextCallback } from './context-request.event';
|
||||
import type { UmbControllerInterface } from 'src/core/controller/controller.interface';
|
||||
import { UmbControllerHostInterface } from 'src/core/controller/controller-host.mixin';
|
||||
|
||||
import { UmbControllerHostInterface, UmbControllerInterface } from '@umbraco-cms/controllers';
|
||||
|
||||
export class UmbContextConsumerController extends UmbContextConsumer<UmbControllerHostInterface> implements UmbControllerInterface {
|
||||
|
||||
export class UmbContextConsumerController<T>
|
||||
extends UmbContextConsumer<T, UmbControllerHostInterface>
|
||||
implements UmbControllerInterface<T>
|
||||
{
|
||||
public get unique() {
|
||||
return this._contextAlias;
|
||||
}
|
||||
|
||||
constructor(host:UmbControllerHostInterface, contextAlias: string, callback: UmbContextCallback) {
|
||||
constructor(
|
||||
host: UmbControllerHostInterface,
|
||||
contextAlias: string | ContextToken<T>,
|
||||
callback: UmbContextCallback<T>
|
||||
) {
|
||||
super(host, contextAlias, callback);
|
||||
host.addController(this);
|
||||
}
|
||||
@@ -20,5 +26,4 @@ export class UmbContextConsumerController extends UmbContextConsumer<UmbControll
|
||||
this.host.removeController(this);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { ContextToken } from '../injectionToken';
|
||||
import { isUmbContextProvideEventType, umbContextProvideEventType } from '../provide/context-provide.event';
|
||||
import { UmbContextRequestEventImplementation, UmbContextCallback } from './context-request.event';
|
||||
|
||||
@@ -5,8 +6,7 @@ import { UmbContextRequestEventImplementation, UmbContextCallback } from './cont
|
||||
* @export
|
||||
* @class UmbContextConsumer
|
||||
*/
|
||||
export class UmbContextConsumer<HostType extends EventTarget = EventTarget> {
|
||||
|
||||
export class UmbContextConsumer<ContextType, HostType extends EventTarget = EventTarget> {
|
||||
private _instance?: unknown;
|
||||
get instance(): unknown | undefined {
|
||||
return this._instance;
|
||||
@@ -23,13 +23,16 @@ export class UmbContextConsumer<HostType extends EventTarget = EventTarget> {
|
||||
* @param {UmbContextCallback} _callback
|
||||
* @memberof UmbContextConsumer
|
||||
*/
|
||||
constructor(protected host: HostType, protected _contextAlias: string, private _callback: UmbContextCallback) {}
|
||||
constructor(
|
||||
protected host: HostType,
|
||||
protected _contextAlias: string | ContextToken<ContextType>,
|
||||
private _callback: UmbContextCallback<ContextType>
|
||||
) {}
|
||||
|
||||
|
||||
private _onResponse = (instance: unknown) => {
|
||||
private _onResponse = (instance: ContextType) => {
|
||||
this._instance = instance;
|
||||
this._callback(instance);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @memberof UmbContextConsumer
|
||||
|
||||
@@ -1,14 +1,16 @@
|
||||
import { ContextToken } from '../injectionToken';
|
||||
|
||||
export const umbContextRequestEventType = 'umb:context-request';
|
||||
|
||||
export type UmbContextCallback = (instance: any) => void;
|
||||
export type UmbContextCallback<T> = (instance: T) => void;
|
||||
|
||||
/**
|
||||
* @export
|
||||
* @interface UmbContextRequestEvent
|
||||
*/
|
||||
export interface UmbContextRequestEvent extends Event {
|
||||
readonly contextAlias: string;
|
||||
readonly callback: UmbContextCallback;
|
||||
export interface UmbContextRequestEvent<T> extends Event {
|
||||
readonly contextAlias: string | ContextToken<T>;
|
||||
readonly callback: UmbContextCallback<T>;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -17,12 +19,15 @@ export interface UmbContextRequestEvent extends Event {
|
||||
* @extends {Event}
|
||||
* @implements {UmbContextRequestEvent}
|
||||
*/
|
||||
export class UmbContextRequestEventImplementation extends Event implements UmbContextRequestEvent {
|
||||
public constructor(public readonly contextAlias: string, public readonly callback: UmbContextCallback) {
|
||||
export class UmbContextRequestEventImplementation<T> extends Event implements UmbContextRequestEvent<T> {
|
||||
public constructor(
|
||||
public readonly contextAlias: string | ContextToken<T>,
|
||||
public readonly callback: UmbContextCallback<T>
|
||||
) {
|
||||
super(umbContextRequestEventType, { bubbles: true, composed: true, cancelable: true });
|
||||
}
|
||||
}
|
||||
|
||||
export const isUmbContextRequestEvent = (event: Event): event is UmbContextRequestEventImplementation => {
|
||||
export const isUmbContextRequestEvent = (event: Event): event is UmbContextRequestEventImplementation<Event> => {
|
||||
return event.type === umbContextRequestEventType;
|
||||
};
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
export interface UmbControllerInterface {
|
||||
get unique(): string | undefined;
|
||||
import { ContextToken } from '@umbraco-cms/context-api';
|
||||
|
||||
export interface UmbControllerInterface<T> {
|
||||
get unique(): string | ContextToken<T>;
|
||||
hostConnected(): void;
|
||||
hostDisconnected(): void;
|
||||
destroy(): void;
|
||||
|
||||
Reference in New Issue
Block a user