Skip to content

Commit

Permalink
refactor(element): separate files and review types
Browse files Browse the repository at this point in the history
  • Loading branch information
alimd committed Dec 28, 2022
1 parent 999e1b1 commit c6912a5
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 25 deletions.
4 changes: 2 additions & 2 deletions ui/element/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
"esm",
"alwatr"
],
"main": "element.js",
"main": "index.js",
"type": "module",
"types": "element.d.ts",
"types": "index.d.ts",
"author": "S. Ali Mihandoost <[email protected]> (https://ali.mihandoost.com)",
"license": "MIT",
"files": [
Expand Down
10 changes: 10 additions & 0 deletions ui/element/src/dummy-element.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {LitElement} from 'lit';

import {LoggerMixin} from './mixins/logging.js';

/**
* Alwatr Dummy Element
*
* Include: LoggerMixin
*/
export const AlwatrDummyElement = LoggerMixin(LitElement);
12 changes: 5 additions & 7 deletions ui/element/src/element.ts → ui/element/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {alwatrRegisteredList} from '@alwatr/logger';
import {LitElement} from 'lit';

import {LoggerMixin} from './mixins/logging.js';
import {SignalMixin} from './mixins/signal.js';
import {AlwatrRootElement} from './root.js';
export * from './dummy-element.js';
export * from './smart-element.js';
export * from './root-element.js';

export {LoggerMixin, SignalMixin, AlwatrRootElement};
export * from './mixins/localize.js';
export * from './mixins/direction.js';

export * from 'lit';
export * from 'lit/decorators.js';
Expand All @@ -21,5 +21,3 @@ alwatrRegisteredList.push({
version: '{{ALWATR_VERSION}}',
});

export const AlwatrDummyElement = LoggerMixin(LitElement);
export const AlwatrSmartElement = SignalMixin(LoggerMixin(LitElement));
11 changes: 5 additions & 6 deletions ui/element/src/mixins/logging.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
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';
import type {LitElement, PropertyValues} from 'lit';

export declare class LoggerMixinInterface extends LitElement {
protected _logger: AlwatrLogger;
}

export function LoggerMixin<ClassType extends Constructor<LitElement>>(
superClass: ClassType,
): Constructor<LoggerMixinInterface> & ClassType {
export function LoggerMixin<T extends Constructor<LitElement>>(
superClass: T,
): Constructor<LoggerMixinInterface> & T {
class LoggerMixinClass extends superClass {
protected _logger = createLogger(`<${this.tagName.toLowerCase()}>`);

Expand Down Expand Up @@ -55,5 +54,5 @@ export function LoggerMixin<ClassType extends Constructor<LitElement>>(
}
}

return LoggerMixinClass as unknown as Constructor<LoggerMixinInterface> & ClassType;
return LoggerMixinClass as unknown as Constructor<LoggerMixinInterface> & T;
}
11 changes: 5 additions & 6 deletions ui/element/src/mixins/signal.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import {LitElement} from 'lit';

import type {Constructor} from '../type.js';
import type {LitElement} from 'lit';

export declare class SignalMixinInterface extends LitElement {
protected _signalListenerList: Array<unknown>;
}

export function SignalMixin<ClassType extends Constructor<LitElement>>(
superClass: ClassType,
): Constructor<SignalMixinInterface> & ClassType {
export function SignalMixin<T extends Constructor<LitElement>>(
superClass: T,
): Constructor<SignalMixinInterface> & T {
class SignalMixinClass extends superClass {
protected _signalListenerList: Array<Record<string, unknown>> = [];

Expand All @@ -23,5 +22,5 @@ export function SignalMixin<ClassType extends Constructor<LitElement>>(
}
}

return SignalMixinClass as unknown as Constructor<SignalMixinInterface> & ClassType;
return SignalMixinClass as unknown as Constructor<SignalMixinInterface> & T;
}
10 changes: 6 additions & 4 deletions ui/element/src/root.ts → ui/element/src/root-element.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import {l10n} from '@alwatr/i18n';
import {router} from '@alwatr/router';
import {LitElement, html, css} from 'lit';
import {html, css} from 'lit';
import {cache} from 'lit/directives/cache.js';

import {LoggerMixin} from './mixins/logging.js';
import {AlwatrSmartElement} from './smart-element.js';

import type {RoutesConfig} from '@alwatr/router';
import type {CSSResultGroup} from 'lit';

/**
* Alwatr Root Base Element
* Alwatr Root Element
*
* Include: AlwatrSmartElement, root styles, router config, multi-page render
*/
export class AlwatrRootElement extends LoggerMixin(LitElement) {
export class AlwatrRootElement extends AlwatrSmartElement {
static override styles: CSSResultGroup = css`
:host {
display: flex;
Expand Down
9 changes: 9 additions & 0 deletions ui/element/src/smart-element.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {AlwatrDummyElement} from './dummy-element.js';
import {SignalMixin} from './mixins/signal.js';

/**
* Alwatr Smart Element
*
* Include: SignalMixin, AlwatrDummyElement
*/
export const AlwatrSmartElement = SignalMixin(AlwatrDummyElement);

0 comments on commit c6912a5

Please sign in to comment.