From a305c5d0e027b4c19fb1a22e4dc75797d205655a Mon Sep 17 00:00:00 2001 From: MohammadMahdi Zamanian Date: Sun, 25 Dec 2022 22:15:20 +0330 Subject: [PATCH] feat(ui/element): seprate mixins --- ui/element/src/element.ts | 85 +++----------------------------- ui/element/src/mixins/logging.ts | 59 ++++++++++++++++++++++ ui/element/src/mixins/signal.ts | 24 +++++++++ 3 files changed, 90 insertions(+), 78 deletions(-) create mode 100644 ui/element/src/mixins/logging.ts create mode 100644 ui/element/src/mixins/signal.ts diff --git a/ui/element/src/element.ts b/ui/element/src/element.ts index 8f49e0f60..bcac628a0 100644 --- a/ui/element/src/element.ts +++ b/ui/element/src/element.ts @@ -1,10 +1,11 @@ -import {alwatrRegisteredList, createLogger} from '@alwatr/logger'; +import {alwatrRegisteredList} from '@alwatr/logger'; import {LitElement} from 'lit'; -import type {Constructor} from './type.js'; -import type {AlwatrLogger} from '@alwatr/logger/type.js'; -import type {ListenerInterface} from '@alwatr/signal'; -import type {PropertyValues} from 'lit'; +import {LoggerMixin} from './mixins/logging.js'; +import {SignalMixin} from './mixins/signal.js'; + +export * from './mixins/logging.js'; +export * from './mixins/signal.js'; export * from 'lit'; export * from 'lit/decorators.js'; @@ -19,77 +20,5 @@ alwatrRegisteredList.push({ version: '{{ALWATR_VERSION}}', }); -export declare class LoggerMixinInterface extends LitElement { - protected _logger: AlwatrLogger; -} - -export function LoggerMixin>( - superClass: ClassType, -): Constructor & ClassType { - class LoggerMixinClass extends superClass { - protected _logger = createLogger(`<${this.tagName.toLowerCase()}>`); - - // eslint-disable-next-line @typescript-eslint/no-explicit-any - constructor(...args: any[]) { - super(...args); - this._logger.logMethod('constructor'); - } - - override connectedCallback(): void { - this._logger.logMethod('connectedCallback'); - super.connectedCallback(); - } - - override disconnectedCallback(): void { - this._logger.logMethod('disconnectedCallback'); - super.disconnectedCallback(); - } - - protected override update(changedProperties: PropertyValues): void { - this._logger.logMethodArgs('update', {changedProperties}); - super.update(changedProperties); - } - - protected override firstUpdated(changedProperties: PropertyValues): void { - this._logger.logMethodArgs('firstUpdated', {changedProperties}); - super.firstUpdated(changedProperties); - } - - protected override render(): unknown { - this._logger.logMethod('render'); - return; - } - - override dispatchEvent(event: Event): boolean { - this._logger.logMethodArgs('dispatchEvent', { - type: event.type, - detail: (event as Event & {detail?: unknown}).detail, - }); - return super.dispatchEvent(event); - } - } - - return LoggerMixinClass as unknown as Constructor & ClassType; -} - -export declare class SignalMixinInterface extends LitElement { - protected _signalListenerList: Array>; -} - -export function SignalMixin>( - superClass: ClassType, -): Constructor & ClassType { - class SignalMixinClass extends superClass { - protected _signalListenerList: Array> = []; - - override disconnectedCallback(): void { - super.disconnectedCallback(); - - this._signalListenerList.forEach((listener) => listener.remove()); - } - } - - return SignalMixinClass as unknown as Constructor & ClassType; -} - +export const AlwatrDummyElement = LoggerMixin(LitElement); export const AlwatrElement = SignalMixin(LoggerMixin(LitElement)); diff --git a/ui/element/src/mixins/logging.ts b/ui/element/src/mixins/logging.ts new file mode 100644 index 000000000..6362e5392 --- /dev/null +++ b/ui/element/src/mixins/logging.ts @@ -0,0 +1,59 @@ +import {createLogger} from '@alwatr/logger'; +import {LitElement} from 'lit'; + +import type {Constructor} from '../type.js'; +import type {AlwatrLogger} from '@alwatr/logger/type.js'; +import type {PropertyValues} from 'lit'; + +export declare class LoggerMixinInterface extends LitElement { + protected _logger: AlwatrLogger; +} + +export function LoggerMixin>( + superClass: ClassType, +): Constructor & ClassType { + class LoggerMixinClass extends superClass { + protected _logger = createLogger(`<${this.tagName.toLowerCase()}>`); + + // eslint-disable-next-line @typescript-eslint/no-explicit-any + constructor(...args: any[]) { + super(...args); + this._logger.logMethod('constructor'); + } + + override connectedCallback(): void { + this._logger.logMethod('connectedCallback'); + super.connectedCallback(); + } + + override disconnectedCallback(): void { + this._logger.logMethod('disconnectedCallback'); + super.disconnectedCallback(); + } + + protected override update(changedProperties: PropertyValues): void { + this._logger.logMethodArgs('update', {changedProperties}); + super.update(changedProperties); + } + + protected override firstUpdated(changedProperties: PropertyValues): void { + this._logger.logMethodArgs('firstUpdated', {changedProperties}); + super.firstUpdated(changedProperties); + } + + protected override render(): unknown { + this._logger.logMethod('render'); + return; + } + + override dispatchEvent(event: Event): boolean { + this._logger.logMethodArgs('dispatchEvent', { + type: event.type, + detail: (event as Event & {detail?: unknown}).detail, + }); + return super.dispatchEvent(event); + } + } + + return LoggerMixinClass as unknown as Constructor & ClassType; +} diff --git a/ui/element/src/mixins/signal.ts b/ui/element/src/mixins/signal.ts new file mode 100644 index 000000000..6d4f9267d --- /dev/null +++ b/ui/element/src/mixins/signal.ts @@ -0,0 +1,24 @@ +import {LitElement} from 'lit'; + +import type {Constructor} from '../type.js'; +import type {ListenerInterface} from '@alwatr/signal'; + +export declare class SignalMixinInterface extends LitElement { + protected _signalListenerList: Array>; +} + +export function SignalMixin>( + superClass: ClassType, +): Constructor & ClassType { + class SignalMixinClass extends superClass { + protected _signalListenerList: Array> = []; + + override disconnectedCallback(): void { + super.disconnectedCallback(); + + this._signalListenerList.forEach((listener) => listener.remove()); + } + } + + return SignalMixinClass as unknown as Constructor & ClassType; +}