-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
75b54b5
commit a305c5d
Showing
3 changed files
with
90 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<ClassType extends Constructor<LitElement>>( | ||
superClass: ClassType, | ||
): Constructor<LoggerMixinInterface> & 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<LoggerMixinInterface> & ClassType; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<ListenerInterface<keyof AlwatrSignals>>; | ||
} | ||
|
||
export function SignalMixin<ClassType extends Constructor<LitElement>>( | ||
superClass: ClassType, | ||
): Constructor<SignalMixinInterface> & ClassType { | ||
class SignalMixinClass extends superClass { | ||
protected _signalListenerList: Array<ListenerInterface<keyof AlwatrSignals>> = []; | ||
|
||
override disconnectedCallback(): void { | ||
super.disconnectedCallback(); | ||
|
||
this._signalListenerList.forEach((listener) => listener.remove()); | ||
} | ||
} | ||
|
||
return SignalMixinClass as unknown as Constructor<SignalMixinInterface> & ClassType; | ||
} |