-
Notifications
You must be signed in to change notification settings - Fork 27
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
Showing
5 changed files
with
157 additions
and
19 deletions.
There are no files selected for viewing
44 changes: 25 additions & 19 deletions
44
packages/components/src/components/docs/bal-doc-app/bal-doc-app.tsx
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
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
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,116 @@ | ||
import { ComponentInterface } from '@stencil/core' | ||
import { isWindowDefined } from './browser' | ||
|
||
export interface BalLogger { | ||
components: string[] | ||
custom: boolean | ||
lifecycle: boolean | ||
render: boolean | ||
event: boolean | ||
} | ||
|
||
export interface Loggable { | ||
log: LogInstance | ||
createLogger(log: LogInstance): void | ||
} | ||
|
||
export type LogInstance = (message?: any, ...optionalParams: any[]) => void | ||
|
||
export const defaultLoggerConfig: BalLogger = { | ||
components: [], | ||
event: false, | ||
lifecycle: false, | ||
render: false, | ||
custom: false, | ||
} | ||
|
||
const getConfig = () => { | ||
let loggerConfig = defaultLoggerConfig | ||
|
||
if ( | ||
isWindowDefined() && | ||
(window as any).BaloiseDesignSystem && | ||
(window as any).BaloiseDesignSystem.config && | ||
(window as any).BaloiseDesignSystem.config.logger | ||
) { | ||
loggerConfig = (window as any).BaloiseDesignSystem.config.logger | ||
} | ||
|
||
return loggerConfig | ||
} | ||
|
||
const buildLogger = (tag: string) => { | ||
const l = (type: 'lifecycle' | 'event' | 'custom' | 'render', message?: any, ...optionalParams: any[]) => { | ||
const config = getConfig() | ||
|
||
if (config.components.includes(tag) && config[type]) { | ||
console.log(message, ...optionalParams) | ||
} | ||
} | ||
|
||
return { | ||
custom: (message?: any, ...optionalParams: any[]) => l('custom', ` ➡️ [${tag}] - ${message}`, ...optionalParams), | ||
lifecycle: (lifecycleName: string, ...optionalParams: any[]) => | ||
l( | ||
'lifecycle', | ||
`${ | ||
lifecycleName === 'connectedCallback' | ||
? '🟢' | ||
: lifecycleName === 'disconnectedCallback' | ||
? '🔴' | ||
: lifecycleName === 'componentDidLoad' | ||
? '🏁' | ||
: ' ➡️' | ||
} [${tag}] - (${lifecycleName})`, | ||
...optionalParams, | ||
), | ||
event: (eventName: string, ...optionalParams: any[]) => | ||
l('event', `🔥 [${tag}] - (${eventName})`, ...optionalParams), | ||
render: (...optionalParams: any[]) => l('render', `🖌️ [${tag}] - (render)`, ...optionalParams), | ||
} | ||
} | ||
|
||
export function Logger(tag = 'unknown') { | ||
return function (target: ComponentInterface, _propertyKey: string, _descriptor: PropertyDescriptor) { | ||
const { connectedCallback, disconnectedCallback, render, componentDidLoad, createLogger } = target | ||
|
||
const log = buildLogger(tag) | ||
|
||
target.connectedCallback = function () { | ||
log.lifecycle(`connectedCallback`, this) | ||
|
||
const events = Object.keys(this) | ||
.filter(n => n.startsWith('bal')) | ||
.filter(n => typeof this[n] === 'object') | ||
|
||
for (let index = 0; index < events.length; index++) { | ||
const event = events[index] | ||
const emitter = this[event] | ||
this[event] = { | ||
emit: (...args: any) => { | ||
log.event(event, this, ...args) | ||
return emitter.emit.call(this, ...args) | ||
}, | ||
} | ||
} | ||
|
||
createLogger.call(this, log.custom) | ||
return connectedCallback && connectedCallback.call(this) | ||
} | ||
|
||
target.disconnectedCallback = function () { | ||
log.lifecycle(`disconnectedCallback`, this) | ||
return disconnectedCallback && disconnectedCallback.call(this) | ||
} | ||
|
||
target.componentDidLoad = function () { | ||
log.lifecycle(`componentDidLoad`, this) | ||
return componentDidLoad && componentDidLoad.call(this) | ||
} | ||
|
||
target.render = function () { | ||
log.render(this) | ||
return (render as any).call(this) | ||
} | ||
} | ||
} |