migrate @umbraco-cms/controller to libs

This commit is contained in:
Jacob Overgaard
2023-01-20 13:28:41 +01:00
parent 9815303c90
commit e53ee08093
10 changed files with 23 additions and 37 deletions

View File

@@ -1,13 +1,13 @@
import type { HTMLElementConstructor } from '../models';
import type { HTMLElementConstructor } from '../../src/core/models';
import { UmbControllerInterface } from './controller.interface';
export declare class UmbControllerHostInterface extends HTMLElement {
//#controllers:UmbController[];
//#attached:boolean;
hasController(controller:UmbControllerInterface): boolean;
hasController(controller: UmbControllerInterface): boolean;
getControllers(filterMethod: (ctrl: UmbControllerInterface) => boolean): UmbControllerInterface[];
addController(controller:UmbControllerInterface): void;
removeController(controller:UmbControllerInterface): void;
addController(controller: UmbControllerInterface): void;
removeController(controller: UmbControllerInterface): void;
}
/**
@@ -19,7 +19,6 @@ export declare class UmbControllerHostInterface extends HTMLElement {
*/
export const UmbControllerHostMixin = <T extends HTMLElementConstructor>(superClass: T) => {
class UmbContextConsumerClass extends superClass {
#controllers: UmbControllerInterface[] = [];
#attached = false;
@@ -29,7 +28,7 @@ export const UmbControllerHostMixin = <T extends HTMLElementConstructor>(superCl
* @param {UmbControllerInterface} ctrl
*/
hasController(ctrl: UmbControllerInterface): boolean {
return (this.#controllers.indexOf(ctrl) !== -1);
return this.#controllers.indexOf(ctrl) !== -1;
}
/**
@@ -45,18 +44,17 @@ export const UmbControllerHostMixin = <T extends HTMLElementConstructor>(superCl
* @param {UmbControllerInterface} ctrl
*/
addController(ctrl: UmbControllerInterface): void {
// Check if there is one already with same unique
if(ctrl.unique) {
this.#controllers.forEach(x => {
if(x.unique === ctrl.unique) {
if (ctrl.unique) {
this.#controllers.forEach((x) => {
if (x.unique === ctrl.unique) {
this.removeController(x);
}
});
}
this.#controllers.push(ctrl);
if(this.#attached) {
if (this.#attached) {
ctrl.hostConnected();
}
}
@@ -68,9 +66,9 @@ export const UmbControllerHostMixin = <T extends HTMLElementConstructor>(superCl
*/
removeController(ctrl: UmbControllerInterface): void {
const index = this.#controllers.indexOf(ctrl);
if(index !== -1) {
if (index !== -1) {
this.#controllers.splice(index, 1);
if(this.#attached) {
if (this.#attached) {
ctrl.hostDisconnected();
}
ctrl.destroy();
@@ -83,8 +81,8 @@ export const UmbControllerHostMixin = <T extends HTMLElementConstructor>(superCl
* @param {string} unique
*/
removeControllerByAlias(unique: string): void {
this.#controllers.forEach(x => {
if(x.unique === unique) {
this.#controllers.forEach((x) => {
if (x.unique === unique) {
this.removeController(x);
}
});

View File

@@ -1,16 +1,14 @@
import { expect } from '@open-wc/testing';
import { customElement } from 'lit/decorators.js';
import { UmbContextProviderController } from '../context-api/provide/context-provider.controller';
import { UmbControllerHostInterface, UmbControllerHostMixin } from './controller-host.mixin';
import { UmbContextProviderController } from '@umbraco-cms/context-api';
class MyClass {
prop = 'value from provider';
}
@customElement('test-my-controller-host')
class MyHostElement extends UmbControllerHostMixin(HTMLElement) {
}
export class MyHostElement extends UmbControllerHostMixin(HTMLElement) {}
describe('UmbContextProvider', () => {
type NewType = UmbControllerHostInterface;
@@ -22,10 +20,8 @@ describe('UmbContextProvider', () => {
hostElement = document.createElement('test-my-controller-host') as UmbControllerHostInterface;
});
describe('Destroyed controllers is gone from host', () => {
it('has a host property', () => {
const ctrl = new UmbContextProviderController(hostElement, 'my-test-context', contextInstance);
expect(hostElement.hasController(ctrl)).to.be.true;
@@ -38,7 +34,6 @@ describe('UmbContextProvider', () => {
describe('Unique controllers replace each other', () => {
it('has a host property', () => {
const firstCtrl = new UmbContextProviderController(hostElement, 'my-test-context', contextInstance);
const secondCtrl = new UmbContextProviderController(hostElement, 'my-test-context', contextInstance);
@@ -46,5 +41,4 @@ describe('UmbContextProvider', () => {
expect(hostElement.hasController(secondCtrl)).to.be.true;
});
});
});

View File

@@ -1,7 +1,4 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { UmbController } from '../../src/core/controller/controller.class';
import { UmbControllerHostInterface } from '../../src/core/controller/controller-host.mixin';
import { UmbContextConsumerController } from '../../src/core/context-api/consume/context-consumer.controller';
import {
UmbNotificationOptions,
UmbNotificationService,
@@ -9,6 +6,8 @@ import {
UMB_NOTIFICATION_SERVICE_CONTEXT_TOKEN,
} from '../../src/core/notification';
import { ApiError, CancelablePromise, ProblemDetails } from '@umbraco-cms/backend-api';
import { UmbController, UmbControllerHostInterface } from '@umbraco-cms/controller';
import { UmbContextConsumerController } from '@umbraco-cms/context-api';
export class UmbResourceController extends UmbController {
#promise: Promise<any>;

View File

@@ -1,7 +1,7 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { UmbControllerHostInterface } from '../../src/core/controller/controller-host.mixin';
import { UmbNotificationOptions } from '../../src/core/notification';
import { UmbResourceController } from './resource.controller';
import { UmbNotificationOptions } from 'src/core/notification';
import { UmbControllerHostInterface } from '@umbraco-cms/controller';
export function tryExecuteAndNotify<T>(
host: UmbControllerHostInterface,

View File

@@ -1,17 +1,14 @@
import { Observable } from 'rxjs';
import { UmbObserver } from './observer';
import type { UmbControllerInterface } from 'src/core/controller/controller.interface';
import { UmbControllerHostInterface } from '@umbraco-cms/controller';
import { UmbControllerInterface, UmbControllerHostInterface } from '@umbraco-cms/controller';
export class UmbObserverController<T> extends UmbObserver<T> implements UmbControllerInterface {
_alias?: string;
public get unique() {
return this._alias;
}
constructor(host:UmbControllerHostInterface, source: Observable<T>, callback: (_value: T) => void, alias?: string) {
constructor(host: UmbControllerHostInterface, source: Observable<T>, callback: (_value: T) => void, alias?: string) {
super(source, callback);
this._alias = alias;
@@ -29,6 +26,4 @@ export class UmbObserverController<T> extends UmbObserver<T> implements UmbContr
hostConnected() {
return;
}
}

View File

@@ -1,6 +1,6 @@
import type { Observable } from 'rxjs';
import { UmbControllerHostInterface } from '../controller/controller-host.mixin';
import { UniqueBehaviorSubject } from '../observable-api/unique-behavior-subject';
import { UmbControllerHostInterface } from '@umbraco-cms/controller';
export interface UmbDataStoreIdentifiers {
key?: string;

View File

@@ -23,7 +23,7 @@
"@umbraco-cms/models": ["src/core/models"],
"@umbraco-cms/backend-api": ["libs/backend-api"],
"@umbraco-cms/context-api": ["src/core/context-api"],
"@umbraco-cms/controller": ["src/core/controller"],
"@umbraco-cms/controller": ["libs/controller"],
"@umbraco-cms/element": ["src/core/element"],
"@umbraco-cms/extensions-api": ["src/core/extensions-api"],
"@umbraco-cms/extensions-registry": ["src/core/extensions-registry"],