@leafer/debug 1.0.0-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023-present, Chao (Leafer) Wan
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1 @@
1
+ # @leafer/debug
package/package.json ADDED
@@ -0,0 +1,25 @@
1
+ {
2
+ "name": "@leafer/debug",
3
+ "version": "1.0.0-alpha.1",
4
+ "description": "@leafer/debug",
5
+ "author": "Chao (Leafer) Wan",
6
+ "license": "MIT",
7
+ "main": "src/index.ts",
8
+ "files": ["src"],
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "https://github.com/leaferjs/leafer.git"
12
+ },
13
+ "homepage": "https://github.com/leaferjs/leafer/tree/main/packages/debug",
14
+ "bugs": "https://github.com/leaferjs/leafer/issues",
15
+ "keywords": [
16
+ "leafer",
17
+ "leaferjs"
18
+ ],
19
+ "dependencies": {
20
+ "@leafer/math": "1.0.0-alpha.1"
21
+ },
22
+ "devDependencies": {
23
+ "@leafer/interface": "1.0.0-alpha.1"
24
+ }
25
+ }
package/src/Debug.ts ADDED
@@ -0,0 +1,66 @@
1
+ import { IObject } from '@leafer/interface'
2
+
3
+
4
+ export class Debug {
5
+
6
+ static enable: boolean
7
+
8
+ static filterList: string[] = []
9
+ static excludeList: string[] = []
10
+
11
+ // other
12
+ static __: IObject = {}
13
+
14
+ @debugAttr()
15
+ static showRepaint: boolean
16
+
17
+
18
+ public name: string
19
+
20
+ constructor(name: string) {
21
+ this.name = name
22
+ }
23
+
24
+ static get(name: string): Debug {
25
+ return new Debug(name)
26
+ }
27
+
28
+ static set filter(name: string | string[]) {
29
+ if (!name) name = []
30
+ else if (typeof name === 'string') name = [name]
31
+ this.filterList = name
32
+ }
33
+
34
+ static set exclude(name: string | string[]) {
35
+ if (!name) name = []
36
+ else if (typeof name === 'string') name = [name]
37
+ this.excludeList = name
38
+ }
39
+
40
+ log(...messages: unknown[]): void {
41
+ if (D.enable) {
42
+ if (D.filterList.length && D.filterList.every(name => name !== this.name)) return
43
+ if (D.excludeList.length && D.excludeList.some(name => name === this.name)) return
44
+ console.log('%c' + this.name, 'color:#21ae62', ...messages)
45
+ }
46
+ }
47
+
48
+ warn(...messages: unknown[]): void {
49
+ console.warn(this.name, ...messages)
50
+ }
51
+
52
+ error(...messages: unknown[]): void {
53
+ console.error(this.name, ...messages)
54
+ }
55
+ }
56
+
57
+ const D = Debug
58
+
59
+ function debugAttr(defaultValue?: unknown) {
60
+ return (target: IObject, key: string) => {
61
+ Object.defineProperty(target, key, {
62
+ get() { return target.enable ? target.__[key] : defaultValue },
63
+ set(value: unknown) { this.__[key] = value }
64
+ })
65
+ }
66
+ }
package/src/Run.ts ADDED
@@ -0,0 +1,53 @@
1
+ import { IncrementId } from '@leafer/math'
2
+ import { Debug } from './Debug'
3
+
4
+
5
+ interface ids {
6
+ [name: string]: number
7
+ }
8
+ interface names {
9
+ [name: string]: string
10
+ }
11
+
12
+
13
+ const debug = Debug.get('RunTime')
14
+
15
+ export class Run {
16
+
17
+ static currentId: number
18
+ static currentTime: number
19
+ static currentName: string
20
+
21
+ static idMap: ids = {}
22
+ static nameMap: names = {}
23
+ static nameToIdMap: ids = {}
24
+
25
+ static start(name: string, microsecond?: boolean): number {
26
+ const id = IncrementId.create(IncrementId.RUNTIME)
27
+ R.currentId = R.idMap[id] = microsecond ? performance.now() : Date.now()
28
+ R.currentName = R.nameMap[id] = name
29
+ R.nameToIdMap[name] = id
30
+ return id
31
+ }
32
+
33
+ static end(id: number, microsecond?: boolean): void {
34
+ const time = R.idMap[id]
35
+ const name = R.nameMap[id]
36
+ R.idMap[id] = undefined
37
+ R.nameMap[id] = undefined
38
+ R.nameToIdMap[name] = undefined
39
+
40
+ if (microsecond) {
41
+ debug.log(name, performance.now() - time, 'µs')
42
+ } else {
43
+ debug.log(name, Date.now() - time, 'ms')
44
+ }
45
+ }
46
+
47
+ static endOfName(name: string, microsecond?: boolean): void {
48
+ const id = R.nameToIdMap[name]
49
+ if (id) R.end(id, microsecond)
50
+ }
51
+ }
52
+
53
+ const R = Run
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from './Run'
2
+ export * from './Debug'