-
Notifications
You must be signed in to change notification settings - Fork 46
/
debug.ts
38 lines (33 loc) · 1.13 KB
/
debug.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { EventEmitter } from '@pixi/utils';
import { Message } from "./message"
export namespace Debug {
const messages: Message[] = []
const eventEmitter = new EventEmitter()
export function on(event: string | symbol, fn: EventEmitter.ListenerFn, context: any) {
eventEmitter.on(event, fn, context)
}
export function warn(message: Message, args?: any) {
if (!messages.includes(message)) {
messages.push(message)
let formatted = formatMessage(message, args)
console.warn(`PIXI3D: ${formatted}`)
eventEmitter.emit("warn", formatted)
}
}
export function error(message: Message, args?: any) {
if (!messages.includes(message)) {
messages.push(message)
let formatted = formatMessage(message, args)
console.error(`PIXI3D: ${formatted}`)
eventEmitter.emit("error", formatted)
}
}
function formatMessage(message: Message, args?: any) {
let formatted = <string>message
let match: RegExpExecArray | null
while ((match = /{(\w*)}/g.exec(formatted)) !== null && args) {
formatted = formatted.replace(match[0], args[match[1]])
}
return formatted
}
}