Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/type/console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export enum ConsoleMethods {
TIME = "time",
TIME_LOG = "timeLog",
TIME_END = "timeEnd",
COUNT = "count",
COUNT_RESET = "countReset",
CLEAR = "clear",
TABLE = "table",
}
Expand Down
29 changes: 29 additions & 0 deletions src/utils/services/console-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { getArrayIntersection, getType, isBaseData } from "@utils/tools"
import { AnyArray, AnyObject } from "@type/interface"
import { ConsoleMethods, ConsoleUpdateType, ITableLogInfo, LogInfo, LogType, enableConsoleMethods } from "@type/console"
import { processConsoleValueList } from "@utils/tools/console-value"
import { isUndefined } from "@utils/tools/judge"

export interface IConsoleOptions {
/** 刷新前回调 */
Expand All @@ -19,6 +20,7 @@ export default class ConsoleService {
/** iframe中的window */
private window!: Window & typeof globalThis
private timeStamps: Record<string, number> = {}
private countMap: Record<string, number> = {}
private consoleOptions: IConsoleOptions = {}

constructor(iframe?: HTMLIFrameElement) {
Expand All @@ -29,6 +31,7 @@ export default class ConsoleService {
if (iframe) {
this.iframe = iframe
this.window = this.iframe!.contentWindow as Window & typeof globalThis
this.clearCacheData()
this.setConsole()
} else {
if (!this.iframe) {
Expand Down Expand Up @@ -120,6 +123,25 @@ export default class ConsoleService {
return `${label}: ${duration}ms`
}

public count(label: string = "default") {
const countMap = this.countMap
if (isUndefined(countMap[label])) {
countMap[label] = 1
} else {
countMap[label]++
}
this.log(`${label}: ${countMap[label]}`)
}

public countReset(label: string = "default") {
const countMap = this.countMap
if (isUndefined(countMap[label])) {
this.warn(`Counter "${label}" doesn’t exist.`)
} else {
countMap[label] = 0
}
}

public assert(...args: AnyArray) {
const [assertion, ...restArgs] = args
const result = this.window.eval(assertion)
Expand Down Expand Up @@ -206,6 +228,11 @@ export default class ConsoleService {
this.consoleOptions.onLogsUpdated?.(ConsoleUpdateType.ADD, logInfo)
}

private clearCacheData = () => {
this.timeStamps = {}
this.countMap = {}
}

private setConsole() {
enableConsoleMethods.forEach((method) => {
// @ts-expect-error: method为console内部方法
Expand All @@ -227,6 +254,8 @@ export default class ConsoleService {
[ConsoleMethods.TIME]: () => this.time(...args),
[ConsoleMethods.TIME_LOG]: () => this.timeLog(...args),
[ConsoleMethods.TIME_END]: () => this.timeEnd(args[0]),
[ConsoleMethods.COUNT]: () => this.count(args[0]),
[ConsoleMethods.COUNT_RESET]: () => this.countReset(args[0]),
[ConsoleMethods.ASSERT]: () => this.assert(...args),
[ConsoleMethods.CLEAR]: () => this.clear(),
[ConsoleMethods.TABLE]: () => this.table(args[0], args[1]),
Expand Down